Bootstrap Tutorial menu

Bootstrap Navbar

A navbar is the navigation bar at the top of a website, and Bootstrap's version works on both phones and laptops.

A Simple Navbar

A navbar needs the classes navbar and navbar-expand-*. The breakpoint in navbar-expand-lg says from which width the links are shown in a row.

Example

html
<!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>

<nav class="navbar navbar-expand-lg bg-body-tertiary">
  <div class="container">
    <a class="navbar-brand fw-bold" href="#">Karachi Coders</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 ms-auto">
        <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="#">Courses</a></li>
        <li class="nav-item"><a class="nav-link" href="#">Tutorials</a></li>
        <li class="nav-item"><a class="nav-link" href="#">Contact</a></li>
      </ul>
    </div>
  </div>
</nav>

<div class="container py-4">
  <p>Make the result window narrow. The links hide behind the menu button. Click it to open them.</p>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Try it Yourself »

The Navbar Explained

PartClass or attributeJob
Barnavbar navbar-expand-lgThe navbar; shows links in a row from 992px
Brandnavbar-brandYour logo or site name
Menu buttonnavbar-togglerThe "hamburger" button on small screens
Button targetdata-bs-target="#mainMenu"Which part the button opens and closes
Collapsing areacollapse navbar-collapseHidden on small screens until the button is clicked
Link listnavbar-navThe list of links
Linknav-linkOne link; active marks the current page
Push rightms-autoMoves the links to the right side

The id on the collapsing <div> must match the data-bs-target on the button. The opening and closing is done by Bootstrap's JavaScript, so the bundle script is required.

Add aria-current="page" to the active link and keep the aria-label on the menu button. Screen readers use them to tell blind users where they are.

Colours

Bootstrap 5.3 uses data-bs-theme to switch a navbar between light and dark text. Combine it with a background class:

Example

html
<!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>

<nav class="navbar bg-dark" data-bs-theme="dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Dark Navbar</a>
  </div>
</nav>

<nav class="navbar bg-primary mt-2" data-bs-theme="dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Primary Navbar</a>
    <a class="btn btn-light btn-sm" href="#">Log in</a>
  </div>
</nav>

</body>
</html>
Try it Yourself »

Fixed and Sticky Navbars

ClassEffect
fixed-topAlways at the top of the screen, even when scrolling; it covers content, so add top padding to the body
sticky-topScrolls with the page until it reaches the top, then sticks
fixed-bottomAlways at the bottom of the screen

sticky-top is often the easiest, because it does not cover your content.

Exercise

Exercise

What does the navbar-expand-md class do?

Learn Bootstrap with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp