Bootstrap Navs Tabs And Pills Complete Guide

 Last Update:2025-06-22T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    6 mins read      Difficulty-Level: beginner

Understanding the Core Concepts of Bootstrap Navs, Tabs, and Pills

Bootstrap Navs

Overview

  • Bootstrap Navs are basic navigation elements that can be styled and customized using utility classes.
  • They are used to navigate through different pages or sections on a website.

Key Features

  • Horizontal & Vertical Alignment: Use utility classes .flex-column for vertical alignment.
  • Disabled Links: Use .disabled class to visually indicate disabled links.
  • Active State: Use .active class to highlight the current navigation item.

Usage Example

<ul class="nav">
  <li class="nav-item">
    <a class="nav-link active" aria-current="page" href="#">Active</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  </li>
</ul>

Bootstrap Tabs

Overview

  • Bootstrap Tabs are used to hide and show content panels based on user interaction.
  • They are ideal for smaller screens where you want to save space.

Key Features

  • Automatic Data Attributes: Use data-bs-toggle="tab" and data-bs-target="#elementId" on the nav links.
  • Content Panels: Each tab corresponds to a content panel with class .tab-pane.

Usage Example

<ul class="nav nav-tabs">
  <li class="nav-item">
    <a class="nav-link active" data-bs-toggle="tab" href="#home" role="tab" aria-selected="true">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-bs-toggle="tab" href="#profile" role="tab" aria-selected="false">Profile</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-bs-toggle="tab" href="#contact" role="tab" aria-selected="false">Contact</a>
  </li>
</ul>

<div class="tab-content">
  <div class="tab-pane fade show active" id="home" role="tabpanel">Home content here</div>
  <div class="tab-pane fade" id="profile" role="tabpanel">Profile content here</div>
  <div class="tab-pane fade" id="contact" role="tabpanel">Contact content here</div>
</div>

Bootstrap Pills

Overview

  • Similar to tabs, Bootstrap Pills use a distinct styling for a horizontal tab list.
  • They provide more visual separation compared to standard tabs.

Key Features

  • No Automatic Data Attributes: Pills typically use the .nav-pills class and do not require additional data attributes.
  • Vertical Alignment: Use .flex-column to align pills vertically.

Usage Example

<ul class="nav nav-pills">
  <li class="nav-item">
    <a class="nav-link active" aria-current="page" href="#">Active</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Profile</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Messages</a>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
  </li>
</ul>

Important Information

  1. Responsive Design: All Bootstrap navigation components are inherently responsive. Use media queries or Bootstrap's utility classes to adjust their behavior across devices.

  2. Accessibility: Ensure your navigation elements are accessible. Use appropriate ARIA roles (role="tablist", role="tab", role="tabpanel") and attributes (aria-selected, aria-disabled) to enhance accessibility.

  3. Customization: Customize the appearance of Bootstrap Navs, Tabs, and Pills using CSS or Bootstrap's utility classes.

  4. JavaScript Integration: Tabs and Pills rely on Bootstrap’s JavaScript for interactivity. Ensure your project includes Bootstrap’s JavaScript and dependencies (jQuery, Popper.js).

  5. Server-Side Navigation: For large-scale applications, consider server-side navigation to avoid client-side rendering delays and improve SEO.

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement Bootstrap Navs, Tabs, and Pills

Step-by-Step Guide to Bootstrap Navs, Tabs, and Pills

1. Setup Your Project

First, make sure you have Bootstrap included in your project. You can include Bootstrap via CDN in your HTML file.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bootstrap Navs, Tabs, and Pills</title>
    <!-- Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <!-- Your content goes here -->

    <!-- Bootstrap JS and dependencies -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>

2. Bootstrap Navs

Example: Basic Nav

<nav class="nav">
    <a class="nav-link active" href="#">Home</a>
    <a class="nav-link" href="#">Features</a>
    <a class="nav-link" href="#">Pricing</a>
    <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</nav>

3. Bootstrap Tabs

Step 1: Define Tab Navigation

<ul class="nav nav-tabs" id="myTab" role="tablist">
    <li class="nav-item" role="presentation">
        <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Home</a>
    </li>
    <li class="nav-item" role="presentation">
        <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Profile</a>
    </li>
    <li class="nav-item" role="presentation">
        <a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">Contact</a>
    </li>
</ul>

Step 2: Define Tab Content

<div class="tab-content" id="myTabContent">
    <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
        <p>Welcome to the Home tab! This is where you can add home page content.</p>
    </div>
    <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
        <p>Welcome to the Profile tab! This is where you can add profile page content.</p>
    </div>
    <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">
        <p>Welcome to the Contact tab! This is where you can add contact page content.</p>
    </div>
</div>

4. Bootstrap Pills

Step 1: Define Pill Navigation

<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
    <li class="nav-item" role="presentation">
        <a class="nav-link active" id="pills-home-tab" data-toggle="pill" href="#pills-home" role="tab" aria-controls="pills-home" aria-selected="true">Home</a>
    </li>
    <li class="nav-item" role="presentation">
        <a class="nav-link" id="pills-profile-tab" data-toggle="pill" href="#pills-profile" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</a>
    </li>
    <li class="nav-item" role="presentation">
        <a class="nav-link" id="pills-contact-tab" data-toggle="pill" href="#pills-contact" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</a>
    </li>
