如何在导航栏中的“导航栏-品牌”下移动“导航”元素

时间:2019-03-09 11:58:54

标签: html css flexbox bootstrap-4 navbar

我使用Bootstrap 4,将navbar-brand设置在中间,并将所有元素设置在右侧。我想将我的nav元素设置在navbar-brand下。 enter image description here

此外,我想在nav的右侧设置“登录”和“注销”,但是使用mr-auto无效。

html:

<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
  <a href="#" class="navbar-brand">Academind</a>
  <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
          <span class="navbar-toggler-icon"></span>
      </button>
  <div class="collapse navbar-collapse" id="navbarMenu">
    <ul class="navbar-nav  ">
      <li class="nav-item ">
        <a href="#" class="nav-link">Users</a>
      </li>
      <li class="nav-item">
        <a href="#" class="nav-link">Products</a>
      </li>
    </ul>
    <ul class="navbar-nav  mr-auto ">
      <li class="nav-item ">
        <a href="#" class="nav-link">Login</a>
      </li>
      <li class="nav-item">
        <a href="#" class="nav-link">Log out</a>
      </li>
    </ul>
  </div>
</nav>

css:

@media (min-width: 768px) {
  .navbar-brand {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }
}

2 个答案:

答案 0 :(得分:2)

要右对齐登录名,您可以使用ml-auto自动填充左侧一侧,这会将链接推到右侧

enter image description here

选项1:

然后调整导航栏的min-height。使用align-items-end将链接推到底部,然后根据需要在顶部对齐品牌。这样将显示2个导航栏行。

@media (min-width: 567px) {
    .navbar-brand
    {
        position: absolute;
        top: 5px;
        left: 50%;
        transform: translateX(-50%);
    }
    .navbar {
        min-height: 90px;
    }
}  

<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
    <a href="#" class="navbar-brand">Academind</a>
    <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse align-self-end" id="navbarMenu">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a href="#" class="nav-link">Users</a>
            </li>
            <li class="nav-item">
                <a href="#" class="nav-link">Products</a>
            </li>
        </ul>
        <ul class="navbar-nav ml-auto">
            <li class="nav-item ">
                <a href="#" class="nav-link">Login</a>
            </li>
            <li class="nav-item">
                <a href="#" class="nav-link">Log out</a>
            </li>
        </ul>
    </div>
</nav>

https://www.codeply.com/go/07ibCvkQJi

选项2 :(删除多余的CSS)

只需将flex-column应用于navbar,然后将品牌和切换器包装在一个flexbox div中。使用自动保证金工具将品牌居中。

<nav class="navbar navbar-expand-sm navbar-dark bg-dark flex-column align-items-stretch">
    <div class="d-flex">
        <a href="#" class="navbar-brand mx-sm-auto mr-auto">Academind</a>
        <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarMenu">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>
    <div class="collapse navbar-collapse w-100" id="navbarMenu">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a href="#" class="nav-link">Users</a>
            </li>
            <li class="nav-item">
                <a href="#" class="nav-link">Products</a>
            </li>
        </ul>
        <ul class="navbar-nav ml-auto">
            <li class="nav-item">
                <a href="#" class="nav-link">Login</a>
            </li>
            <li class="nav-item">
                <a href="#" class="nav-link">Logout</a>
            </li>
        </ul>
    </div>
</nav>

https://www.codeply.com/go/qVJPhAy6fS


相关: How can I have Brand and Navbar on separate lines?

答案 1 :(得分:1)

您可以使用bootstrap4类d-flex,而无需任何额外的CSS:

<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
    <div class="w-100 d-flex flex-column">

    <div class="w-100 d-flex justify-content-center align-items-center">
        <a href="#" class="navbar-brand">Academind</a>
        <button class="navbar-toggler ml-auto" data-toggle="collapse" data-target="#navbarMenu">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>

    <div class="collapse navbar-collapse" id="navbarMenu">

        <ul class="navbar-nav mr-auto">
            <li class="nav-item ">
                <a href="#" class="nav-link">Users</a>
            </li>
            <li class="nav-item">
                <a href="#" class="nav-link">Products</a>
            </li>
        </ul>

        <ul class="navbar-nav  ml-auto">
            <li class="nav-item ">
                <a href="#" class="nav-link">Login</a>
            </li>
            <li class="nav-item">
                <a href="#" class="nav-link">Logout</a>
            </li>
        </ul>

    </div>
</div></nav>

演示:https://jsfiddle.net/PouyaAk/1m2pLn9j/18/