【问题标题】:Centering elements vertically in a fixed-height container [duplicate]在固定高度容器中垂直居中元素[重复]
【发布时间】:2017-10-13 01:21:35
【问题描述】:

我有一个需要基于像素的导航栏的网站 - 在本例中为 height: 80px

问题是,我无法将ulli 元素垂直居中。

我尝试过使用:top:50%; transform: translate(0,-50%),如我的 jsfiddle 中所示,也尝试过使用 flex 定位,但都没有奏效。

Jsfiddle:https://jsfiddle.net/agreyfield91/w3s8cj92/

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;
}

nav ul {
  list-style: none;
  text-align: center;
  margin: 0;
  padding: 0;
  top: 50%;
  transform: translate(0, -50%);
}

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; overflow: hidden; 添加到nav ul 规则。 position: relative; 使转换工作和overflow: hidden; 使其从其子项中获取高度(浮动并需要清除)。 Fiddle demo

标签: html css flexbox css-position


【解决方案1】:

您需要将position: relative 添加到navposition: absolutenav ul(以使top: 50%; transform: translate(0,-50%); 工作),或者您可以在nav 上使用display: flex; align-items: center。我只会使用 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>

【讨论】:

    【解决方案2】:

    添加显示 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>

    【讨论】:

      【解决方案3】:

      您的代码似乎可以简单得多:

      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>

      【讨论】:

      • 我在链接周围添加了边框以显示可点击区域,即标题的整个高度。此处的其他答案仅涵盖文本。不知道你想走哪条路。
      【解决方案4】:

      如果您有块的静态高度,则可以将带有无包装容器的元素居中,垂直居中对齐 inline[-block] 元素。

      .vertical-container {
        height: 80px;
        border: 1px solid black;
        white-space: nowrap;
        /* this prevents vertical block to "fall out" of container */
      }
      
      .vertical-aligner {
        display: inline-block;
        vertical-align: middle;
        height: 100%;
        width: 0;
      }
      
      .vertical-content {
        display: inline-block;
        vertical-align: middle;
        border: 1px solid red;
        white-space: normal;
        /* reset white-space for content */
      }
      &lt;div class="vertical-container"&gt;&lt;span class="vertical-aligner"&gt;&lt;/span&gt;&lt;span class="vertical-content"&gt;Text that should be vertically middle of container&lt;/span&gt;&lt;/div&gt;

      我已经相应地更新了你的小提琴:https://jsfiddle.net/w3s8cj92/2/

      附:我想提供更多信息,但稍后

      【讨论】:

        【解决方案5】:

        CSS替换

        header {
            height: 80px;
            position: fixed;
            top: 0;
            transition: top 0.2s ease-in-out;
            width: 100%;
            display: table;
        }
        
        footer { background: #ddd;}
        * { color: transparent}
        nav {
            height: 100%;
          width: 100%;
          background-color: bisque;
        }
        nav ul {
            margin: 0;
            padding: 0;
            display: table-cell;
            vertical-align: middle;
            height: 80px;
        }
        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 css 和 nav ul css 它的工作正常
        猜你喜欢
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-22
        • 1970-01-01
        • 2017-06-28
        • 1970-01-01
        • 2018-01-20
        相关资源
        最近更新 更多