</ul>

Step 2: Define Pill Content

<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
        <p>Welcome to the Home pill! This is where you can add home page content.</p>
    </div>
    <div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">
        <p>Welcome to the Profile pill! This is where you can add profile page content.</p>
    </div>
    <div class="tab-pane fade" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">
        <p>Welcome to the Contact pill! This is where you can add contact page content.</p>
    </div>
</div>

5. Complete Example

Here is a complete example that combines the above steps into a single HTML file:

Top 10 Interview Questions & Answers on Bootstrap Navs, Tabs, and Pills

1. What is the difference between Navs, Tabs, and Pills in Bootstrap?

Answer: In Bootstrap, Navs, Tabs, and Pills are all used to navigate between sections of a website, but they have different visual and structural characteristics.

  • Navs: These are basic navigation links. They are commonly used to create navigation bars or sidebars. These links are generally styled for a horizontal alignment and do not provide any interactive behavior like tabs or pills.

  • Tabs: These provide a single content area that can be viewed by clicking on various links (tabs). Only the content associated with the clicked tab is shown, and the other contents are hidden. Tabs are useful for creating a user interface that separates content into distinct sections while still keeping them on the same page.

  • Pills: Similar to tabs, pills also allow you to toggle between different sections of content. The main difference is the visual styling: pills have a rounded look compared to the flat style of tabs. Pills can be used to emphasize the importance of each section or just for a different aesthetic appearance.

2. How do you create a basic navigation bar using Bootstrap Navs?

Answer: To create a basic horizontal navigation bar using Bootstrap Navs, you can structure your HTML as follows:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
      </li>
    </ul>
  </div>
</nav>

3. How can you make the navigation bar responsive using Bootstrap Navs?

Answer: Bootstrap Navs automatically become responsive with the help of the navbar-toggler button. When the screen size is smaller (typically, in mobile view), the navbar-toggler icon button appears, allowing users to expand or collapse the navigation menu. This behavior is controlled by the following classes:

  • navbar-expand-lg: This class is used to specify the breakpoint at which the navigation menu becomes responsive. You can replace lg with sm, md, xl, or xxl based on your preference.

Make sure to include the navbar-toggler button and the div with the collapse class as shown in the previous answer.

4. How do you create Tabs in Bootstrap?

Answer: To create tabs in Bootstrap, you need to combine nav, tab-content, and tab-pane classes. Here’s an example:

<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="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact</button>
  </li>
</ul>
<div class="tab-content" id="myTabContent">
  <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">Content for Home...</div>
  <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">Content for Profile...</div>
  <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">Content for Contact...</div>
</div>

Note: Ensure that data-bs-toggle="tab" and data-bs-target match the corresponding id of the content div.

5. How do you create Pills in Bootstrap?

Answer: Creating pills in Bootstrap is similar to creating tabs, but you need to use the nav-pills class instead of nav-tabs. Here is how:

<ul class="nav nav-pills" id="pills-tab" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="pills-home-tab" data-bs-toggle="pill" data-bs-target="#pills-home" type="button" role="tab" aria-controls="pills-home" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="pills-profile-tab" data-bs-toggle="pill" data-bs-target="#pills-profile" type="button" role="tab" aria-controls="pills-profile" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="pills-contact-tab" data-bs-toggle="pill" data-bs-target="#pills-contact" type="button" role="tab" aria-controls="pills-contact" aria-selected="false">Contact</button>
  </li>
</ul>
<div class="tab-content" id="pills-tabContent">
  <div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">Content for Home...</div>
  <div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">Content for Profile...</div>
  <div class="tab-pane fade" id="pills-contact" role="tabpanel" aria-labelledby="pills-contact-tab">Content for Contact...</div>
</div>

6. Can you create vertical tabs or pills in Bootstrap?

Answer: Yes, you can create vertical tabs or pills by adding the flex-column class to the nav element. Here’s an example of vertical pills:

<div class="row">
  <div class="col-3">
    <ul class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
      <li class="nav-item" role="presentation">
        <button class="nav-link active" id="v-pills-home-tab" data-bs-toggle="pill" data-bs-target="#v-pills-home" type="button" role="tab" aria-controls="v-pills-home" aria-selected="true">Home</button>
      </li>
      <li class="nav-item" role="presentation">
        <button class="nav-link" id="v-pills-profile-tab" data-bs-toggle="pill" data-bs-target="#v-pills-profile" type="button" role="tab" aria-controls="v-pills-profile" aria-selected="false">Profile</button>
      </li>
      <li class="nav-item" role="presentation">
        <button class="nav-link" id="v-pills-messages-tab" data-bs-toggle="pill" data-bs-target="#v-pills-messages" type="button" role="tab" aria-controls="v-pills-messages" aria-selected="false">Messages</button>
      </li>
      <li class="nav-item" role="presentation">
        <button class="nav-link" id="v-pills-settings-tab" data-bs-toggle="pill" data-bs-target="#v-pills-settings" type="button" role="tab" aria-controls="v-pills-settings" aria-selected="false">Settings</button>
      </li>
    </ul>
  </div>
  <div class="col-9">
    <div class="tab-content" id="v-pills-tabContent">
      <div class="tab-pane fade show active" id="v-pills-home" role="tabpanel" aria-labelledby="v-pills-home-tab">Content for Home...</div>
      <div class="tab-pane fade" id="v-pills-profile" role="tabpanel" aria-labelledby="v-pills-profile-tab">Content for Profile...</div>
      <div class="tab-pane fade" id="v-pills-messages" role="tabpanel" aria-labelledby="v-pills-messages-tab">Content for Messages...</div>
      <div class="tab-pane fade" id="v-pills-settings" role="tabpanel" aria-labelledby="v-pills-settings-tab">Content for Settings...</div>
    </div>
  </div>
