如何在导航栏中间对齐项目?

时间:2019-05-27 09:33:42

标签: html css css3 sass flexbox

导航栏中有6个项目:

  1. 前三个项目应与flex-start对齐-在导航栏的开头。

  2. 然后徽标应位于导航栏的中心

  3. 最后-> flex-end应该有2个图标

这是导航栏当前状态的屏幕截图:

enter image description here

问题:是徽标的位置-正在使用margin-left: '27%'。并且在不同的屏幕尺寸上徽标不能很好地对齐。

我想调整徽标flex槽的方式,有没有办法用flex做到这一点?

检查代码:

HTML / jsx:

 <div className="container">
  <header className="header">
    <nav className="user-nav">
      <div className="user-nav-item">
        <Link href="/">
          <a className="user-nav-item-link">Overview</a>
        </Link>
      </div>
      <div className="user-nav-item">
        <Link href="/search">
          <a className="user-nav-item-link">Search</a>
        </Link>
      </div>
      <div className="user-nav-item">
        <Link href="/feed">
          <a className="user-nav-item-link">Feed</a>
        </Link>
      </div>
      <h3 className="logo">Logo</h3>
    </nav>

    <div className="user-nav-icon">
      <div className="user-nav-icon-box">
        <img src={bellIcon} alt="notify icon" />
      </div>
      <div className="user-nav-icon-box">
        <img src={settingsIcon} alt="settings icon" />
      </div>
    </div>
  </header>
</div>

CSS:

.container {
  max-width: 100vw;
  display: flex;
  flex-direction: column;


.header {
  display: flex;
  align-items: center;
  height: 5rem;
  color: #fff;
  background-color: black;

  .user-nav {
    width: 100%;
    display: flex;
    align-items: center;

    &-item {
      width: 5.5rem;
      padding: 1.5rem;
      cursor: pointer;
    }

    &-item-link {
      text-decoration: none;
      color: white;
    }

    .logo {
      margin-left: 27%;
    }

    &-icon {
      display: flex;
      align-items: center;
      background-color: white;
      color: red;
      margin-right: 3rem;

      & > * {
        padding: 0 0.8rem;
        cursor: pointer;
      }

      &-icon-notification {
        color: red;
      }
    }
  }
}

}

3 个答案:

答案 0 :(得分:2)

使用Flex Box。要实现这一目标将更加困难,我有另一种选择。请在Codepen上测试以下代码:

这是您的html:

<div class="parent">
  <div class="nav-menu"> Your Menu</div>
  <div class="logo"></div>
  <div class="icons">Your Icons</div>
</div>

这是您的CSS:

.parent{
  width: 100%;
  min-height: 80px;
  background-color: yellow;
  display: relative;
}
.nav-menu,
.icons{
  display: inline-block;
}
.icons{
  float: right;
}
.logo{
  width: 150px;
  height: 40px;
  position: absolute;
  left: 50%;
   background-color: green;
  transform: translateX(-50%);
}

答案 1 :(得分:2)

您可以简单地从导航中获取徽标,以便徽标,导航和图标这三个都成为弹性项目,并使用space-between来证明标题的内容合理。下面是简化的代码。

P.S。 -共享呈现的代码作为您将来的实现,而不是JSX / SASS

.container {
  max-width: 100vw;
  display: flex;
  flex-direction: column;
}
.container .header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 5rem;
  color: #fff;
  background-color: black;
}
.container .header .user-nav {
  display: flex;
  align-items: center;
}
.container .header .user-nav-item {
  padding: 1.5rem;
  cursor: pointer;
}
.container .header .user-nav-item-link {
  text-decoration: none;
  color: white;
}
.container .header .user-nav-icon {
  display: flex;
  align-items: center;
  background-color: white;
  color: red;
  margin-right: 3rem;
}
.container .header .user-nav-icon > * {
  padding: 0 0.8rem;
  cursor: pointer;
}
.container .header .user-nav-icon-icon-notification {
  color: red;
}
<div class="container">
  <header class="header">
    <nav class="user-nav">
      <div class="user-nav-item">
        <a class="user-nav-item-link">Overview</a>
      </div>
      <div class="user-nav-item">
        <a class="user-nav-item-link">Search</a>
      </div>
      <div class="user-nav-item">
        <a class="user-nav-item-link">Feed</a>
      </div>
    </nav>
    <h3 class="logo">Logo</h3>
    <div class="user-nav-icon">
      <div class="user-nav-icon-box">
        Bell
      </div>
      <div class="user-nav-icon-box">
        Settings
      </div>
    </div>
  </header>
</div>

答案 2 :(得分:1)

以下是使用display的答案:flex; Flexbox是最佳且优雅的方法,可将项目对齐到页面中间,而无需通过Margin,Transform等...进行粗俗的计算。

<html>
  <head>
    <style>
      *, .container {
        width: 100%;
        top: 0;
        left: 0;
        padding: 0;
        margin: 0;
        color: ivory;
        font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
        font-size: 13px;
      }

      .header {
        display: flex;
        align-items: center;
        padding: 10px;
        background-color: gray;
      }

      .user-nav {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 30%;
      }

      .user-nav-item {
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .logo {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40%;
      }

      .user-nav-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 30%;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <header class="header">
        <nav class="user-nav">
          <div class="user-nav-item">
            <Link href="/">
              <a class="user-nav-item-link">Overview</a>
            </Link>
          </div>
          <div class="user-nav-item">
            <Link href="/search">
              <a class="user-nav-item-link">Search</a>
            </Link>
          </div>
          <div class="user-nav-item">
            <Link href="/feed">
              <a class="user-nav-item-link">Feed</a>
            </Link>
          </div>
        </nav>
        <h3 class="logo">Logoooooooooooooooooooooo</h3>
        <div class="user-nav-icon">
          <div class="user-nav-icon-box">
            <img src={bellIcon} alt="notify icon" />
          </div>
          <div class="user-nav-icon-box">
            <img src={settingsIcon} alt="settings icon" />
          </div>
        </div>
      </header>
    </div>
  </body>
</html>