mc0de
mC0de / Bootstrap Elements / Lesson 02 of 07

Navigation Bar

Give your website direction. Learn to build a polished, responsive Bootstrap navigation bar with branding, links, active states, and a mobile-friendly menu.

Beginner Friendly Bootstrap 5.3 Responsive Design Live Sandbox
navbar.html
<nav class="navbar navbar-expand-lg">
  <a class="navbar-brand">
    MyWebsite
  </a>
  ...
</nav>
MyWebsite Home About Contact
01 / Getting Started

What Is a Navigation Bar?

A navigation bar, often called a navbar, helps visitors find their way around a website. It commonly appears at the top of a page and contains the website’s name, logo, and important links.

Bootstrap provides a ready-made navbar component that adapts to different screen sizes. On large screens, links can appear side by side. On smaller screens, the same links can collapse behind a menu button.

MyWebsite Home About Services Contact
Good to know: The navigation bar is built with HTML, styled by Bootstrap CSS, and uses Bootstrap JavaScript for its collapsible mobile menu.
02 / The Foundation

Create Your First Navbar

Bootstrap uses the navbar class as the foundation. Add a background class and a container to give the navigation bar its structure.

<nav class="navbar bg-body-tertiary">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">
      MyWebsite
    </a>
  </div>
</nav>
navbar Activates Bootstrap’s navigation bar styling.
navbar-brand Styles the website name or brand link.
container-fluid Allows the navbar content to span the available width.
bg-body-tertiary Applies a subtle background using Bootstrap’s theme colors.
03 / Navigation Links

Add Menu Items

A useful navbar needs links. Bootstrap uses navbar-nav for the link collection, nav-item for each item, and nav-link for the actual links.

<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="#about">About</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#contact">Contact</a>
  </li>
</ul>

The active class highlights the current page. The aria-current="page" attribute communicates the current page to assistive technology.

Accessibility tip: Use meaningful link text and real destinations. In our Sandbox, links point to example sections on the preview page.
04 / Responsive Design

Make Your Navbar Mobile-Friendly

One of Bootstrap’s most useful features is its responsive navigation. The class navbar-expand-lg means the menu expands horizontally at Bootstrap’s large breakpoint (992px and above).

Below that width, you can display a hamburger button that opens and closes the navigation links.

<nav class="navbar navbar-expand-lg bg-body-tertiary">
  <div class="container-fluid">

    <a class="navbar-brand" href="#">MyWebsite</a>

    <button class="navbar-toggler" type="button"
      data-bs-toggle="collapse"
      data-bs-target="#mainMenu"
      aria-controls="mainMenu"
      aria-expanded="false"
      aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="mainMenu">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <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="#about">About</a>
        </li>
      </ul>
    </div>

  </div>
</nav>
navbar-expand-lg Shows the expanded navigation at large screen sizes.
navbar-toggler Styles the button that opens the mobile menu.
data-bs-toggle="collapse" Tells Bootstrap to toggle a collapsible element.
navbar-collapse Groups the navigation links inside the collapsible area.
Important: The button’s data-bs-target="#mainMenu" must match the collapse element’s id="mainMenu". The Bootstrap JavaScript bundle is also required for the menu to open and close.
05 / Appearance

Customize Navbar Colors

Bootstrap includes background utilities and color modes that make navbar customization easier. For example, you can create a dark navigation bar with light-colored text.

<nav class="navbar navbar-expand-lg bg-dark"
     data-bs-theme="dark">
  <!-- Your navbar content -->
</nav>
MyWebsite Home About Contact

You can also experiment with bg-primary, bg-light, or bg-body-tertiary. Use data-bs-theme="dark" when you want Bootstrap’s navbar colors and toggle icon to suit a dark background.

06 / Layout Utilities

Align Your Navigation Links

Bootstrap spacing utilities help control where navbar content appears.

<ul class="navbar-nav ms-auto">
  <!-- Navigation items -->
</ul>

The ms-auto class applies automatic margin at the start side, pushing the menu toward the end of the navbar in a standard left-to-right layout.

You can also use me-auto to push following content away, and gap-2 to create spacing between elements.

Your Turn / Practice Time

🚀 mC0de Challenge

Build a Responsive Website Header

Imagine you’re creating a website for a small creative studio. Your task is to build a professional navigation bar that works on desktop and mobile.

Your mission: Complete all five tasks below.

The checklist is for self-review. Use the Sandbox to experiment, and load the sample solution when you’re ready.

Interactive Coding Workspace

mC0de Bootstrap Sandbox

Edit your navbar, run the code, and switch between desktop and mobile previews.

HTML Input / navbar.html
Live Result / Bootstrap 5.3
Ready to code. Switch to Mobile Preview to test the hamburger menu.
Lesson 02 Recap

What You’ve Learned

  • The navbar class provides Bootstrap's navigation foundation.
  • navbar-brand styles the website's brand or name.
  • navbar-nav, nav-item, and nav-link organize navigation links.
  • The active class and aria-current="page" identify the current page.
  • navbar-expand-lg controls the responsive breakpoint.
  • navbar-toggler and collapse navbar-collapse create a mobile menu.
  • Bootstrap's JavaScript bundle powers the hamburger toggle.
  • Background, color mode, and spacing utilities help customize navbar appearance.

Up next: We'll learn how to create interactive Bootstrap Dropdown menus.

Your Learning Journey

Your Learning Journey

0 of 7 completed
Lesson 02 / Navigation Bar 0% Complete

Nice Work! 🎉

You've learned how to build a responsive Bootstrap navigation bar with links, branding, and a working mobile menu. Ready to record your progress?

Lesson 02 of 07 · Bootstrap Elements