</div>

7. How does data-bs-toggle work with Bootstrap Navs, Tabs, and Pills?

Answer: The data-bs-toggle attribute is used to control interactive behaviors, and it can be set to tab or pill depending on the component you want to control. When you click the tab or pill button, Bootstrap uses the data-bs-target (or its old equivalent href) attribute to find the corresponding pane to show.

Here’s a brief breakdown:

  • data-bs-toggle="tab": This tells Bootstrap to switch to the tab panel specified by the data-bs-target attribute.
  • data-bs-toggle="pill": Similar to tabs, but for pill navigation.

These attributes ensure that only one tab or pill pane is visible at a time, making it easy to manage content organization and interaction.

8. How can you style Bootstrap Tabs and Pills?

Answer: Bootstrap provides several ways to style tabs and pills, including using utility classes, custom CSS, or the .nav modifier classes. Here are a few examples:

  • Using Utility Classes: You can use color utility classes to change the color of tabs and pills.

    <!-- Tabs with color -->
    <ul class="nav nav-tabs bg-info">
      <li class="nav-item">
        <a class="nav-link text-light" href="#">Tab 1</a>
      </li>
      <li class="nav-item">
        <a class="nav-link text-light" href="#">Tab 2</a>
      </li>
    </ul>
    
    <!-- Pills with color -->
    <ul class="nav nav-pills bg-success">
      <li class="nav-item">
        <a class="nav-link text-white" href="#">Pill 1</a>
      </li>
      <li class="nav-item">
        <a class="nav-link text-white" href="#">Pill 2</a>
      </li>
    </ul>
    
  • Custom CSS: You can also add custom CSS to override the default styles.

    .custom-tabs .nav-link {
      background-color: #f8f9fa;
      color: #333;
    }
    .custom-tabs .nav-link.active {
      background-color: #007bff;
      color: white;
    }
    
    <ul class="nav nav-tabs custom-tabs">
      <li class="nav-item">
        <a class="nav-link active" href="#">Tab 1</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Tab 2</a>
      </li>
    </ul>
    

9. How can you make Tabs and Pills responsive?

Answer: Tabs and pills in Bootstrap don't need special handling for responsiveness, as they are already responsive by default. However, you can make adjustments if you want them to look better on different screen sizes. For example, you can use Bootstrap grid classes or utility classes to control the display and alignment.

Here’s an example with grid classes:

<div class="container mt-3">
  <div class="row">
    <div class="col-md-3">
      <ul class="nav flex-column nav-pills">
        <li class="nav-item">
          <a class="nav-link active" href="#">Link 1</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link 2</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link 3</a>
        </li>
      </ul>
    </div>
    <div class="col-md-9">
      <!-- Content goes here -->
    </div>
  </div>
</div>

In this example, the pills will be stacked vertically on larger screens and take up the whole width on smaller screens.

10. How do you use JavaScript with Bootstrap Tabs and Pills?

Answer: Bootstrap Tabs and Pills utilize event listeners to switch between tabs and pills. These can be manually triggered using JavaScript if you need to control the behavior programmatically. Here are some of the common events and methods:

  • Event: show.bs.tab This event fires before a new tab is shown. You can use this to prevent switching to a new tab by calling event.preventDefault().

    document.addEventListener('show.bs.tab', function (event) {
      if (confirm('Are you sure you want to switch to this tab?')) {
        // Tab will be shown
      } else {
        event.preventDefault();
      }
    });
    
  • Event: shown.bs.tab This event is triggered after a new tab has been shown.

    document.addEventListener('shown.bs.tab', function (event) {
      console.log('Tab shown:', event.target); // newly activated tab
      console.log('Previous tab:', event.relatedTarget); // previous active tab
    });
    
  • Method: .tab('show') Manually activates a list item element and content container. Tab should have either a data-bs-target or an href targeting a container node in the DOM.

    var firstTabEl = document.querySelector('#myTab li:last-child button')
    var firstTab = new bootstrap.Tab(firstTabEl)
    firstTab.show()
    

By using these events and methods, you can add custom behavior to your tabs and pills, making your application more interactive and user-friendly.

You May Like This Related .NET Topic

Login to post a comment.