将元件垂直居中固定

这个问题在这里已经有了答案:

  • Flexbox:水平和垂直居中8个答案

  • 添加显示flex和align-self中心:https://jsfiddle.net/w3s8cj92/1/

    header {
      height: 80px;
      position: fixed;
      top: 0;
      transition: top 0.2s ease-in-out;
      width: 100%;
    }
    
    footer {
      background: #ddd;
    }
    
    * {
      color: transparent
    }
    
    nav {
      height: 100%;
      width: 100%;
      background-color: bisque;
      display: flex;
    }
    
    nav ul {
      list-style: none;
      text-align: center;
      margin: 0;
      padding: 0;
      top: 50%;
      align-self: center;
    }
    
    nav ul li {
      display: inline-block;
      float: left;
    }
    
    nav ul li a {
      display: block;
      padding: 15px;
      text-decoration: none;
      color: #aaa;
      font-weight: 800;
      text-transform: uppercase;
      margin: 0 10px;
    }
    <header class="nav-down">
      <nav class="headernav">
        <ul>
          <li><a href="#">Gig</a></li>
          <li><a href="#">ity</a></li>
          <li><a href="#">element</a></li>
        </ul>
      </nav>
    </header>

    您可能需要添加position: relative对于navposition: absolute nav ul (获取top: 50%; transform: translate(0,-50%);工作),或者您可以使用display: flex; align-items: center display: flex; align-items: center on nav 。 我只会使用flexbox。

    header {
      height: 80px;
      position: fixed;
      top: 0;
      transition: top 0.2s ease-in-out;
      width: 100%;
    }
    
    footer {
      background: #ddd;
    }
    
    * {
      color: transparent
    }
    
    nav {
      height: 100%;
      width: 100%;
      background-color: bisque;
      display: flex;
      align-items: center;
    }
    
    nav ul {
      list-style: none;
      text-align: center;
      margin: 0;
      padding: 0;
    }
    
    nav ul li {
      display: inline-block;
      float: left;
    }
    
    nav ul li a {
      display: block;
      padding: 15px;
      text-decoration: none;
      color: #aaa;
      font-weight: 800;
      text-transform: uppercase;
      margin: 0 10px;
    }
    <header class="nav-down">
      <nav class="headernav">
        <ul>
          <li><a href="#">Gig</a></li>
          <li><a href="#">ity</a></li>
          <li><a href="#">element</a></li>
        </ul>
      </nav>
    </header>

    看起来你的代码可能更简单:

    nav {
      height: 80px;
      position: fixed;
      top: 0;
      width: 100%;
      display: flex;
      justify-content: center;   /* horizontal alignment of child elements (optional) */
      background-color: bisque;
    }
    
    nav a {
      text-decoration: none;
      color: #aaa;
      font-weight: 800;
      text-transform: uppercase;
      display: flex;
      align-items: center;      /* vertical alignment of text */
      border: 1px dashed red;
    }
    
    a + a {
      margin-left: 20px;
    }
    <nav>
      <a href="#">Gig</a>
      <a href="#">ity</a>
      <a href="#">element</a>
    </nav>
    链接地址: http://www.djcxy.com/p/75997.html

    上一篇: Centering elements vertically in a fixed

    下一篇: List bullets like 1) 2) 3)