← Learning Hub
Beginner

Navbar

A navbar is the menu bar at the top of a site. On phones it folds into a button (the "hamburger"), which needs Bootstrap's JavaScript.

The navbar is usually the first thing visitors use to find their way around. A good navbar shows the brand, the main sections and maybe a search box or account link. On phones there isn't room for all the links, so they collapse behind a menu button - Bootstrap handles this automatically with a few classes and attributes.

Example
<!DOCTYPE html>
<html lang="en">
  <head>
    <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 class="p-3">
    <nav class="navbar navbar-expand-md bg-dark" data-bs-theme="dark">
      <div class="container-fluid">
        <a class="navbar-brand" href="#">MyShop</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#menu"
                aria-controls="menu" aria-expanded="false" aria-label="Open menu">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="menu">
          <ul class="navbar-nav ms-auto">
            <li class="nav-item"><a class="nav-link active" href="#">Home</a></li>
            <li class="nav-item"><a class="nav-link" href="#">Deals</a></li>
            <li class="nav-item"><a class="nav-link" href="#">Cart</a></li>
          </ul>
        </div>
      </div>
    </nav>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  </body>
</html>
▶ Try it yourself
  • navbar-expand-md: show the full menu from medium screens; fold it on phones.
  • data-bs-target="#menu" must match the id of the part that opens.
  • ms-auto pushes the links to the right.
On the Try-it page, make the result narrow (or use a phone) to see the menu fold into a button.
Key points
  • navbar builds a responsive top menu.
  • navbar-expand-* decides when it collapses.
  • The collapse button needs Bootstrap's JavaScript.

📝 Test yourself

1. Why does the folding navbar need Bootstrap's JavaScript?
2. What does navbar-expand-md mean?
3. Fill in the class for the site name in a navbar:
<a class="">MyShop</a>