在Bootstrap中,创建Logo a在左边、菜单项在中间、Logo B在右边的导航栏的最平台友好的方法是什么?

这是我迄今为止尝试过的,它最终被对齐,使标志A在左边,菜单项旁边的标志在左边,标志B在右边。

<div class="navbar navbar-fixed-top navbar-custom ">
  <div class="container" >
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#"><span class="navbar-logo">Logo_A</span></a>
    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#about">Menu Item 1</a></li>
        <li><a href="#contact">Menu Item 2</a></li>
        <li><a href="#about">Menu Item 3</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#"><img src="images/Logo_B.png" class="img-responsive"></a></li>
      </ul>
    </div>
  </div>
</div>

当前回答

在Zim的回答中,例子5的左边和中间只有空白的右边并不是真正的中心navlinks。我们可以用一些改变来修复它:

<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" /> <p>Zim's original code</p> <!-- example 5 left and center only with empty space right --> <nav class="navbar navbar-expand-sm navbar-dark bg-primary flex-sm-nowrap flex-wrap"> <div class="container-fluid"> <button class="navbar-toggler flex-grow-sm-1 flex-grow-0 me-2" type="button" data-bs-toggle="collapse" data-bs-target="#navbar5"> <span class="navbar-toggler-icon"></span> </button> <span class="navbar-brand flex-grow-1">Navbar 5</span> <div class="navbar-collapse collapse flex-grow-1 justify-content-center" id="navbar5"> <ul class="navbar-nav mx-auto"> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Codeply</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> </ul> </div> <div class="flex-grow-1"> <!--spacer--> </div> </div> </nav> <div class="centerMarker">mid</div> <br><br><br> <p>Modified solution</p> <!-- example 5 left and center only --> <nav class="navbar navbar-expand-sm navbar-dark bg-primary flex-sm-nowrap flex-wrap"> <!-- 2.5rem is height of the toggler --> <div class="container-fluid" style="height:2.5rem"> <span class="navbar-brand position-absolute"> <button class="navbar-toggler me-2" type="button" data-bs-toggle="collapse" data-bs-target="#navbar5"> <span class="navbar-toggler-icon"></span> </button> Navbar 5 </span> <div class="navbar-collapse collapse mx-auto w-100" id="navbar5"> <ul class="navbar-nav mx-auto"> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Codeply</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> </ul> </div> </div> </nav> <div class="centerMarker">mid</div> <style> body { font-family: monospace; padding: 10px } .centerMarker { margin: 0 auto; width: fit-content; background-color: wheat; } </style> Switch to full page and resize browser to see navlinks.

Bootstrap不允许我们设置flex-basis:0,所以属性仍然是自动的,间隔器不会相等地展开。所以导航链接不是居中。 我们可以在brand container上使用class position-absolute,在navlinks上使用mx-auto w-100。切换器将导航条的最小高度设置为2.5rem。我们必须设置为流体div容器。

其他回答

引导4(截至alpha 6)

导航条是用flexbox构建的!你需要的不是浮动,而是flexbox和保证金效用。

对于右对齐,在折叠div上使用justify-content-end:

<div class="collapse navbar-collapse justify-content-end">
  <ul class="navbar-nav">
    <li class="nav-item active">
      <a class="nav-link" href="#">Home</a>
    </li>
  </ul>
</div>

完整的例子在这里:https://jsbin.com/kemawa/edit?output

引导4

我们有很多方法来对齐导航栏项目。

左对齐

Class = "navbar-nav mr-auto"

右对齐

Class = "navbar-nav ml-auto"

对于中心对齐

Class = "navbar-nav mx-auto"

<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
    <a routerLink="/" class="navbar-brand" href="#">Bootstrap 4</a>
    <ul class="navbar-nav ml-auto">
        <li class="nav-item">
            <a class="nav-link" href="home">Home</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="about">About</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="contact">Contact us</a>
        </li>
    </ul>
</nav>

在Zim的回答中,例子5的左边和中间只有空白的右边并不是真正的中心navlinks。我们可以用一些改变来修复它:

<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" /> <p>Zim's original code</p> <!-- example 5 left and center only with empty space right --> <nav class="navbar navbar-expand-sm navbar-dark bg-primary flex-sm-nowrap flex-wrap"> <div class="container-fluid"> <button class="navbar-toggler flex-grow-sm-1 flex-grow-0 me-2" type="button" data-bs-toggle="collapse" data-bs-target="#navbar5"> <span class="navbar-toggler-icon"></span> </button> <span class="navbar-brand flex-grow-1">Navbar 5</span> <div class="navbar-collapse collapse flex-grow-1 justify-content-center" id="navbar5"> <ul class="navbar-nav mx-auto"> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Codeply</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> </ul> </div> <div class="flex-grow-1"> <!--spacer--> </div> </div> </nav> <div class="centerMarker">mid</div> <br><br><br> <p>Modified solution</p> <!-- example 5 left and center only --> <nav class="navbar navbar-expand-sm navbar-dark bg-primary flex-sm-nowrap flex-wrap"> <!-- 2.5rem is height of the toggler --> <div class="container-fluid" style="height:2.5rem"> <span class="navbar-brand position-absolute"> <button class="navbar-toggler me-2" type="button" data-bs-toggle="collapse" data-bs-target="#navbar5"> <span class="navbar-toggler-icon"></span> </button> Navbar 5 </span> <div class="navbar-collapse collapse mx-auto w-100" id="navbar5"> <ul class="navbar-nav mx-auto"> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Codeply</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Link</a> </li> </ul> </div> </div> </nav> <div class="centerMarker">mid</div> <style> body { font-family: monospace; padding: 10px } .centerMarker { margin: 0 auto; width: fit-content; background-color: wheat; } </style> Switch to full page and resize browser to see navlinks.

Bootstrap不允许我们设置flex-basis:0,所以属性仍然是自动的,间隔器不会相等地展开。所以导航链接不是居中。 我们可以在brand container上使用class position-absolute,在navlinks上使用mx-auto w-100。切换器将导航条的最小高度设置为2.5rem。我们必须设置为流体div容器。

我发现以下是一个更好的解决方案,取决于你的左,中,右项目的内容。宽度为100%而没有边距会导致div重叠,并且会阻止锚标记正确工作——这是在没有混乱使用z索引的情况下。

.navbar-brand
{
    position: absolute;
    width: 100%;
    left: 0;
    margin: auto;
    margin-left: 48%;
}

最简单的解决方案:

只需将导航栏划分为列:例如,如果你总共有24列,12列将是空的,12列将控制导航栏:

<nav class="navbar navbar-default">
    <div class="row">
       <div class="col-sm-4"></div>
       <div class="col-sm-4"></div>
       <div class="col-sm-4"></div>

       <div class="col-sm-12">
           <ul class="nav navbar-nav" align="center">
              <li><a href="#">Home</a></li>
              <li><a href="#">First Link</a></li>
              <li><a href="#">Second Link</a></li>
              <li><a href="#">Third Link</a></li>
              <li><a href="#">Fourth Link</a></li>
           </ul>
       </div>
    </div>
</nav>