Bootstrap5 Navbar
The navbar is typically placed at the top of the page.
We can use the .navbar
class to create a standard navbar, followed by the .navbar-expand-xxl|xl|lg|md|sm
class to create a responsive navbar (expands horizontally on large screens and stacks vertically on small screens).
Options in the navbar can be created using the <ul>
element with the class="navbar-nav"
class. Then, add the .nav-item
class on the <li>
element and use the .nav-link
class on the <a>
element:
Example
<!-- Horizontal navbar switches to vertical on small screens -->
<nav class="navbar navbar-expand-sm bg-light">
<!-- Links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Link 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 3</a>
</li>
</ul>
</nav>
Vertical Navbar
Create a vertical navbar by removing the .navbar-expand-xxl|xl|lg|md|sm
class:
Example
<!-- Vertical navbar -->
<nav class="navbar bg-light">
<!-- Links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Link 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 3</a>
</li>
</ul>
</nav>
Centered Navbar
Create a centered navbar by adding the .justify-content-center
class:
Example
<nav class="navbar navbar-expand-sm bg-light justify-content-center">
...
</nav>
Navbar with Different Colors
Use the following classes to create navbars with different colors: .bg-primary
, .bg-success
, .bg-info
, .bg-warning
, .bg-danger
, .bg-secondary
, .bg-dark
, and .bg-light
.
Tip: For dark background .navbar-dark
, set the text color to light, and for light background .navbar-light
, set the text color to dark.
Example
<!-- Gray background with black text -->
<nav class="navbar navbar-expand-sm bg-light navbar-light">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Active</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</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>
</nav>
<!-- Black background with white text -->
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">...</nav>
<!-- Blue background with white text -->
<nav class="navbar navbar-expand-sm bg-primary navbar-dark">...</nav>
Active and Disabled States: You can add the .active
class to the <a>
element to highlight the selected option. The .disabled
class is used to make the link unclickable.
Brand/Logo
The .navbar-brand
class is used to highlight the brand/logo:
Example
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<a class="navbar-brand" href="#">Logo</a>
...
</nav>
You can use the .navbar-brand
class to make images fit the navbar.
Example
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<a class="navbar-brand" href="#">
<img decoding="async" src="bird.jpg" alt="Logo" style="width:40px;">
</a>
...
</nav>
Collapsing the Navbar
Usually, on small screens, we collapse the navbar and display the navigation options upon clicking.
To create a collapsible navbar, add class="navbar-toggler"
, data-bs-toggle="collapse"
, and data-target="#thetarget"
to the button. Then wrap the navigation content (links) in a div with class="collapse navbar-collapse"
, and ensure the div's id matches the id specified in the button's data-target
:
Example
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
<!-- Brand -->
<a class="navbar-brand" href="#">Navbar</a>
<!-- Toggler/collapsible Button -->
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<!-- Navbar links -->
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</div>
</nav>
Using Dropdowns in the Navbar
You can set up dropdown menus in the navbar:
Example
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<!-- Brand -->
<a class="navbar-brand" href="#">Logo</a>
<!-- Links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Link 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 2</a>
</li>
<!-- Dropdown -->
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-bs-toggle="dropdown">
Dropdown link
</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Link 1</a>
<a class="dropdown-item" href="#">Link 2</a>
<a class="dropdown-item" href="#">Link 3</a>
</div>
</li>
</ul>
</nav>
Navigation Bar Forms and Buttons
Navigation bar forms <form>
elements use the class="form-inline"
class to layout input fields and buttons:
Example
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<form class="form-inline">
<input class="form-control" type="text" placeholder="Search">
<button class="btn btn-success" type="submit">Search</button>
</form>
</nav>
You can also use other input classes, such as the .input-group-addon
class to add small labels before the input field.
Example
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<form class="form-inline" action="/action_page.php">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">@</span>
</div>
<input type="text" class="form-control" placeholder="Username">
</div>
</form>
</nav>
Navigation Bar Text
Use the .navbar-text
class to set non-link text on the navigation bar, ensuring horizontal alignment, color, and padding.
Example
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<!-- Links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Link 1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 2</a>
</li>
</ul>
<!-- Navbar text-->
<span class="navbar-text">
Navbar text
</span>
</nav>
Fixed Navigation Bar
The navigation bar can be fixed at the top or bottom.
We use the .fixed-top
class to fix the navigation bar at the top:
Example
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
...
</nav>
The .fixed-bottom
class is used to fix the navigation bar at the bottom:
Example
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-bottom">
...
</nav>
Navigation Bar Example
Example
```