.nav-tabs draws a row of tabs with the active one connected to the content below. In Bootstrap 5 you can use it in two different ways, and the markup depends on which one you mean:
- A navigation menu whose tabs are ordinary links to other pages.
- Tab panes, where clicking a tab swaps content on the same page without loading anything.
Page setup
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap tabs</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<main class="container py-4">
<!-- tabs go here -->
</main>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
The JavaScript bundle is only needed for the second variant.
A tab menu for navigation
<nav aria-label="Site sections">
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/posts/">All posts</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/category/bootstrap/">Bootstrap examples</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/contact/">Contact</a>
</li>
</ul>
</nav>
The Bootstrap 3 version put .active on the <li>. Since Bootstrap 4 every <li> needs .nav-item, every link needs .nav-link, and .active goes on the link. Old markup without these classes renders as a plain bulleted list.
aria-current="page" tells screen reader users which tab is the current page; the .active class alone is only visual.
For a disabled tab, add .disabled to the link together with aria-disabled="true", and remove its href so it cannot be reached with the keyboard.
Setting the active tab in Razor
In ASP.NET Core you usually mark the active tab from the current route:
@{
string? controller = ViewContext.RouteData.Values["controller"] as string;
bool onPosts = controller == "Posts";
}
<a class="nav-link @(onPosts ? "active" : "")"
aria-current="@(onPosts ? "page" : null)"
href="/posts/">All posts</a>
When an attribute’s value is a Razor expression that evaluates to null, Razor omits the attribute, so aria-current only appears on the active link.
Tabs that switch content
For tabs that show and hide panes on the same page, use buttons and the tab plugin:
<ul class="nav nav-tabs" id="profile-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-pane" type="button" role="tab"
aria-controls="home-pane" aria-selected="true">Home</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="posts-tab" data-bs-toggle="tab"
data-bs-target="#posts-pane" type="button" role="tab"
aria-controls="posts-pane" aria-selected="false">Posts</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="contact-tab" data-bs-toggle="tab"
data-bs-target="#contact-pane" type="button" role="tab"
aria-controls="contact-pane" aria-selected="false">Contact</button>
</li>
</ul>
<div class="tab-content border border-top-0 p-3" id="profile-tabs-content">
<div class="tab-pane fade show active" id="home-pane" role="tabpanel"
aria-labelledby="home-tab" tabindex="0">Home content</div>
<div class="tab-pane fade" id="posts-pane" role="tabpanel"
aria-labelledby="posts-tab" tabindex="0">Posts content</div>
<div class="tab-pane fade" id="contact-pane" role="tabpanel"
aria-labelledby="contact-tab" tabindex="0">Contact content</div>
</div>
data-bs-toggle="tab" (formerly data-toggle) is all it takes; there is no initialization script. The roles and aria-* attributes come from Bootstrap’s documentation and let screen readers announce the buttons as tabs and report which one is selected. Bootstrap updates aria-selected itself when the active tab changes, and it also supports moving between tabs with the arrow keys.
Each pane needs both .fade and, on the first one, .show active. Leave out .show on the initially active pane and it stays invisible until the user clicks another tab and back.
Opening a tab from the URL
A common request is that page.html#posts-pane opens with the Posts tab selected, and that the address updates when the user switches tabs:
const tabButtons = document.querySelectorAll('#profile-tabs [data-bs-toggle="tab"]');
tabButtons.forEach(button => {
if (button.dataset.bsTarget === location.hash) {
bootstrap.Tab.getOrCreateInstance(button).show();
}
button.addEventListener('shown.bs.tab', () => {
history.replaceState(null, '', button.dataset.bsTarget);
});
});
replaceState changes the address without adding a history entry for every tab click, so the Back button still leaves the page.
Other styles
Swap .nav-tabs for .nav-pills to get rounded buttons, or for .nav-underline (new in 5.3) for a line under the active item. .nav-fill or .nav-justified on the <ul> stretch the tabs across the full width. The markup otherwise stays the same.
Related examples
- Bootstrap accordion
- Bootstrap pagination
- Bootstrap list group example
- Navs and tabs in the Bootstrap documentation