【问题标题】:flex Items are not aligning vertically in navbar [duplicate]flex项目在导航栏中没有垂直对齐[重复]
【发布时间】:2019-03-17 21:35:39
【问题描述】:

我试图让我的两个 UL 在垂直中心(横轴)对齐。我是在 Sass 中做的,我可能会因为嵌套太多而感到困惑。我说的是 ul 是 nav 的直接子代,即“nav-main-list”和“nav-social-list”。

这是我的 HTML 代码:

          * {
      margin: 0;
      padding: 0;
    }
    
    body {
      background: #f3c6c6;
    }
    
    ul {
      list-style-type: none;
    }
    
    a {
      text-decoration: none;
      color: inherit;
    }
    
    nav {
      background-color: #d42e2e;
      height: 3rem;
      width: 100%;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-items: center;
      justify-content: center;
      color: whitesmoke;
    }
    nav ul {
      height: 100%;
      width: 50%;
      display: flex;
      flex-direction: row;
      align-items: center;
      margin: 0 2rem;
    }
    nav ul li {
      height: 100%;
      margin: 0;
      margin-top: auto;
      padding: 0 0.5rem;
    }
    nav ul li:hover {
      color: #d42e2e;
      background: whitesmoke;
    }
    nav ul li:hover ul {
      margin: 1rem 0;
      width: 100%;
      display: flex;
      flex-direction: column;
    }
    nav ul li:hover ul li {
      width: 100%;
      background: #d42e2e;
      color: whitesmoke;
    }
    nav ul li:hover ul li:hover {
      color: #d42e2e;
      background: whitesmoke;
    }
    nav ul li ul {
      display: none;
    }
    nav .nav-main-list {
      text-transform: uppercase;
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      font-size: 1.2rem;
    }
    nav .nav-social-list {
      justify-content: flex-end;
      font-family: "Courier New", Courier, monospace;
      font-size: 1rem;
      align-content: center;
      align-items: center;
    }
    <body>
    <nav>
        <ul class="nav-main-list">
            <li><a href="#">Home</a></li>
            <li><a href="#">Languages</a>
                <ul class="lang-list">
                    <li><a href="#">html</a></li>
                    <li><a href="#">css</a></li>
                    <li><a href="#">sass</a></li>
                    <li><a href="#">php</a></li>
                    <li><a href="#">jquery</a></li>
                </ul>
            </li>
            <li><a href="#">Frameworks</a>
                <ul class="frame-list">
                    <li><a href="#">Bootstrap</a></li>
                    <li><a href="#">Laravel</a></li>
                    <li><a href="#">Angular</a></li>
                </ul>
            </li>
            <li><a href="#">Tools</a>
                <ul class="tools-list">
                    <li class="a" href="#">Git</li>
                    <li class="a" href="#">Photoshop</li>
                    <li class="a" href="#">Github</li>
                </ul>
            </li>
        </ul>
        <ul class="nav-social-list">
            <li><a href="#">Fb</a></li>
            <li><a href="#">Twitter</a></li>
            <li><a href="#">Git</a></li>
            <li><a href="#">Codepen</a></li>
        </ul>
    </nav>
</body>


  

问题是两个 ul 都在顶部而不是在导航栏的中心。我可能会通过从头开始重新编码来正确地做到这一点,但我想找出错误或它不起作用的原因。

谢谢你,祝你有美好的一天!!!

【问题讨论】:

  • 只需将display: flex; align-items: center; 添加到li 元素。它在副本中进行了解释。 jsfiddle.net/d0xc5738

标签: html css flexbox


【解决方案1】:

我认为您看到的问题与填充和边距有关。我对这些做了一些调整,不确定是否有所改进。

   * {
      margin: 0;
      padding: 0;
    }
    
    body {
      background: #f3c6c6;
    }
    
    ul {
      list-style-type: none;
    }
    
    a {
      text-decoration: none;
      color: inherit;
    }
    
    nav {
      background-color: #d42e2e;
      height: 2.5rem;
      width: 100%;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-items: center;
      justify-content: center;
      color: whitesmoke;
    }
    nav ul {
      height: 100%;
      width: 50%;
      display: flex;
      flex-direction: row;
      align-items: center;
      margin: 0 2rem;
      vertical-align: middle;
    }
    nav ul li {
      height: 100%;
      margin: 0;
      margin-top: auto;
      padding: 0.25rem 0.5rem;
    }
    nav ul li:hover {
      color: #d42e2e;
      background: whitesmoke;
    }
    nav ul li:hover ul {
      margin: 0.5rem 0 0.25rem 0;
      width: 100%;
      display: flex;
      flex-direction: column;
    }
    nav ul li:hover ul li {
      width: 100%;
      background: #d42e2e;
      color: whitesmoke;
    }
    nav ul li:hover ul li:hover {
      color: #d42e2e;
      background: whitesmoke;
    }
    nav ul li ul {
      display: none;
    }
    nav .nav-main-list {
      text-transform: uppercase;
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
      font-size: 1.2rem;
    }
    nav .nav-social-list {
      justify-content: flex-end;
      font-family: "Courier New", Courier, monospace;
      font-size: 1rem;
      align-content: center;
      align-items: center;
    }
<body>
<nav>
    <ul class="nav-main-list">
        <li><a href="#">Home</a></li>
        <li><a href="#">Languages</a>
            <ul class="lang-list">
                <li><a href="#">html</a></li>
                <li><a href="#">css</a></li>
                <li><a href="#">sass</a></li>
                <li><a href="#">php</a></li>
                <li><a href="#">jquery</a></li>
            </ul>
        </li>
        <li><a href="#">Frameworks</a>
            <ul class="frame-list">
                <li><a href="#">Bootstrap</a></li>
                <li><a href="#">Laravel</a></li>
                <li><a href="#">Angular</a></li>
            </ul>
        </li>
        <li><a href="#">Tools</a>
            <ul class="tools-list">
                <li class="a" href="#">Git</li>
                <li class="a" href="#">Photoshop</li>
                <li class="a" href="#">Github</li>
            </ul>
        </li>
    </ul>
    <ul class="nav-social-list">
        <li><a href="#">Fb</a></li>
        <li><a href="#">Twitter</a></li>
        <li><a href="#">Git</a></li>
        <li><a href="#">Codepen</a></li>
    </ul>
</nav>

(见great article from CSS Tricks for a complete guide to using flex。)

【讨论】:

  • 这并没有解决问题。它把 li 放在彼此的下方。
  • 抱歉,我以为它是侧边栏导航。我会相应地进行编辑。
  • 我已将flex-direction: column 添加到nav,并将flex-direction: row 添加到nav ul,但看起来很混乱:(
  • 是的,子菜单是下拉菜单,它们按我的意愿工作。让我更新样式表。
  • 这是我之前准备的一个:codepen.io/vogelbeere/pen/jxMVVG
猜你喜欢
  • 2021-12-20
  • 1970-01-01
  • 2012-08-16
  • 2017-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 2018-09-30
相关资源
最近更新 更多