Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Navbar

A navbar is the site header with a logo and a menu. Its main skill is responsiveness: on a wide screen the links stand in a row, and on a phone they hide behind a "hamburger" button (three lines). This collapsing needs Bootstrap's JavaScript.

A simple navbar

The minimal navbar is a <nav> tag with the navbar and navbar-expand classes, a navbar-brand logo and a navbar-nav list of links:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
 
<nav class="navbar navbar-expand bg-body-tertiary">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Tasko</a>
    <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="#">Features</a></li>
      <li class="nav-item"><a class="nav-link" href="#">Pricing</a></li>
    </ul>
  </div>
</nav>

A gray bar: a bold name on the left, three links next to it, the current one darker than the rest. navbar-expand without a breakpoint means "always expanded".

Collapsing on phones

For a responsive navbar replace navbar-expand with navbar-expand-lg and add two parts: a navbar-toggler button and a collapse navbar-collapse wrapper around the links. The button's data-bs-target attribute points to the wrapper's id:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
 
<nav class="navbar navbar-expand-lg bg-body-tertiary">
  <div class="container">
    <a class="navbar-brand" href="#">Tasko</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#mainMenu"
            aria-controls="mainMenu" aria-expanded="false" aria-label="Open menu">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="mainMenu">
      <ul class="navbar-nav me-auto">
        <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>
      <a class="btn btn-primary" href="#">Sign in</a>
    </div>
  </div>
</nav>
 
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>

The preview is narrower than 992 pixels, so a button with three lines is shown on the right. Click it and the links and the "Sign in" button drop down in a column. On a wide screen everything is on one line, and me-auto pushes "Sign in" to the right.

A dark navbar

The data-bs-theme="dark" attribute switches the navbar colors to light text, and bg-dark gives a dark background:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
 
<nav class="navbar navbar-expand bg-dark" data-bs-theme="dark">
  <div class="container-fluid">
    <span class="navbar-brand">Tasko Admin</span>
    <span class="navbar-text">Anna Smith</span>
  </div>
</nav>

This is the admin dashboard header: a dark bar, the name on the left and the user's name on the right, styled with navbar-text.

Where the navbar goes

The navbar is the first element in <body>. To keep it visible while scrolling, add sticky-top to it.

Common mistake

The hamburger button does not open the menu. There are almost always two reasons: bootstrap.bundle.min.js is not loaded, or data-bs-target="#mainMenu" does not match the wrapper's id (a missing hash sign, a typo in the name). Check both before looking for a bug in CSS.

Thanks for reading!

Donate to the site — we work hard for you.