Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 58 ยท Components

Navs and Tabs

Build Bootstrap navigation with nav, nav-tabs and nav-pills, make it fill or stack vertically, and add working tab panes with the correct ARIA roles.

A "nav" is a row (or column) of links that share one look. Bootstrap's nav component is the base for tabs, pills, the links inside a navbar and many sidebars. It is deliberately small: a flex container plus a few classes for the links. Once you know it, the navbar, list groups and dashboards in later lessons feel familiar.

This lesson covers the plain nav, the nav-tabs and nav-pills styles, alignment and filling, vertical navs, and finally how to turn a nav into working tabs that switch content. You will need the Bootstrap script for the last part (the bundle from Installing Bootstrap), and Flex Utilities will help you read the alignment classes.

The base nav

A nav is a ul or nav element with the class nav, containing items with nav-link:

HTML
<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 disabled" aria-disabled="true">Disabled</a></li>
</ul>

nav is display: flex with flex-wrap: wrap, so the links sit in a row without any extra work. The base style has no borders or backgrounds; it is a blank canvas. The class active marks the current page, and aria-current="page" tells screen readers the same thing. disabled greys the link out and blocks the pointer, but, as with buttons, a link has no real disabled state, so add aria-disabled="true" (and tabindex="-1" if it has an href).

If you only have links (no list), you can put nav-link directly on <a> elements inside a <nav class="nav">. Both are fine; use nav with plain links when the list semantics add nothing.

Alignment and direction

Because .nav is flexbox, you use the flex utilities to align it:

  • justify-content-center centers the links, justify-content-end pushes them to the right.
  • flex-column stacks them vertically.
  • flex-sm-row flex-column stacks on phones and goes horizontal from sm.
Aligning and stacking

Tabs and pills

Two modifier classes turn the blank nav into something that looks like a control.

  • nav-tabs draws a bottom border and gives the active link a raised, tab-like outline. Use it when the links switch panels on the same page.
  • nav-pills gives the active link a filled, rounded background (the primary color). It reads well in toolbars and sidebars.

Both go on the same element as nav: <ul class="nav nav-tabs">.

Tabs, pills and the active state

In Bootstrap 5.3 you can also use nav-underline for a minimal tab look with just a line under the active link. It follows the same markup.

Fill and justify

By default the links are only as wide as their text. Two classes change that:

  • nav-fill makes the links share the available width, each as wide as its content plus an equal share of the leftover space. Widths vary with the text.
  • nav-justified gives every link exactly the same width.
HTML
<ul class="nav nav-pills nav-fill">...</ul>
<ul class="nav nav-pills nav-justified">...</ul>

Choose nav-justified for a segmented control look and nav-fill when your labels differ greatly in length.

Vertical pills

Combine flex-column with nav-pills for a sidebar menu. Inside the grid, give the nav a column such as col-md-3 and the content col-md-9. This vertical layout is also what the tab panes below use for "settings" screens.

flowchart TD
  N["ul.nav.nav-tabs"] --> L1["li > button.nav-link.active"]
  N --> L2["li > button.nav-link"]
  T["div.tab-content"] --> P1["div.tab-pane.show.active"]
  T --> P2["div.tab-pane"]
  L1 -. "data-bs-target" .-> P1
  L2 -. "data-bs-target" .-> P2

Each tab button points at its pane; Bootstrap's script swaps the active classes.

Working tabs with panes

A tab control has two halves: the nav with the buttons, and the tab-content container with one tab-pane per tab. You connect them with data attributes, so no JavaScript of your own is needed:

  1. Put data-bs-toggle="tab" on each trigger (use nav-tabs or nav-pills on the nav; both work).
  2. Point each trigger at its pane with data-bs-target="#pane-id" (on a button) or href="#pane-id" (on a link).
  3. Give the first trigger and its pane active; the pane also needs show so it fades in.

The accessibility roles matter. The nav gets role="tablist", each button gets role="tab" with aria-controls (the pane's id) and aria-selected, and each pane gets role="tabpanel" with aria-labelledby pointing back at its tab. Add tabindex="0" to a pane whose first content is not focusable, so keyboard users can reach it. With these roles in place, the arrow keys move between tabs, Home and End jump to the first and last, exactly as users of desktop tab controls expect.

Use <button> for tab triggers when the tab does not navigate anywhere; it is the right element for an action.

Tabs that switch content

Click the tabs, then focus one and press the arrow keys. The fade class on the panes gives the soft transition; leave it out for an instant switch. Because the panes are all in the HTML already, search engines and screen readers see all of the content; hidden panes are simply display: none until selected.

Tabs also work with nav-pills and flex-column. For a vertical set, add aria-orientation="vertical" to the tablist so assistive technology knows the arrow keys are Up and Down.

Do not use tabs as site navigation between pages. Links that load different pages belong in a plain nav with aria-current="page" on the current one. Tabs are for panels inside one page.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which classes make a nav look like filled rounded buttons?

    nav-pills fills the active link with the primary color and rounds it. nav-tabs draws tab outlines and nav-fill only changes widths.
  2. 2. What is the difference between nav-fill and nav-justified?

    nav-fill respects each link's content width and adds equal extra space; nav-justified forces identical widths.
  3. 3. Which attribute pair makes a button open a tab pane without writing JavaScript?

    The data attributes are read by Bootstrap's script, which activates the target pane. The roles are for accessibility and do not switch anything.
  4. 4. How do you make a nav vertical?

    A nav is a flex container, so flex-column changes its direction. Bootstrap has no nav-vertical class.
  5. 5. Which role belongs on each content panel of a tab set?

    The container of triggers is the tablist, each trigger is a tab and each content area is a tabpanel.

Summary

  • nav is a flex container; nav-link styles the links, and active, disabled and aria-current mark state.
  • nav-tabs and nav-pills (and nav-underline) restyle the same markup; nav-fill and nav-justified control widths.
  • Align with flex utilities such as justify-content-center and flex-column.
  • Working tabs need data-bs-toggle="tab", a target pane inside tab-content, and the Bootstrap bundle.
  • Give tabs role="tablist", role="tab" and role="tabpanel" with matching aria-controls and aria-labelledby ids.

Next lesson: The Navbar.