Nav tabs and pills help organize content into sections that users can switch between easily. They make websites cleaner and easier to navigate.
Nav tabs and pills in Bootsrap
<ul class="nav nav-tabs"> <li class="nav-item"> <a class="nav-link active" href="#">Active Tab</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Another Tab</a> </li> </ul> <ul class="nav nav-pills"> <li class="nav-item"> <a class="nav-link active" href="#">Active Pill</a> </li> <li class="nav-item"> <a class="nav-link" href="#">Another Pill</a> </li> </ul>
Use nav-tabs for tab style navigation and nav-pills for pill style navigation.
Add active class to highlight the current tab or pill.
<ul class="nav nav-tabs"> <li class="nav-item"> <a class="nav-link active" href="#home">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="#profile">Profile</a> </li> </ul>
<ul class="nav nav-pills"> <li class="nav-item"> <a class="nav-link active" href="#messages">Messages</a> </li> <li class="nav-item"> <a class="nav-link" href="#settings">Settings</a> </li> </ul>
disabled class to the nav-link and setting tabindex="-1" and aria-disabled="true" for accessibility.<ul class="nav nav-tabs"> <li class="nav-item"> <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled Tab</a> </li> <li class="nav-item"> <a class="nav-link active" href="#">Active Tab</a> </li> </ul>
This example shows two navigation styles: tabs and pills. Clicking each tab or pill changes the visible content below it. It uses Bootstrap 5's JavaScript for switching content and includes accessibility roles and labels.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Nav Tabs and Pills Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <main class="container mt-4"> <h1>Nav Tabs and Pills Demo</h1> <section aria-label="Navigation Tabs"> <h2>Tabs</h2> <ul class="nav nav-tabs" 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> </ul> <div class="tab-content p-3 border border-top-0" id="myTabContent"> <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab"> <p>Welcome to the Home tab content.</p> </div> <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab"> <p>This is the Profile tab content.</p> </div> </div> </section> <section aria-label="Navigation Pills" class="mt-5"> <h2>Pills</h2> <ul class="nav nav-pills" role="tablist"> <li class="nav-item" role="presentation"> <button class="nav-link active" id="messages-pill" data-bs-toggle="tab" data-bs-target="#messages" type="button" role="tab" aria-controls="messages" aria-selected="true">Messages</button> </li> <li class="nav-item" role="presentation"> <button class="nav-link" id="settings-pill" data-bs-toggle="tab" data-bs-target="#settings" type="button" role="tab" aria-controls="settings" aria-selected="false">Settings</button> </li> </ul> <div class="tab-content p-3 border border-top-0" id="myPillContent"> <div class="tab-pane fade show active" id="messages" role="tabpanel" aria-labelledby="messages-pill"> <p>You have no new messages.</p> </div> <div class="tab-pane fade" id="settings" role="tabpanel" aria-labelledby="settings-pill"> <p>Adjust your settings here.</p> </div> </div> </section> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Use button elements with data-bs-toggle for better accessibility and Bootstrap JavaScript support.
Always include aria-controls, aria-selected, and roles for screen readers.
Bootstrap's JavaScript must be included for tabs and pills to switch content properly.
Nav tabs and pills organize content into clickable sections.
Use nav-tabs for tab style and nav-pills for pill style navigation.
Include accessibility attributes and Bootstrap JS for interactive switching.
Practice
nav-tabs and nav-pills in Bootstrap navigation?Solution
Step 1: Understand Bootstrap navigation styles
nav-tabscreates a tab-like navigation with rectangular borders around each item.Step 2: Compare with
nav-pillsnav-pillsstyles navigation links as rounded pills, giving a softer look.Final Answer:
nav-tabsshows tabs with borders,nav-pillsshows rounded pill-shaped links. -> Option BQuick Check:
Tabs = rectangular, Pills = rounded [OK]
- Thinking nav-tabs are vertical only
- Believing nav-pills don't need JS
- Confusing disabled/enabled states
Solution
Step 1: Recall Bootstrap classes for pills
The correct class for pill navigation isnav-pillscombined withnav.Step 2: Check each option
<ul class='nav nav-pills'>usesnav nav-pills, which is correct. Others have incorrect or non-existent classes.Final Answer:
<ul class='nav nav-pills'> -> Option CQuick Check:
Usenav nav-pillsfor pills [OK]
- Using only 'pills' without 'nav'
- Combining 'nav-tabs' and 'pills' incorrectly
- Misspelling class names
<ul class='nav nav-tabs' role='tablist'>
<li class='nav-item'>
<a class='nav-link active' href='#home' role='tab' aria-selected='true'>Home</a>
</li>
<li class='nav-item'>
<a class='nav-link' href='#profile' role='tab' aria-selected='false'>Profile</a>
</li>
</ul>Solution
Step 1: Analyze the classes and attributes
Thenav-tabsclass creates tab style navigation. Thenav-link activeclass highlights the 'Home' tab as active.Step 2: Understand the visual effect
'Home' tab will appear selected with a border, 'Profile' will appear as a normal tab link.Final Answer:
Two tabs labeled 'Home' and 'Profile', with 'Home' highlighted as active. -> Option AQuick Check:
Active tab is highlighted in nav-tabs [OK]
- Confusing pills with tabs visually
- Assuming both tabs are active
- Ignoring role and aria attributes
<ul class='nav nav-pills'>
<li class='nav-item'>
<a class='nav-link active' data-toggle='tab' href='#home'>Home</a>
</li>
<li class='nav-item'>
<a class='nav-link' data-toggle='tab' href='#profile'>Profile</a>
</li>
</ul>Solution
Step 1: Check Bootstrap version attribute changes
Bootstrap 5 replaceddata-togglewithdata-bs-togglefor JavaScript plugins.Step 2: Verify other attributes and classes
nav-pillsis valid,hrefcan use fragment IDs, andnav-itembelongs onli.Final Answer:
The attributedata-toggleshould bedata-bs-togglein Bootstrap 5+. -> Option AQuick Check:
Use 'data-bs-toggle' in Bootstrap 5+ [OK]
- Using old Bootstrap 4 attributes
- Misplacing classes on wrong elements
- Thinking href must be full URL
Solution
Step 1: Identify correct classes and roles for pills navigation
Usenav nav-pillsonulwithrole='tablist'for accessibility.Step 2: Check anchor attributes for tab switching and accessibility
Anchors needid,data-bs-toggle='pill',hrefto panel,role='tab',aria-controls, andaria-selected.Step 3: Confirm active state on the selected tab
The active tab hasnav-link activeandaria-selected='true'.Final Answer:
<ul class='nav nav-pills' role='tablist'> <li class='nav-item'> <a class='nav-link active' id='home-tab' data-bs-toggle='pill' href='#home' role='tab' aria-controls='home' aria-selected='true'>Home</a> </li> </ul>-> Option DQuick Check:
Use 'nav-pills' with proper roles and 'data-bs-toggle' [OK]
- Omitting role or aria attributes
- Using old 'data-toggle' instead of 'data-bs-toggle'
- Missing active class on selected tab
Ready to prove it?
Pass the test and this pattern moves unknown
