← Back to Bootstrap Course | Chapter 6: Components — Navigation | Lesson 1 of 7

Navbar basics

The navbar is the responsive header bar that holds a brand, links and other navigation.

In this page:

  1. Navbar basics
Syntax
markup
<nav class="navbar navbar-expand-lg bg-body-tertiary">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Brand</a>
    <div class="navbar-nav">
      <a class="nav-link" href="#">Link</a>
    </div>
  </div>
</nav>

Navbar basics

Create a nav with .navbar and .navbar-expand-lg to expand at lg, add a colour scheme with data-bs-theme="dark" and a background class. Inside, put .navbar-brand, a .navbar-toggler button and a .collapse.navbar-collapse containing .navbar-nav links.

The toggler needs the Bootstrap JavaScript.

Note: navbar-expand-{breakpoint} controls where the links stop collapsing.

Example: Navbar basics

markup
<!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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body class="p-3">
<nav class="navbar navbar-expand-lg bg-dark" data-bs-theme="dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Site</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#nav" aria-controls="nav" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
    <div class="collapse navbar-collapse" id="nav">
      <ul class="navbar-nav me-auto"><li class="nav-item"><a class="nav-link active" href="#">Home</a></li><li class="nav-item"><a class="nav-link" href="#">Pricing</a></li></ul>
    </div>
  </div>
</nav>
</body>
</html>
Live Example
Related Topics
Common Mistakes
  1. Forgetting the JS bundle for the toggler
  2. Missing data-bs-target ids
  3. Not choosing an expand breakpoint
Chapter Summary
  • .navbar with navbar-expand-*
  • Brand, toggler and collapse
  • navbar-nav holds links
  • Needs the JS bundle for toggling
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.