Bootstrap 5 Navbar: Quickly Implement a Responsive Navigation Menu

Quick Implementation of Responsive Navigation Menu (Bootstrap5 Navbar Basic Usage)

1. Include Bootstrap5 Resources

To use Bootstrap5’s navbar, you first need to include Bootstrap’s CSS and JavaScript files in your HTML. The fastest way is via CDN links, no local installation required:

<!-- Bootstrap5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- Bootstrap5 JS (place at the end of body or use defer) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

2. Basic Navbar Structure

The core of Bootstrap5’s navbar is the <nav> tag with the .navbar class, implemented through these key components:

  • Brand Identification: .navbar-brand (usually for logos or site names)
  • Navigation Link Container: .navbar-nav (wraps navigation items)
  • Individual Navigation Item: .nav-item (for <li> tags)
  • Navigation Link: .nav-link (for <a> tags)
  • Mobile Collapse Button: .navbar-toggler (hamburger menu button)
  • Collapsible Content: .collapse.navbar-collapse (collapsible navigation area)

Example Code:

<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
  <!-- Brand Identification -->
  <div class="container-fluid">
    <a class="navbar-brand" href="#">My Website</a>

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

    <!-- Collapsible Navigation Content -->
    <div class="collapse navbar-collapse" id="navbarContent">
      <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="#">Products</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="dropdownMenu" 
             role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Services
          </a>
          <ul class="dropdown-menu" aria-labelledby="dropdownMenu">
            <li><a class="dropdown-item" href="#">Service 1</a></li>
            <li><a class="dropdown-item" href="#">Service 2</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </div>
</nav>

3. Responsive Collapse Logic

Control navbar display states across different screens using .navbar-expand-* classes:
- .navbar-expand-lg: Expands navigation on screens ≥992px (lg breakpoint), automatically collapses to hamburger menu on smaller screens.
- Breakpoint Explanation: Bootstrap5 breakpoints include sm(576px), md(768px), lg(992px), xl(1200px). Choose the appropriate expansion level based on your needs.

Effect: On small screens, clicking the hamburger button expands navigation links downward from the top; on large screens, all links are displayed directly.

4. Navbar Style Customization

  • Background Color: Set via .bg-* classes (e.g., bg-primary for blue, bg-light for light gray).
  • Text Color: Use .navbar-dark (white text) or .navbar-light (black text) with background colors.
  • Active State: The .active class marks the current page navigation item (automatically highlighted).
  • Dropdown Menu: Click .dropdown-toggle to expand, with default animation effects.

Example:

<!-- Navbar with black text and light gray background -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <!-- Highlighted navigation item -->
  <li class="nav-item">
    <a class="nav-link active" href="#">Home</a> <!-- .active class auto-highlights -->
  </li>
  <!-- Custom background color -->
  <li class="nav-item">
    <a class="nav-link" href="#" style="background-color: #f0f0f0;">Products</a>
  </li>
</nav>

5. Common Extended Features

  • Fixed at Top: Add .fixed-top class to keep navbar fixed at the top during scrolling:
  <nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top">
  • Right-Aligned Content: Add search bars, buttons, etc., using .d-flex for alignment:
  <form class="d-flex">
    <input class="form-control me-2" type="search" placeholder="Search">
    <button class="btn btn-outline-light" type="submit">Search</button>
  </form>
  • Navbar Spacing: Adjust container size with .my-3 (vertical margins) or .px-4 (horizontal margins).

6. Notes

  • CDN Order: Include CSS first, then JS (or place at the end of <body>).
  • Collapse State Validation: Ensure data-bs-target matches the id of the collapsible container.
  • Accessibility Attributes: Add aria-controls, aria-expanded, etc., to improve screen reader compatibility.

With the above content, you’ve mastered the core usage of Bootstrap5’s navbar. Simply copy the example code and replace content to quickly implement a responsive navigation menu without writing CSS from scratch!

Xiaoye