使用CSS

时间:2018-06-02 14:28:53

标签: html css vertical-alignment

如您所见,图标与输入框不完全对齐:

this image

我只想让图标与输入框对齐而不能使用position absolute,因为输入框会溢出其他内容。

我能做些什么来做对吗?



header.toplogo {
  overflow: hidden;
  margin-top: 10px;
  position: relative;
  display: grid;
  grid-template-columns: 170px 1fr 100px
}

header.toplogo div {
  width: 100%;
  margin: 0 auto;
  line-height: 150px;
  vertical-align: middle;
  grid-column: 2/3;
}

header.toplogo div i {
  background-color: #ff3232;
  padding: 10px 12px 14px 12px;
  border-radius: 5px 0 0 5px;
  color: white;
  font-size: 18px;
  transition: 0.6s;
}

<header class="toplogo" style="direction:ltr">
  <div><i class="fa fa-search"></i><input type="text" name="search" placeholder="Search ..."></div>
</header>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

这是我的解决方案

将此添加到ur css

input {
  box-sizing: border-box;
  height: 42px;

}

vertical align:middle添加到i代码

&#13;
&#13;
header.toplogo {
  overflow: hidden;
  margin-top: 10px;
  position: relative;
  display: grid;
  grid-template-columns: 170px 1fr 100px
}

header.toplogo div {
  width: 100%;
  margin: 0 auto;
  line-height: 150px;
  vertical-align: middle;
  grid-column: 2/3;
}

header.toplogo div i {
  background-color: #ff3232;
  padding: 10px 12px 14px 12px;
  border-radius: 5px 0 0 5px;
  color: white;
  font-size: 18px;
  transition: 0.6s;
  vertical-align: middle;
}

input {
  box-sizing: border-box;
  height: 42px;
 
}
&#13;
<link href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" rel="stylesheet" />
<header class="toplogo" style="direction:ltr">
  <div><i class="fas fa-search"></i><input type="text" name="search" placeholder="Search ..."></div>
</header>
&#13;
&#13;
&#13;