Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 58 ยท Components

The Navbar

Build a responsive Bootstrap 5 navbar with navbar-expand-lg, brand, collapse toggler, forms, sticky and fixed positions, color modes and an offcanvas menu.

The navbar is the most recognizable Bootstrap component: a horizontal bar with a brand on the left, links in the middle, and maybe a search box on the right. On a phone the links fold into a hamburger button. Getting that responsive behavior right by hand takes real effort, and Bootstrap gives it to you with a few classes and one data attribute.

You will need the pieces from Navs and Tabs, because a navbar is built from nav-link items, and the Bootstrap script for the toggler (see Installing Bootstrap). Every example below includes both.

Anatomy of a navbar

A navbar has a wrapper, an inner container, and up to four kinds of content:

  • navbar on the outer element (use <nav>), plus navbar-expand-* to say when it becomes horizontal.
  • A container (or container-fluid) inside, to control its width. See Containers.
  • navbar-brand for the name or logo.
  • navbar-toggler (the hamburger button) and a collapse navbar-collapse region holding the links.
  • Optional navbar-text, forms and dropdowns.

The class navbar-expand-lg is the key to responsiveness. Below the lg breakpoint the links are hidden inside the collapse and the toggler shows. From lg upward the toggler disappears and the links lay out in a row. You can use any breakpoint (sm, md, lg, xl, xxl), or plain navbar-expand to never collapse. This is the same mobile-first logic from Breakpoints and Responsive Design.

flowchart TD
  NB["nav.navbar.navbar-expand-lg"] --> C["div.container"]
  C --> B["a.navbar-brand"]
  C --> T["button.navbar-toggler"]
  C --> CO["div.collapse.navbar-collapse"]
  CO --> U["ul.navbar-nav > li.nav-item > a.nav-link"]
  CO --> F["form (optional)"]

The toggler's data-bs-target points at the collapse region's id.

A responsive navbar

Look at three details. me-auto on the navbar-nav pushes everything after it to the right (auto margins, from the flex lesson). The toggler has an aria-label because it contains only an icon. And the id in data-bs-target matches the id of the collapse region exactly; a mismatch is the most common reason a toggler "does nothing".

Brand, text and images

navbar-brand can wrap plain text, an image or both. For a logo, set a fixed size on the image, for example width="30" height="30", and use utilities such as d-inline-block align-text-top me-2. Use an <a> when the brand links home, or a <span> with the class when it should not be clickable.

navbar-text is for non-link text that has to line up with the links, like a username or version number.

Forms and buttons in a navbar

A search form sits fine inside navbar-collapse. Add d-flex to put the input and button on one line, and role="search" to the form so assistive technology can find it.

HTML
<form class="d-flex" role="search">
  <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
  <button class="btn btn-outline-success" type="submit">Search</button>
</form>

If you place the form outside the collapse, you can keep it visible on small screens, but watch the width: a long input beside the brand and toggler will wrap awkwardly on a phone.

Color schemes

In Bootstrap 5.3 the navbar's color follows the color mode system. Instead of the old navbar-light and navbar-dark classes, put data-bs-theme="dark" on the navbar element and the text, toggler icon and hover colors switch to light-on-dark. Then choose any background class: bg-dark, bg-primary, or a custom color.

HTML
<nav class="navbar navbar-expand-lg bg-dark" data-bs-theme="dark">

Remember to pair the theme with a background that actually is dark. data-bs-theme="dark" on a white navbar gives you unreadable white text.

Dark navbar with a search form and a sticky position

Position: fixed and sticky

  • fixed-top and fixed-bottom pin the navbar to the viewport edge and take it out of the document flow. The page content then slides underneath it, so add top padding (about the navbar height, often padding-top: 4.5rem on body) to avoid hiding content.
  • sticky-top keeps the navbar in the normal flow and only sticks once you scroll past it. It needs no padding fix, so prefer it when you can.
  • The default is a normal static bar.

Because a fixed bar covers part of the page, in-page anchors can scroll behind it. Add scroll-margin-top to targets, or scroll-padding-top on html.

Offcanvas navbar

Instead of expanding the links down the page on a phone, many sites slide a panel in from the side. Bootstrap's offcanvas does this: replace collapse navbar-collapse with offcanvas offcanvas-end and point the toggler at it with data-bs-toggle="offcanvas". Inside, a header with a title and a close button precedes the offcanvas-body that holds the links. From the expand breakpoint upward, the offcanvas renders as an ordinary inline navbar, with no sliding.

An offcanvas navbar

The offcanvas locks page scrolling, traps focus while open, and closes with Escape or a click on the backdrop. You will meet it again as a standalone component in Carousel, Offcanvas and Toasts.

Keep the number of top-level links small (five to seven). Group the rest in a dropdown, which you will learn in Dropdowns, Tooltips and Popovers.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does navbar-expand-lg do?

    The expand class sets the breakpoint where the navbar stops collapsing. Below it the toggler appears and the links hide.
  2. 2. A toggler button does nothing when clicked. What is the most likely cause?

    The toggler finds its target through the selector in data-bs-target, and it needs Bootstrap's JavaScript to act.
  3. 3. How do you give a navbar light text on a dark background in Bootstrap 5.3?

    5.3 uses color modes. The old navbar-light and navbar-dark classes are deprecated.
  4. 4. What is the main difference between fixed-top and sticky-top?

    Fixed elements are taken out of the document flow and overlap content, while sticky elements keep their space until scrolling reaches them.
  5. 5. Why does the icon-only toggler need an aria-label?

    An accessible name is required for any control without text content.

Summary

  • A navbar is nav.navbar plus navbar-expand-*, a brand, a toggler and a collapse navbar-collapse region of navbar-nav links.
  • The toggler's data-bs-target must match the collapse id, and the Bootstrap bundle must be loaded.
  • Use me-auto to push content apart, d-flex for forms and role="search" on search forms.
  • Color schemes use data-bs-theme plus a background class; positions are fixed-top, fixed-bottom and sticky-top.
  • Swap the collapse for an offcanvas to get a sliding menu on small screens.

Next lesson: Cards.