如何在div中垂直居中链接

时间:2016-10-27 20:00:25

标签: html css

现在搜索链接卡在div的顶部。如何使此链接在div中垂直居中?

.advanced-button {
  width: 200px;
  height: 55px;
  background-color: #f4c23d;
  line-height: 40px;
}
.advanced-button a {
  margin-left: 15px;
  font-size: 16px;
  font-family: 'Montserrat', sans-serif;
  color: #222222;
  font-weight: 700;
  text-transform: uppercase;
  vertical-align: middle;
}
.advanced-button i {
  float: right;
  width: 40px;
  height: 55px;
  line-height: 40px;
  text-align: center;
  background-color: #f0b20e;
}
<div class="advanced-button">
  <a href="#">Search Now<i class="fa fa-search"></i></a>
</div>

2 个答案:

答案 0 :(得分:3)

最简单的方法是在链接上设置行高以匹配div的高度:

&#13;
&#13;
.advanced-button {
  width: 200px;
  height: 55px;
  background-color: #f4c23d;
  line-height: 40px;
}
.advanced-button a {
  margin-left: 15px;
  font-size: 16px;
  font-family: 'Montserrat', sans-serif;
  color: #222222;
  font-weight: 700;
  text-transform: uppercase;
  vertical-align: middle;
  line-height:55px;
}
.advanced-button i {
  float: right;
  width: 40px;
  height: 55px;
  line-height: 40px;
  text-align: center;
  background-color: #f0b20e;
}
&#13;
<div class="advanced-button">
  <a href="#">Search Now<i class="fa fa-search"></i></a>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

只需在链接中添加line-height: 55px;即可。

.advanced-button {
  width: 200px;
  height: 55px;
  background-color: #f4c23d;
  line-height: 40px;
}
.advanced-button a {
  margin-left: 15px;
  font-size: 16px;
  font-family: 'Montserrat', sans-serif;
  color: #222222;
  font-weight: 700;
  text-transform: uppercase;
  vertical-align: middle;
  line-height: 55px;
}
.advanced-button i {
  float: right;
  width: 40px;
  height: 55px;
  line-height: 40px;
  text-align: center;
  background-color: #f0b20e;
}
<div class="advanced-button">
  <a href="#">Search Now<i class="fa fa-search"></i></a>
</div>