skip to Main Content

I am trying to put two navbar elements to the end of the navbar in Bootstrap 5 but it’s not working

I’ve tried ms-auto with a div encapsulated around the log in and sign up elements but that didn’t work. I’ve also tried float and text but that didn’t work either.

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

  <title>Hello, world!</title>
</head>

<body>
  <nav class="navbar navbar-dark bg-dark navbar-expand-sm">
    <a class="navbar-brand" href="">Navbar</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarPractice" aria-controls="navbarPractice" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
    <div class="collapse navbar-collapse" id="navbarPractice">
      <div class="navbar-nav">
        <a class="nav-link active" aria-current="page" href="">Home</a>
        <a class="nav-link" href="">About</a>
        <a class="nav-link" href="">Support</a>
        <a class="nav-link" href="">Log In</a>
        <a class="nav-link" href="">Sign Up</a>
      </div>
    </div>
  </nav>

  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>

2

Answers


  1. Try this:

    @media (min-width: 576px) {
      .navbar-nav {
        width: 100%;
      }
      #navbarPractice>div a.login {
        margin-left: auto;
      }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.1/js/bootstrap.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
    <nav class="navbar navbar-dark bg-dark navbar-expand-sm">
      <a class="navbar-brand" href="">Navbar</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarPractice" aria-controls="navbarPractice" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
      <div class="collapse navbar-collapse" id="navbarPractice">
        <div class="navbar-nav">
          <a class="nav-link active" aria-current="page" href="">Home</a>
          <a class="nav-link" href="">About</a>
          <a class="nav-link" href="">Support</a>
          <a class="nav-link login" href="">Log In</a>
          <a class="nav-link" href="">Sign Up</a>
        </div>
      </div>
    </nav>
    </div>
    </div>
    </nav>
    Login or Signup to reply.
  2. Use the w-100 or flex-fill class to stretch the navbar-nav element. Without that you have no space to implement ms-auto, which then works as expected. You’d use ms-sm-auto to only apply margin above the mobile breakpoint.

    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    
    <nav class="navbar navbar-dark bg-dark navbar-expand-sm">
      <a class="navbar-brand" href="">Navbar</a>
      
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarPractice" aria-controls="navbarPractice" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      
      <div class="collapse navbar-collapse" id="navbarPractice">
        <div class="navbar-nav w-100">
          <a class="nav-link active" aria-current="page" href="">Home</a>
          <a class="nav-link" href="">About</a>
          <a class="nav-link" href="">Support</a>
          <a class="nav-link ms-sm-auto" href="">Log In</a>
          <a class="nav-link" href="">Sign Up</a>
        </div>
      </div>
    </nav>
    
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search