Use the tab JavaScript plugin—include it individually or through the compiled bootstrap.js
file—to extend our navigational tabs and pills to create tabbable panes of local content, even via
dropdown menus.
If you’re building our JavaScript from source, it requires
util.js
.
Dynamic tabbed interfaces, as described in the WAI ARIA
Authoring Practices, require role="tablist"
,
role="tab"
, role="tabpanel"
, and additional aria-
attributes in order to convey their structure, functionality and current
state to users of assistive technologies (such as screen readers).
Note that dynamic tabbed interfaces should not contain dropdown menus, as this causes both usability and accessibility issues. From a usability perspective, the fact that the currently displayed tab’s trigger element is not immediately visible (as it’s inside the closed dropdown menu) can cause confusion. From an accessibility point of view, there is currently no sensible way to map this sort of construct to a standard WAI ARIA pattern, meaning that it cannot be easily made understandable to users of assistive technologies.
<!-- Tab -->
<ul class="nav nav-tabs mb-3" id="myTab" role="tablist">
<li class="nav-item">
<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">
<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">
<a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">Contact</a>
</li>
</ul>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">Some text here</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">Some text here</div>
<div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">Some text here</div>
</div>
<!-- End of tab -->
To help fit your needs, this works with <ul>
-based markup, as shown above, or with any arbitrary “roll your own” markup. Note that if you’re using <nav>
,
you shouldn’t add role="tablist"
directly to it, as this would override the element’s native role as a navigation landmark. Instead, switch to an alternative element (in the example below,
a simple <div>
) and wrap the <nav>
around it.
<!-- Tab -->
<nav>
<div class="nav nav-tabs mb-3" id="nav-tab" role="tablist">
<a class="nav-item nav-link active" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab" aria-controls="nav-home" aria-selected="true">Home</a>
<a class="nav-item nav-link" id="nav-profile-tab" data-toggle="tab" href="#nav-profile" role="tab" aria-controls="nav-profile" aria-selected="false">Profile</a>
<a class="nav-item nav-link" id="nav-contact-tab" data-toggle="tab" href="#nav-contact" role="tab" aria-controls="nav-contact" aria-selected="false">Contact</a>
</div>
</nav>
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">Some text here</div>
<div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">Some text here</div>
<div class="tab-pane fade" id="nav-contact" role="tabpanel" aria-labelledby="nav-contact-tab">Some text here</div>
</div>
<!-- End of tab -->
You can activate a tab or pill navigation without writing any JavaScript by simply specifying data-toggle="tab"
or data-toggle="pill"
on an element. Use these data
attributes on .nav-tabs
or
.navs
.
<!-- Nav tabs -->
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item">
<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">
<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">
<a class="nav-link" id="messages-tab" data-toggle="tab" href="#messages" role="tab" aria-controls="messages" aria-selected="false">Messages</a>
</li>
<li class="nav-item">
<a class="nav-link" id="settings-tab" data-toggle="tab" href="#settings" role="tab" aria-controls="settings" aria-selected="false">Settings</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane active" id="home" role="tabpanel" aria-labelledby="home-tab">...</div>
<div class="tab-pane" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
<div class="tab-pane" id="messages" role="tabpanel" aria-labelledby="messages-tab">...</div>
<div class="tab-pane" id="settings" role="tabpanel" aria-labelledby="settings-tab">...</div>
</div>
Enable tabbable tabs via JavaScript (each tab needs to be activated individually):
$('#myTab a').on('click', function (e) {
e.preventDefault();
$(this).tab('show');
})
You can activate individual tabs in several ways:
$('#myTab a[href="#profile"]').tab('show'); // Select tab by name
$('#myTab li:first-child a').tab('show'); // Select first tab
$('#myTab li:last-child a').tab('show'); // Select last tab
$('#myTab li:nth-child(3) a').tab('show'); // Select third tab
For more information, see the official Bootstrap documentation.