Bootstrap 5 Inline List with .list-inline and .list-inline-item

An inline list shows its items side by side instead of one per line: a row of tags, the links in a footer, the steps of a short process. Bootstrap 5.3 does it with two classes.

The examples need only the stylesheet: https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css.

The markup

<ul class="list-inline">
  <li class="list-inline-item">Home</li>
  <li class="list-inline-item">All posts</li>
  <li class="list-inline-item">Examples</li>
  <li class="list-inline-item">Contact</li>
</ul>

This is the one change from Bootstrap 3, where .list-inline on the <ul> was enough and every <li> was styled automatically. Since Bootstrap 4, each item needs .list-inline-item. Old markup without it renders as a vertical list with no bullets, which is the usual sign of a half-finished upgrade.

.list-inline removes the bullets and left padding. .list-inline-item makes each item inline-block and adds a 0.5rem right margin to every item except the last.

Inline ordered lists lose their numbers

You can put the same classes on an <ol>, but the numbers disappear. Browsers only draw a marker for elements displayed as list items, and inline-block items are not. If the order should be visible, number the items with a CSS counter:

<ol class="list-inline steps">
  <li class="list-inline-item">Download</li>
  <li class="list-inline-item">Install</li>
  <li class="list-inline-item">Configure</li>
  <li class="list-inline-item">Run</li>
</ol>
.steps {
  counter-reset: step;
}

.steps .list-inline-item::before {
  counter-increment: step;
  content: counter(step) ". ";
}

This renders as “1. Download 2. Install 3. Configure 4. Run” on a single line, and the <ol> still tells assistive technology that the order matters. Because the bullets are gone, Safari’s VoiceOver may stop announcing the element as a list; adding role="list" to the <ol> restores that.

A flex alternative

The inline-block approach spaces items with a right margin, which leaves a gap on the right of every line when the list wraps. The flex utilities give more control and an even gap in both directions:

<ul class="list-unstyled d-flex flex-wrap gap-3">
  <li>Home</li>
  <li>All posts</li>
  <li>Examples</li>
  <li>Contact</li>
</ul>

gap-3 sets 1rem between items horizontally and between wrapped lines vertically. Add justify-content-center to center the row, which is awkward to do neatly with inline-block items.

When it is navigation

The list in the original example (Home, All posts, Examples, Contact) is a menu. For that, use a nav component inside a <nav> landmark so screen reader users can jump to it:

<nav aria-label="Main">
  <ul class="nav">
    <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="/examples/">Examples</a></li>
    <li class="nav-item"><a class="nav-link" href="/contact/">Contact</a></li>
  </ul>
</nav>

If the items describe where the user is in a hierarchy, the breadcrumb component is the ordered inline list Bootstrap already provides. It uses an <ol>, draws the separators for you, and marks the current page with aria-current:

<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="/">Home</a></li>
    <li class="breadcrumb-item"><a href="/examples/">Examples</a></li>
    <li class="breadcrumb-item active" aria-current="page">Bootstrap</li>
  </ol>
</nav>

Related examples

Leave a Comment