Table of contents
1.
Introduction
2.
How to enable tab-plugin?
3.
Using Data Attributes
4.
Using Javascript
5.
Introducing fade effect
6.
Events in the tab plugin
7.
Frequently Asked Questions
7.1.
How do I create a tabbed interface with Bootstrap?
7.2.
Can I use different styles for different tabs in Bootstrap?
7.3.
How do I make a tab active by default in Bootstrap?
7.4.
How do I handle events in Bootstrap tabs?
7.5.
How do I disable a tab in Bootstrap?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Bootstrap - Tab Plugin

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Bootstrap navigation elements were introduced to style the navigation bar. Additional features are added to these nav tabs as plug-ins. The plug-in allows switching between panes of views using nav-tabs. On clicking each nav-tab , a single pane of view will be shown to the user. It is in a similar context to conditional rendering. The identity of content is referenced inside the click handler of the tab and the plugin helps render that particular view.

OG Image

In this blog, we will learn about Bootstrap-Tab Plugins.

How to enable tab-plugin?

There are two primary methods to enable tab-plugin, the first one is using data attributes, and the second one is using Javascript.

Using Data Attributes

Here we can access the tabs by using the data-toggle in the anchor tag; We can use it like this data-toggle = “tab” or this data-toggle=”pill”.

<!doctype html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
	<link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
	<title>Bootstrap Example</title>
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</head>

<body>
	<h2 style="margin-bottom: 10px" align="center">Ninjas Playground</h2>

	<!-- Nav tabs -->
	<ul class="nav nav-tabs" id="myTab" role="tablist" style="margin-left: 10px">
		<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">
				CN 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">
				Ninja Profile
			</button>
		</li>
		<li class="nav-item" role="presentation">
			<button class="nav-link" id="messages-tab" data-bs-toggle="tab" data-bs-target="#messages" type="button"
				role="tab" aria-controls="messages" aria-selected="false">
				Ninja Inbox
			</button>
		</li>
	</ul>

	<!-- Tab panes -->
	<div class="tab-content" style="margin-left: 10px">
		<div class="tab-pane active" id="home" role="tabpanel" aria-labelledby="home-tab" style="margin-top: 5px">
			Home page of Coding Ninjas
		</div>
		<div class="tab-pane" id="profile" role="tabpanel" aria-labelledby="profile-tab" style="margin-top: 5px">
			Profile page of Ninja
		</div>
		<div class="tab-pane" id="messages" role="tabpanel" aria-labelledby="messages-tab" style="margin-top: 5px">
			Inbox of Ninja
		</div>
	</div>

</body>

</html>

 

Output

Output of Code using Data Attributes

Using Javascript

We can also enable each tab using several ways,

<script>
      // Select tab by href
      let triggerElement = document.querySelector('a[href="#profile"]');
      $(triggerElement).tab("show");

      // Select first tab
      let firstTabElement = document.querySelector("li:first-child a");
      $(firstTabElement).tab("show");

      // Select last tab
      let lastTabElement = document.querySelector("li:last-child a");
      $(lastTabElement).tab("show");
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" />
You can also try this code with Online Javascript Compiler
Run Code

Output

Output of above code using Javascript

Introducing fade effect

Adding a fade effect means adding a level of styling while switching between panes in the view. In order to add a fade effect, we need to give a “fade” class to each of the tab-pane containers.

<!doctype html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
	<link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
	<title>Bootstrap Example</title>
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</head>

<body>
	<h2 style="margin-bottom: 10px" align="center">Ninjas Playground</h2>

	<!-- Nav tabs -->
	<ul class="nav nav-tabs" id="myTab" style="margin-left: 10px">
		<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-selected="true">
				CN Home
			</button>
		</li>
		<li class="nav-item" role="presentation">
			<button class="nav-link" id="dashboard-tab" data-bs-toggle="tab" data-bs-target="#dashboard" type="button"
				role="tab" aria-selected="false">
				Ninja Dashboard
			</button>
		</li>
		<li class="nav-item" role="presentation">
			<button class="nav-link" id="inbox-tab" data-bs-toggle="tab" data-bs-target="#inbox" type="button"
				role="tab" aria-selected="false">
				Ninja Inbox
			</button>
		</li>
	</ul>
	<div class="tab-content" style="margin-left: 10px; margin-top: 10px">
		<div class="tab-pane fade show active" role="tabpanel" id="home" aria-labelledby="home-tab">
			Coding Ninjas Home
		</div>
		<div class="tab-pane fade" role="tabpanel" id="dashboard" aria-labelledby="dashboard-tab">
			Ninjas Dashboard
		</div>
		<div class="tab-pane fade" id="inbox" role="tabpanel" aria-labelledby="inbox-tab">
			Ninjas Inbox
		</div>
	</div>
</body>

</html>

 

Output

Output of above code

Events in the tab plugin

image of events tab

Example

$('#inbox-tab a').on('show.bs.tab', function (e) {
  var currentTab = $(e.target); // newly activated tab
  var previousTab = $(e.relatedTarget); // previous active tab

  // Do something with the tabs here

})

In this example, the event is bound to all elements within the element with an ID of myTab, which is typically a tabbed interface. The event.target property gives you access to the newly activated tab while the event.relatedTarget property gives you access to the previously active tab. You can use these properties to perform some action before the new tab is displayed, such as updating the content of the new tab based on the previous tab's data.

Frequently Asked Questions

How do I create a tabbed interface with Bootstrap?

To create a tabbed interface with Bootstrap, you need to wrap your tabs and their corresponding content in a container with a class of .tab-content, and then use the data-toggle attribute to toggle between tabs using JavaScript.

Can I use different styles for different tabs in Bootstrap?

Yes, you can apply different styles to different tabs by using custom CSS classes on the tab elements and their corresponding content.

How do I make a tab active by default in Bootstrap?

To make a tab active by default in Bootstrap, you need to add the .active class to the tab and its corresponding content.

How do I handle events in Bootstrap tabs?

Bootstrap provides several events for handling tabs, such as the show.bs.tab event that is triggered when a new tab is about to be shown and the shown.bs.tab event that is triggered after a new tab is shown. You can bind these events to the tabs using JavaScript to perform actions based on user interactions with the tabs.

How do I disable a tab in Bootstrap?

To disable a tab in Bootstrap, you need to add the .disabled class to the tab and its corresponding content. This will prevent users from clicking on the tab to make it active.

Check this out - Bootstrap Accordion

Conclusion

Congrats on getting through this article; we went through all of the concepts in the tab plugin and also saw some examples implementing the same. We saw multiple ways to activate a tab-pane onto the view and also the events associated with tab-panes.

If you want to get started with bootstrap for free and want to learn more about it then you can visit Learn Bootstrap, and if you want to know why Bootstrap is a great framework of CSS, you can also check this blog.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like AmazonMicrosoft, etc, you must look at the problems, interview experiences, and interview bundle for placement preparations.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass