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
Tab Buttons
Profile | Courses | Results
Bootstrap JavaScript
Handles the selected tab and active state.
Content Panel
Shows the selected tab's information.
Courses Content Becomes Visible
The Profile and Results panels are hidden while the Courses panel is active.
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
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%
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-targetthat does not match the content panel ID. -
Forgetting the
tab-paneclass on content panels. - Forgetting to mark the initial tab and its panel as active.
-
Using
data-toggleinstead ofdata-bs-togglein 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-tabscreates bordered tab navigation.nav-pillscreates pill-style navigation.data-bs-targetconnects a tab to its panel.- Bootstrap JavaScript handles tab switching.
- Tabs can improve student dashboards, profile pages, and settings screens.