Signup/Sign In

How to align navbar items in left, center or right?

Bootstrap Navbar provides attractive headers to the website pages. It is used for styling the website's header. Navbar is generally placed at the top of the web page. It is used for navigation between web pages.

The elements in the navbar are aligned to the left of the navbar by default. Here, we are going to learn how to align elements to left, right, and center in the navbar.

Items in the Default navbar

Bootstrap 5 provides a number of classes used to create a navbar as per our requirements.

  • The .navbar class is used within <nav> element to create a navbar.
  • The .navbar is wrapped with navbar-expand-{sm, md, lg, xl, xxl } for responsive collapsing and color schemes classes.
  • Use the .container class to control the width of the navbar.
  • Use .navbar-brand to add a website, product, or company name.
  • Use .navbar-nav for full-height and lightweight navigation.
  • Use .navbar-toggler to add the collapse the navbar <button>.
  • Use .navbar-item to add items to the navigation.
  • Use .navbar-link to add links within .navbar-item.
  • Use .disable to disable any item link.

In the default navbar, the brand name and the items are aligned to the left side within the navbar.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap </title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet"> 
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
</head>
<body>    
	<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
	  <div class="container-fluid">
		<a class="navbar-brand" href="#">Navbar</a>
		<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
		  <span class="navbar-toggler-icon"></span>
		</button>
		<div class="collapse navbar-collapse" id="navbarSupportedContent">
		  <ul class="navbar-nav ">
			<li class="nav-item">
			  <a class="nav-link active" aria-current="page" href="#">Home</a>
			</li>
			<li class="nav-item">
			  <a class="nav-link" href="#">Course</a>
			</li>			
			<li class="nav-item">
			  <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
			</li>
		  </ul>		  
		</div>
	  </div>
	</nav>	
</body>
</html>

Output:

Here is the output of the above program.

navbar

Aligning items to left, right, and center within the Navbar

The items within the navbar can be aligned using margin utilities.

  • The .mx-auto class can be used to align the items to the center of the navbar.
  • The .ms-auto class is used to align items to the right of the navbar.
  • The .me-auto class is used to align items to the left of that navbar.
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap </title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet"> 
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script>
</head>
<body>    
	<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
	  <div class="container-fluid">	    
		<div class="collapse navbar-collapse " id="navbarSupportedContent">
		  <ul class="navbar-nav me-auto order-0 ">
			<li class="nav-item">
			  <a class="nav-link active" aria-current="page" href="#">Home</a>
			</li>
			<li class="nav-item">
			  <a class="nav-link" href="#">Course</a>
			</li>
			<li class="nav-item">
			  <a class="nav-link" href="#">About us</a>
			</li>
			<li class="nav-item">
			  <a class="nav-link" href="#">Contacts</a>
			</li>
			<li class="nav-item">
			  <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
			</li>
		  </ul>
		  <div class="mx-auto">
			<a class="navbar-brand " href="#">Navbar</a>
			<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
			  <span class="navbar-toggler-icon"></span>
			</button>
		</div>
		  <form class="d-flex ms-auto order-5">
          <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
          <button class="btn btn-outline-success" type="submit">Search</button>
          </form>
		</div>
	  </div>
	</nav>
</body>
</html>

Output

Here is the output of the above program where links are aligned to the left, the brand name to the center, and the search field to the right within the navbar.

aligning items in navbar

Conclusion

Hence we can use margin utilities to align the items within the navbar to left, right, or center. You can enhance the look of the navbar by aligning the navbar items at different positions. Bootstrap 5 makes aligning items in the navbar easier.



About the author: