在标题导航栏中居中垂直

时间:2016-05-19 13:39:06

标签: html css alignment

我在标题中创建了一个导航栏,并将其与另一个元素对齐,以便它们居中。但是,在向标题添加图像后,其中一个元素不再居中:

#header nav {
  display: inline;
}
#header {
  background: #5D6D63;
  padding: 28px 0 26px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  width: 100%;
}
#header nav ul {
  float: right;
  margin: 0px;
  margin-right: 100px;
}
li {
  list-style-type: none;
  float: left;
  margin-left: 46px;
}
a[href^="#"] {
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  letter-spacing: -1px;
  text-decoration: none
}
a[href="#top"] {
  margin-left: 100px;
}
<header id="header">
  <a href="#top">Name</a>
  <a href="https://github.com/">
    <img src="assets/imgs/GitHub-Mark-Light-64px.png" alt="GitHub" height="24" width="24">
  </a>
  <nav id="navbar">
    <ul>
      <li><a href="#">Link 1</a>
      </li>
      <li><a href="#">Link 2</a>
      </li>
      <li><a href="#">Link 3</a>
      </li>
    </ul>
  </nav>
</header>

问题是Name锚标记的垂直对齐方式。它应该与github图像和导航栏对齐。如果我删除github图像,它工作正常,它是对齐的。为什么github图像会导致Name锚标记向下推几个像素?

以下是显示未对齐(带图像)和对齐(无图像)的图片

Misaligned

Aligned

4 个答案:

答案 0 :(得分:2)

因为内联元素的默认vertical alignmentbaseline,您需要middle。所以添加一个规则,如:

img {
  vertical-align:middle;
}

&#13;
&#13;
#header nav{
    display:inline;
}
#header {
    background: #5D6D63;
    padding: 28px 0 26px;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    width: 100%;

}
#header nav ul {
    float: right;
    margin: 0px;
    margin-right: 100px;
}
li {
    list-style-type: none;
    float: left;
    margin-left: 46px; 
}
a[href^="#"]{
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    letter-spacing: -1px;
    text-decoration: none
}

a[href="#top"] {
    margin-left:100px;
}
img {
  vertical-align:middle;
}
&#13;
 <header id="header"> 
        <a href="#top">Name</a>
        <a href="https://github.com/">
            <img src="http://www.uoyabause.org/images/logos/GitHub-Mark-Light-64px.png" alt="GitHub" height="24" width="24">
        </a>
        <nav id="navbar">
          <ul>
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
            <li><a href="#">Link 3</a></li>       
          </ul>
        </nav>
    </header>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

正如j08691(及其他人)所述,您正在寻找的财产是vertical-align:middle

但是,有一点需要注意:你必须记住默认情况下它旁边的图像和锚标签都有vertical-align:baseline - 这意味着,如果你只将它们中的一个垂直居中,它的基线将垂直对齐到兄弟元素的中心,而不是它的中间。 (根据您应用于哪个元素,这可能会使您的对齐问题恶化。)

以下是您需要修改/添加的样式块:

a[href="#top"] {
  margin-left: 100px;
  vertical-align: middle;
}
#header img {
  vertical-align: middle;
}

&#13;
&#13;
#header nav {
  display: inline;
}
#header {
  background: #5D6D63;
  padding: 28px 0 26px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  width: 100%;
}
#header nav ul {
  float: right;
  margin: 0px;
  margin-right: 100px;
}
li {
  list-style-type: none;
  float: left;
  margin-left: 46px;
}
a[href^="#"] {
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  letter-spacing: -1px;
  text-decoration: none
}
a[href="#top"] {
  margin-left: 100px;
  vertical-align: middle;
}
#header img {
  vertical-align: middle;
}
&#13;
<header id="header">
  <a href="#top">Name</a>
  <a href="https://github.com/">
    <img src="assets/imgs/GitHub-Mark-Light-64px.png" alt="GitHub" height="24" width="24">
  </a>
  <nav id="navbar">
    <ul>
      <li><a href="#">Link 1</a>
      </li>
      <li><a href="#">Link 2</a>
      </li>
      <li><a href="#">Link 3</a>
      </li>
    </ul>
  </nav>
</header>
&#13;
&#13;
&#13;

希望这有帮助!如果您有任何问题,请告诉我。

答案 2 :(得分:0)

使图像Verticla对齐: 添加Css像这样:

a img{
   vertical-align:middle
}

答案 3 :(得分:0)

您可以将#header显示为flex并使用align-content: center;

&#13;
&#13;
#header nav{
    display:inline;
}
#header {
    display: flex; /* NEW: Makes the container as flexbox */
    align-content: center; /* NEW: Center the items inside the container vertically */
    background: #5D6D63;
    padding: 28px 0 26px;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    width: 100%;

}
#header nav ul {
    float: right;
    margin: 0px;
    margin-right: 100px;
}
li {
    list-style-type: none;
    float: left;
    margin-left: 46px; 
}
a[href^="#"]{
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    letter-spacing: -1px;
    text-decoration: none
}

a[href="#top"] {
    margin-left:100px;
}
&#13;
<header id="header"> 
        <a href="#top">Name</a>
        <a href="https://github.com/">
            <img src="http://www.uoyabause.org/images/logos/GitHub-Mark-Light-64px.png" alt="GitHub" height="24" width="24">
        </a>
        <nav id="navbar">
          <ul>
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
            <li><a href="#">Link 3</a></li>       
          </ul>
        </nav>
    </header>
&#13;
&#13;
&#13;