Skip to content

Bootstrap 5 Navbar Right Alignment

Problem

In Bootstrap 5, aligning navbar items to the right side requires different classes than previous Bootstrap versions. Many developers familiar with Bootstrap 3's navbar-right or Bootstrap 4's ml-auto find these methods no longer work in Bootstrap 5 due to significant changes in the framework's class naming convention system.

Solution Overview

Bootstrap 5 introduced RTL (right-to-left) support and logical property naming, replacing directional left and right classes with start and end equivalents. The primary solution is to use ms-auto (margin-start-auto) instead of the deprecated ml-auto (margin-left-auto).

Primary Solution: Using ms-auto

The most straightforward and recommended approach is applying the ms-auto class to your navbar navigation container:

html
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
  <li class="nav-item">
    <a class="nav-link" href="#">Right Link 1</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Right Link 2</a>
  </li>
</ul>

TIP

The mb-2 mb-lg-0 classes control margin-bottom responsiveness and are optional styling choices rather than required for right alignment.

Complete Working Example

Here's a complete Bootstrap 5 navbar with right-aligned navigation items:

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Bootstrap 5 Navbar</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <nav class="navbar navbar-expand-lg bg-primary navbar-dark">
    <div class="container">
      <a class="navbar-brand" href="#">Brand Name</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
        <span class="navbar-toggler-icon"></span>
      </button>
      
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav ms-auto mb-lg-0">
          <li class="nav-item">
            <a class="nav-link active" href="#">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">About</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Services</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Contact</a>
          </li>
        </ul>
      </div>
    </div>
  </nav>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Alternative Approaches

Using justify-content-end on Container

You can apply the justify-content-end class to the collapse container:

html
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
  <ul class="navbar-nav">
    <!-- Your nav items here -->
  </ul>
</div>

Using Custom CSS

For more control, you can use custom CSS with flexbox:

css
#customNavLinks {
  justify-content: flex-end;
}
html
<div class="collapse navbar-collapse" id="customNavLinks">
  <ul class="navbar-nav">
    <!-- Your nav items here -->
  </ul>
</div>

Bootstrap 5 Nomenclature Changes

Bootstrap 5 replaced left/right terminology with start/end for RTL support. Here are the key changes:

Bootstrap 4 ClassBootstrap 5 Equivalent
ml-automs-auto
mr-autome-auto
pl-*ps-*
pr-*pe-*
text-lefttext-start
text-righttext-end
float-leftfloat-start
float-rightfloat-end

Version Note

If you're using Bootstrap 5 alpha, ml-auto might still work, but for beta and stable releases, always use the new ms-auto/me-auto classes.

React Bootstrap Considerations

When using React Bootstrap, you have two options:

jsx
// Option 1: On the container
<Navbar.Collapse className="justify-content-end">
  {/* Nav items */}
</Navbar.Collapse>

// Option 2: On the Nav component
<Navbar.Collapse>
  <Nav className="ms-auto">
    {/* Nav items */}
  </Nav>
</Navbar.Collapse>

Conclusion

For right-aligning navbar items in Bootstrap 5:

  1. Primary method: Use ms-auto on your navbar-nav container
  2. Alternative: Use justify-content-end on the navbar-collapse container
  3. Custom approach: Apply custom CSS with justify-content: flex-end

The ms-auto approach is the most straightforward and follows Bootstrap 5's updated naming conventions, ensuring compatibility with RTL layouts and future framework updates.