Master Bootstrap From Scratch

Clear, interactive, and structured Bootstrap lessons designed for beginners and developers.

Bootstrap Tabs

Learn how to organize content into interactive tabs using Bootstrap 5.

What are Bootstrap Tabs?

Bootstrap tabs allow users to switch between different sections of content without loading a new page. When a user selects a tab, its associated content panel becomes visible.

Examples: Student Details, Course Information, Product Details, Profile Settings, and Dashboard Sections.

Learning Objectives

  • Understand the working of Bootstrap tabs.
  • Create horizontal navigation tabs.
  • Connect tab buttons to their content panels.
  • Create tabs with borders using nav-tabs.
  • Create pill-style tabs using nav-pills.
  • Build a practical student information interface.

1. How Do Bootstrap Tabs Work?

Each tab button is connected to a content panel using matching target and panel IDs. Bootstrap JavaScript switches the active tab and displays its associated content.

Working Diagram
1
Tab Buttons

Profile | Courses | Results

2
Bootstrap JavaScript

Handles the selected tab and active state.

3
Content Panel

Shows the selected tab's information.

User selects Courses
↓
Courses Content Becomes Visible

The Profile and Results panels are hidden while the Courses panel is active.

Important: Tabs switch content on the current page. They do not automatically navigate to separate URLs.

2. Creating Basic Tabs

Use nav and nav-tabs to create the tab navigation. The tab-content container holds the content panels.

Program Code
<ul class="nav nav-tabs" role="tablist">
    <li class="nav-item" role="presentation">
        <button class="nav-link active"
                data-bs-toggle="tab"
                data-bs-target="#home"
                type="button" role="tab">
            Home
        </button>
    </li>

    <li class="nav-item" role="presentation">
        <button class="nav-link"
                data-bs-toggle="tab"
                data-bs-target="#about"
                type="button" role="tab">
            About
        </button>
    </li>
</ul>

<div class="tab-content border border-top-0 p-3">
    <div class="tab-pane fade show active" id="home" role="tabpanel">
        Welcome to the Home tab.
    </div>

    <div class="tab-pane fade" id="about" role="tabpanel">
        Learn more about us.
    </div>
</div>
Program Output
Home

Welcome to the Home tab.

About

Learn more about us.

3. Tabs with Borders

The nav-tabs class gives the navigation a tab-style appearance with a visible border around the active tab.

Program Code
<ul class="nav nav-tabs" id="courseTabs" role="tablist">
    <li class="nav-item" role="presentation">
        <button class="nav-link active"
                data-bs-toggle="tab"
                data-bs-target="#java"
                type="button" role="tab">
            Java
        </button>
    </li>

    <li class="nav-item" role="presentation">
        <button class="nav-link"
                data-bs-toggle="tab"
                data-bs-target="#python"
                type="button" role="tab">
            Python
        </button>
    </li>
</ul>
Program Output
Java

Learn Core Java, OOP, JDBC, and Spring Boot.

Python

Learn Python fundamentals, APIs, and backend development.

.NET

Learn C#, ASP.NET Core, MVC, and Web APIs.

4. Creating Pill Tabs

Use nav-pills instead of nav-tabs to create navigation options that look like pills.

Program Code
<ul class="nav nav-pills" role="tablist">
    <li class="nav-item" role="presentation">
        <button class="nav-link active"
                data-bs-toggle="pill"
                data-bs-target="#details"
                type="button" role="tab">
            Details
        </button>
    </li>

    <li class="nav-item" role="presentation">
        <button class="nav-link"
                data-bs-toggle="pill"
                data-bs-target="#fees"
                type="button" role="tab">
            Fees
        </button>
    </li>
</ul>
Program Output
Course Details: Full Stack Development training includes frontend, backend, databases, and project work.
Course Fees: Contact the institute for current course fees.

5. Vertical Tabs

Tabs can also be arranged vertically using Bootstrap flex utilities. This layout is useful for settings pages and dashboards with multiple categories.

Program Code
<div class="d-flex align-items-start gap-3">

    <div class="nav flex-column nav-pills"
         role="tablist" aria-orientation="vertical">

        <button class="nav-link active"
                data-bs-toggle="pill"
                data-bs-target="#profile"
                type="button" role="tab">
            Profile
        </button>

        <button class="nav-link"
                data-bs-toggle="pill"
                data-bs-target="#settings"
                type="button" role="tab">
            Settings
        </button>

    </div>

    <div class="tab-content">
        <div class="tab-pane fade show active" id="profile">
            Profile information goes here.
        </div>
        <div class="tab-pane fade" id="settings">
            Settings information goes here.
        </div>
    </div>

</div>
Program Output
Profile

Manage your name, email, and profile details.

Settings

Manage notification and account preferences.

6. Practical Example: Student Dashboard

In a student dashboard, different tabs can show profile information, enrolled courses, and results. This example uses sample data to demonstrate tab switching.

Program Code
<ul class="nav nav-tabs" role="tablist">
    <li class="nav-item" role="presentation">
        <button class="nav-link active"
                data-bs-toggle="tab"
                data-bs-target="#studentProfile"
                type="button" role="tab">
            Profile
        </button>
    </li>

    <li class="nav-item" role="presentation">
        <button class="nav-link"
                data-bs-toggle="tab"
                data-bs-target="#studentCourses"
                type="button" role="tab">
            Courses
        </button>
    </li>

    <li class="nav-item" role="presentation">
        <button class="nav-link"
                data-bs-toggle="tab"
                data-bs-target="#studentResults"
                type="button" role="tab">
            Results
        </button>
    </li>
</ul>

<div class="tab-content border border-top-0 p-3">
    <div class="tab-pane fade show active" id="studentProfile">
        Student Name: Rahul Sharma
    </div>

    <div class="tab-pane fade" id="studentCourses">
        Java Full Stack
        Python Full Stack
    </div>

    <div class="tab-pane fade" id="studentResults">
        HTML: 85%
        SQL: 90%
    </div>
</div>
Program Output
Student Profile

Name: Rahul Sharma

Student ID: CIIT-1025

Enrolled Courses
  • Java Full Stack
  • Python Full Stack
Examination Results

HTML: 85%

SQL: 90%

Real-world note: This example uses static sample data. In an ASP.NET Core MVC project, student details and results can instead be loaded from a database through a controller and view model.

7. Important Classes and Attributes

Class / Attribute Purpose
nav Creates a navigation container.
nav-tabs Creates tab-style navigation with borders.
nav-pills Creates pill-style navigation.
nav-link Styles a tab navigation item.
active Marks the initially selected tab or panel.
tab-content Contains all tab panels.
tab-pane Defines an individual content panel.
data-bs-toggle="tab" Enables tab behavior for the button.
data-bs-target Connects a tab button to its content panel.
fade Adds a fade transition when panels change.

8. Common Mistakes

  • Using a data-bs-target that does not match the content panel ID.
  • Forgetting the tab-pane class on content panels.
  • Forgetting to mark the initial tab and its panel as active.
  • Using data-toggle instead of data-bs-toggle in Bootstrap 5.
  • Forgetting to include Bootstrap JavaScript.
  • Reusing the same IDs for multiple tab groups on one page.

Summary

  • Bootstrap tabs organize related content into separate panels.
  • nav-tabs creates bordered tab navigation.
  • nav-pills creates pill-style navigation.
  • data-bs-target connects a tab to its panel.
  • Bootstrap JavaScript handles tab switching.
  • Tabs can improve student dashboards, profile pages, and settings screens.