Bootstrap NavBar with left, center or right aligned items

前端 未结 10 1202
旧巷少年郎
旧巷少年郎 2020-11-21 11:24

In Bootstrap, what is the most platform-friendly way to create a navigation bar that has Logo A on the left, menu items in the center, and Logo B on the rig

相关标签:
10条回答
  • 2020-11-21 11:40

    I found the following was a better solution depending on the content of your left, center and right items. A width of 100% without a margin caused overlapping of divs and was preventing anchor tags to work correctly - that is without the messy use of z-indexes.

    .navbar-brand
    {
        position: absolute;
        width: 100%;
        left: 0;
        margin: auto;
        margin-left: 48%;
    }
    
    0 讨论(0)
  • 2020-11-21 11:47

    I needed something similar (left, center and right aligned items), but with ability to mark centered items as active. What worked for me was:

    http://www.bootply.com/CSI2KcCoEM

    <nav class="navbar navbar-default" role="navigation">
      <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>    
      </div>
      <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
          <li class="navbar-left"><a href="#">Left 1</a></li>
          <li class="navbar-left"><a href="#">Left 2</a></li>
          <li class="active"><a href="#">Center 1</a></li>
          <li><a href="#">Center 2</a></li>
          <li><a href="#">Center 3</a></li>
          <li class="navbar-right"><a href="#">Right 1</a></li>
          <li class="navbar-right"><a href="#">Right 2</a></li>
        </ul>
      </div>
    </nav>
    

    CSS:

    @media (min-width: 768px) {
      .navbar-nav {
        width: 100%;
        text-align: center;
      }
      .navbar-nav > li {
        float: none;
        display: inline-block;
      }
      .navbar-nav > li.navbar-right {
        float: right !important;
      }
    }
    
    0 讨论(0)
  • 2020 Update

    Bootstrap 4

    Now that Bootstrap 4 has flexbox, Navbar alignment is much easier. Here are updated examples for left, right and center in the Bootstrap 4 Navbar, and many other alignment scenarios demonstrated here.

    The flexbox, auto-margins, and ordering utility classes can be used to align Navbar content as needed. There are many things to consider including the order and alignment of Navbar items (brand, links, toggler) on both large screens and the mobile/collapsed views. Don't use the grid classes (row,col) for the Navbar.

    Here are various examples...

    Left, center(brand) and right links:

    <nav class="navbar navbar-expand-md navbar-dark bg-dark">
        <div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Left</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="//codeply.com">Codeply</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
            </ul>
        </div>
        <div class="mx-auto order-0">
            <a class="navbar-brand mx-auto" href="#">Navbar 2</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>
        <div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="#">Right</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
            </ul>
        </div>
    </nav>
    

    http://www.codeply.com/go/qhaBrcWp3v


    Another BS4 Navbar option with center links and overlay logo image:

    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="navbar-collapse collapse w-100 dual-collapse2 order-1 order-md-0">
            <ul class="navbar-nav ml-auto text-center">
                <li class="nav-item active">
                    <a class="nav-link" href="#">Link</a>
                </li>
            </ul>
        </div>
        <div class="mx-auto my-2 order-0 order-md-1 position-relative">
            <a class="mx-auto" href="#">
                <img src="//placehold.it/120/ccff00" class="rounded-circle">
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".dual-collapse2">
                <span class="navbar-toggler-icon"></span>
            </button>
        </div>
        <div class="navbar-collapse collapse w-100 dual-collapse2 order-2 order-md-2">
            <ul class="navbar-nav mr-auto text-center">
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
            </ul>
        </div>
    </nav>
    

    Or, these other Bootstrap 4 alignment scenarios:

    brand left, dead center links, (empty right)


    brand and links center, icons left and right


    More Bootstrap 4 examples:

    toggler left on mobile, brand right
    center brand and links on mobile
    right align links on desktop, center links on mobile
    left links & toggler, center brand, search right


    Also see: Bootstrap 4 align navbar items to the right
    Bootstrap 4 navbar right align with button that doesn't collapse on mobile
    Center an element in Bootstrap 4 Navbar


    Bootstrap 3

    Option 1 - Brand center, with left/right nav links:

    <nav class="navbar navbar-default" role="navigation">
      <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>    
      </div>
      <a class="navbar-brand" href="#">Brand</a>
      <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav navbar-left">
            <li><a href="#">Left</a></li>
            <li><a href="#about">Left</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
          <li><a href="#about">Right</a></li>
          <li><a href="#contact">Right</a></li>
        </ul>
      </div>
    </nav>
    
    .navbar-brand
    {
        position: absolute;
        width: 100%;
        left: 0;
        text-align: center;
        margin:0 auto;
    }
    .navbar-toggle {
        z-index:3;
    }
    

    http://bootply.com/98314 (3.x)


    Option 2 - Left, center and right nav links:

    <nav class="navbar navbar-default" role="navigation">
      <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>    
      </div>
      <div class="navbar-collapse collapse">
        <ul class="nav navbar-nav">
            <li><a href="#">Left</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-center">
            <li><a href="#">Center</a></li>
            <li><a href="#">Center</a></li>
          <li><a href="#">Center</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Right</a></li>
        </ul>
      </div>
    </nav>
    
    @media (min-width: 768px) {
      .navbar-nav.navbar-center {
        position: absolute;
        left: 50%;
        transform: translatex(-50%);
      }
    }
    

    http://bootply.com/SGYC6BWeBK

    Option 3 - Center both brand and links

    .navbar .navbar-header,
    .navbar-collapse {
        float:none;
        display:inline-block;
        vertical-align: top;
    }
    
    @media (max-width: 768px) {
        .navbar-collapse  {
            display: block;
        }
    }
    

    http://codeply.com/go/1lrdvNH9GI

    More examples:

    Left brand, center links
    Left toggler, center brand

    For 3.x also see nav-justified: Bootstrap center navbar


    Center Navbar in Bootstrap
    Bootstrap 4 align navbar items to the right

    0 讨论(0)
  • 2020-11-21 11:49

    Bootstrap 4

    We have many ways to align navBars Items.

    For Left Align

    class = "navbar-nav mr-auto"

    For Right Align

    class = "navbar-nav ml-auto"

    For Center Align

    class = "navbar-nav mx-auto"

    <nav class="navbar navbar-expand-sm bg-dark navbar-dark">
    <a routerLink="/" class="navbar-brand" href="#">Bootsrap 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>
    
    0 讨论(0)
  • 2020-11-21 11:52
    <div class="d-flex justify-content-start">hello</div>    
    <div class="d-flex justify-content-end">hello</div>
    <div class="d-flex justify-content-center">hello</div>
    <div class="d-flex justify-content-between">hello</div>
    <div class="d-flex justify-content-around">hello</div>
    

    Put the fields that you want to center, right or left according above division.

    0 讨论(0)
  • 2020-11-21 11:57

    You set the margin left to auto -> ml-auto on the section you want to move to the right.

     <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
      <div class="navbar-nav ml-auto"> !<--MARGIN LEFT AUTO HERE !-->
        <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
        <a class="nav-item nav-link" href="#">Features</a>
        <a class="nav-item nav-link" href="#">Pricing</a>
        <a class="nav-item nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
      </div>
    </div>
    

    0 讨论(0)
提交回复
热议问题