Bootstrap 5 Navigation Components: Implementation of Tabs and Breadcrumb Navigation

To use Bootstrap 5’s navigation components, you first need to include Bootstrap 5’s CSS and JS files in your HTML. The following are detailed implementation steps for tab navigation and breadcrumb navigation, suitable for beginners to get started quickly.

1. Preparation: Include Bootstrap 5 Resources

In the <head> of your HTML file, include Bootstrap 5’s CSS. Before the closing </body> tag, include the JS (with Popper.js) to ensure navigation functionality works properly.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>Bootstrap 5 Navigation Component Example</title>
  <!-- Include Bootstrap 5 CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <!-- Navigation and content will be placed here -->

  <!-- Include Bootstrap 5 JS (with Popper) for interactive features like tab switching -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

2. Tab Navigation (Tabs)

Tab navigation is commonly used to categorize and display content (e.g., switching between product details and user information). It allows users to switch between different content panels by clicking tabs.

2.1 Basic Tab Implementation

<!-- Tab Navigation -->
<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="messages-tab" data-bs-toggle="tab" data-bs-target="#messages" type="button" role="tab" aria-controls="messages" aria-selected="false">Messages</button>
  </li>
</ul>

<!-- Tab Content -->
<div class="tab-content pt-3" id="myTabContent">
  <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
    <h5>Home Content</h5>
    <p>Core information for the home page, such as latest announcements and popular recommendations.</p>
  </div>
  <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
    <h5>Profile</h5>
    <p>User basic information, such as name, avatar, and contact details.</p>
  </div>
  <div class="tab-pane fade" id="messages" role="tabpanel" aria-labelledby="messages-tab">
    <h5>Messages</h5>
    <p>Unread messages, system notifications, and chat records.</p>
  </div>
</div>

2.2 Key Classes & Attributes

  • Navigation Container:
  • .nav: Base class for all navigation components.
  • .nav-tabs: Converts the navigation into tab-style layout (underlined with content switching).
  • role="tablist": Accessibility attribute to mark this as a tab list.

  • Tab Items:

  • .nav-item: Container for individual tab items.
  • .nav-link: Tab button. Requires data-bs-toggle="tab" to trigger switching and data-bs-target="#panelID" to associate with content.
  • aria-controls and aria-labelledby: Accessibility attributes to link tabs with their content.

  • Content Area:

  • .tab-content: Parent container for all tab contents.
  • .tab-pane: Individual tab panel. Must match the id with the tab’s data-bs-target.
  • .fade: Adds fade-in/fade-out transition animation. .show and .active ensure the first tab is visible by default.

2.3 Extension: Pill Navigation

To create horizontal “pill” navigation (rounded tabs), replace .nav-tabs with .nav-pills:

<ul class="nav nav-pills" id="myPillsTab" role="tablist">
  <!-- Tab item code remains the same as above -->
</ul>

3. Breadcrumb Navigation

Breadcrumbs display page hierarchy (e.g., “Home > Product Category > Product Details”) to show the user’s current location intuitively.

3.1 Basic Breadcrumb Implementation

<nav aria-label="Breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="#">Home</a></li>
    <li class="breadcrumb-item"><a href="#">Product Category</a></li>
    <li class="breadcrumb-item active" aria-current="page">Product Details</li>
  </ol>
</nav>

3.2 Key Classes & Attributes

  • Container:
  • .breadcrumb: Outer container for breadcrumbs, showing default separators.
  • aria-label="Breadcrumb": Accessibility label describing the region’s purpose.

  • Items:

  • .breadcrumb-item: Container for each hierarchical level.
  • .active: Marks the current page (no link). Requires aria-current="page" to declare it as the current location.
  • Regular items use <a href="#"> to link to the parent page; the current page omits the <a> tag.

3.3 Custom Separator

To modify the default separator (e.g., “/”), override it with CSS:

.breadcrumb-item + .breadcrumb-item::before {
  content: "›"; /* Replace with a right arrow symbol; ensure Bootstrap’s CSS is included */
  color: #6c757d;
}

4. Summary

  • Tab Navigation: Implemented with .nav-tabs and requires JavaScript for switching. Ideal for categorizing content.
  • Breadcrumb Navigation: Implemented with .breadcrumb and works with pure HTML/CSS (no JS needed).
  • Both support accessibility attributes for better user experience. Bootstrap 5 also offers style extensions (e.g., pill tabs, custom separators).

With these examples, beginners can quickly master the basic usage of Bootstrap 5 navigation components and extend them based on project needs.

Xiaoye