Discover how to make your website's navigation smooth and simple without writing complicated code!
Why Nav tabs and pills in Bootsrap? - Purpose & Use Cases
Imagine you want to create a website with different sections like Home, Profile, and Messages. You write separate links and try to show or hide content manually when users click each link.
This manual way means writing lots of JavaScript to track clicks, show or hide content, and update styles. It's easy to make mistakes, and the code becomes messy and hard to maintain.
Bootstrap's nav tabs and pills provide ready-made styles and behavior. You just write simple HTML with special classes, and Bootstrap handles the active tab switching and content display automatically.
<ul> <li><a href="#" onclick="showHome()">Home</a></li> <li><a href="#" onclick="showProfile()">Profile</a></li> </ul>
<ul class="nav nav-tabs"> <li class="nav-item"><a class="nav-link active" href="#home" data-bs-toggle="tab">Home</a></li> <li class="nav-item"><a class="nav-link" href="#profile" data-bs-toggle="tab">Profile</a></li> </ul>
You can create clean, interactive navigation with minimal code and no extra JavaScript, making your site easier to build and maintain.
Think of a user dashboard where you switch between Overview, Settings, and Notifications tabs smoothly without page reloads or complex scripts.
Manual tab navigation requires complex JavaScript and is error-prone.
Bootstrap nav tabs and pills simplify navigation with built-in styles and behavior.
This makes your website more interactive and easier to manage.
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
