html - CSS 导航垂直对齐

标签 html css vertical-alignment

我不知道获得我想要的结果的最佳实践。我在这里使用了表格,这让我接近了我想要的东西。

结果没有垂直居中的文本,我不知道怎么做。尝试使用 UL 来获得这个但没有成功:

#hotspotbg table {
  margin-top: 1px;
}
#hotspotbg table tr td {
  border-right: 1px solid #ccc;
  border-left: 1px solid #ccc;
}
#hotspotbg table tr td a {
  text-align: center;
  text-decoration: none;
  display: block;
  font-family: Tahoma, Geneva, sans-serif;
  color: #fff;
  height: 51px;
}
#hotspotbg table tr td a:hover {
  background: #FFF;
  color: #000;
}
<table width="900" height="51px" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td width="25%" height="51" valign="middle"><a href="#">Home</a>
    </td>
    <td width="25%"><a href="#">Products</a>
    </td>
    <td width="25%"><a href="#">Reviews</a>
    </td>
    <td width="25%"><a href="#">Contact</a>
    </td>
  </tr>
</table>

最佳答案

将子项(在您的情况下为 a 元素)的 line-height 设置为父项的高度,将垂直对齐a 元素的文本。有关更多信息,请参见下文。

.wrapper {
    height: 70px;
    width: 100%;
    background: red;
    overflow: hidden;
}

.wrapper a {
    /*NOT AFFECTING THE VERTICAL ALIGN*/
    display: block;
    float: left;
    height: 70px;
    width: 50%;
    color: #fff;
    text-align: center;
    /*AFFECTING VERTICAL ALIGN*/
    line-height: 70px;
}
<div class="wrapper">
    <a href="#">test</a>
    <a href="#">nice</a>
</div>

关于html - CSS 导航垂直对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31328678/

相关文章:

javascript - 为什么我的进度条在开始加载之前显示完整的进度值

javascript - 如何根据窗口大小剃掉部分图像

HTML5 网页在 IE8 中乱显示

css - IE9 剪辑固定的绝对边界半径的 child

css - 如何垂直居中导航栏元素(Twitter Bootstrap)?

html - 水平对齐没有 float 的div

html - 当容器具有最小和最大高度时,CSS overflow auto 不起作用

javascript - 停止条形图在图表中居中

html - 将相对定位的 child 保持在父 block 内 max-top equivalent needed

css - 不能垂直对齐一行内的内容