Bootstrap 4 align navbar items to the right

后端 未结 21 2587
無奈伤痛
無奈伤痛 2020-11-21 06:14

How do I align a navbar item to right?

I want to have the login and register to the right. But everything I try does not seem to work.

This is wha

相关标签:
21条回答
  • 2020-11-21 06:58

    Use this code to move items to the right.

    <div class="collapse navbar-collapse justify-content-end">
    
    0 讨论(0)
  • 2020-11-21 07:02

    On Bootsrap 4.0.0-beta.2, none of the answers listed here worked for me. Finally, the Bootstrap site gave me the solution, not via its doc but via its page source code...

    Getbootstrap.com align their right navbar-nav to the right with the help of the following class: ml-md-auto.

    0 讨论(0)
  • 2020-11-21 07:02

    If all above fails, I added 100% width to the navbar class in CSS. Until then mr auto wasn't working for me on this project using 4.1.

    0 讨论(0)
  • 2020-11-21 07:04

    Just add mr-auto class at ul:

    <ul class="nav navbar-nav mr-auto">
    

    If you have menu list in both side you can do something like this:

    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <ul class="navbar-nav ml-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">left 1</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">left 2</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">left disable</a>
      </li>
    </ul>
    
    0 讨论(0)
  • 2020-11-21 07:04

    It's little change in boostrap 4. To align navbar to right side, you've to make only two changes. they are:

    1. in navbar-nav class add w-100 as navbar-nav w-100 to make width as 100
    2. in nav-item dropdown class add ml-auto as nav-item dropdown ml-auto to make margin left as auto.

    If you didn't understand, please refer the image that i've attached to this.

    CodePen Link

    Full source code

    <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="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNavDropdown">
        <ul class="navbar-nav w-100">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Features</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Pricing</a>
          </li>
          <li class="nav-item dropdown ml-auto">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Dropdown link
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              <a class="dropdown-item" href="#">Action</a>
              <a class="dropdown-item" href="#">Another action</a>
              <a class="dropdown-item" href="#">Something else here</a>
            </div>
          </li>
        </ul>  
      </div>
    </nav>
    
    0 讨论(0)
  • 2020-11-21 07:04

    Using the bootstrap flex box helps us to control the placement and alignment of your navigation element. for the problem above adding mr-auto is a better solution to it .

    <div id="app" class="container">
      <nav class="navbar navbar-toggleable-md navbar-light bg-faded">
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <a class="navbar-brand" href="#">Navbar</a>
        <ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Features</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Pricingg</a>
          </li>
        </ul>
        <ul class="navbar-nav  " >
          <li  class="nav-item">
            <a class="nav-link" href="{{ url('/login') }}">Login</a>
          </li>
          <li  class="nav-item">
            <a class="nav-link" href="{{ url('/register') }}">Register</a>
          </li>
        </ul>
      </nav>
      @yield('content')
    </div>
    

    other placement may include

    fixed- top
        fixed bottom
        sticky-top
    
    0 讨论(0)
提交回复
热议问题