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
Try this:
Use the
w-100
orflex-fill
class to stretch the navbar-nav element. Without that you have no space to implementms-auto
, which then works as expected. You’d usems-sm-auto
to only apply margin above the mobile breakpoint.