Bootstrap 5 Navbar
The navbar is the responsive header bar at the top of most Bootstrap sites: a brand/logo, navigation links and maybe a search box. On wide screens the links sit in a row; on narrow screens they collapse behind a "hamburger" ☰ button.
Navbar building blocks
| Class / attribute | Purpose |
|---|---|
.navbar |
The bar (a flex container) |
.navbar-expand-{bp} |
Show links in a row from breakpoint bp up; collapsed below |
.navbar-brand |
Logo or site name |
.navbar-nav |
List of links (uses .nav-item and .nav-link) |
.navbar-toggler + .navbar-toggler-icon |
Hamburger button |
.collapse.navbar-collapse |
Part that collapses on small screens |
.navbar-text |
Plain text with correct vertical alignment |
data-bs-theme="dark" |
Light text for dark backgrounds (5.3) |
.fixed-top / .fixed-bottom / .sticky-top |
Pin the navbar |
A responsive navbar
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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-sm bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">🅱️ Brand</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#mainNav"
aria-controls="mainNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="mainNav">
<ul class="navbar-nav me-auto">
<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="#">Features</a></li>
<li class="nav-item"><a class="nav-link" href="#">Pricing</a></li>
</ul>
<span class="navbar-text">Hi, Ana</span>
</div>
</div>
</nav>
<div class="container mt-3"><p>Page content…</p></div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>.me-auto on the link list pushes everything after it (the "Hi, Ana" text) to the right edge.
Note: The preview frame is only as wide as the lesson column. This example uses
navbar-expand-sm(576px), so you'll normally see the links in a row. Change it tonavbar-expand-lgand run again: the frame is probably narrower than 992px, so you'll see the ☰ button — click it. Resize your browser window to watch the switch live.
Colour schemes
Pick any background utility. On dark backgrounds, add data-bs-theme="dark" so the links and toggler icon turn light.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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 bg-dark mb-2" data-bs-theme="dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Dark</a>
<div class="navbar-nav">
<a class="nav-link active" href="#">Home</a><a class="nav-link" href="#">About</a>
</div>
</div>
</nav>
<nav class="navbar navbar-expand bg-primary mb-2" data-bs-theme="dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Primary</a>
<div class="navbar-nav">
<a class="nav-link active" href="#">Home</a><a class="nav-link" href="#">About</a>
</div>
</div>
</nav>
<nav class="navbar navbar-expand" style="background-color: #e9dffb;">
<div class="container-fluid">
<a class="navbar-brand" href="#">Custom colour</a>
<div class="navbar-nav">
<a class="nav-link active" href="#">Home</a><a class="nav-link" href="#">About</a>
</div>
</div>
</nav>
</body>
</html>.navbar-expand without a breakpoint means "never collapse".
Dropdowns and a search form
Navbars can contain dropdowns and forms. Use .d-flex on the form to keep the input and button on one line.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body style="min-height: 260px;">
<nav class="navbar navbar-expand-md bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Shop</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#shopNav"
aria-controls="shopNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="shopNav">
<ul class="navbar-nav me-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">Categories</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">👕 Clothes</a></li>
<li><a class="dropdown-item" href="#">👟 Shoes</a></li>
</ul>
</li>
<li class="nav-item"><a class="nav-link" href="#">Deals</a></li>
</ul>
<form class="d-flex" role="search" onsubmit="event.preventDefault(); console.log('search:', this.q.value)">
<input class="form-control form-control-sm me-2" name="q" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-sm btn-outline-success" type="submit">Go</button>
</form>
</div>
</div>
</nav>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>Fixed and sticky navbars
.fixed-top pins the bar to the top of the viewport and takes it out of the normal flow, so add top padding to <body> or the first content will hide underneath. .sticky-top keeps the bar in the flow and only sticks once you scroll past it — usually the easier choice.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<p class="p-3 mb-0 bg-warning-subtle">Scroll down inside the frame ↓</p>
<nav class="navbar sticky-top bg-dark" data-bs-theme="dark">
<div class="container-fluid"><span class="navbar-brand">I stick to the top</span></div>
</nav>
<div class="container py-3">
<p>Line 1</p><p>Line 2</p><p>Line 3</p><p>Line 4</p><p>Line 5</p>
<p>Line 6</p><p>Line 7</p><p>Line 8</p><p>Line 9</p><p>Line 10</p>
</div>
</body>
</html>Tip: Every navbar should sit inside a container (
.containeror.container-fluid) so its contents get proper padding and alignment.
Test yourself
What does navbar-expand-lg mean?
- The navbar is always collapsed
- Links show in a row from 992px up and collapse below
- The navbar is extra large
- Links collapse from 992px up
How do you get light link text on a dark navbar in Bootstrap 5.3?
- .navbar-light
- data-bs-theme="dark" on the navbar
- .text-white on each link
- .navbar-inverse
Which positioning class keeps the navbar in normal flow but sticks it when you scroll past?
- .fixed-top
- .sticky-top
- .position-fixed
- .navbar-static