Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 58 ยท Components

Alerts, Badges, Progress and Spinners

Use Bootstrap alerts, dismissible alerts, badges, progress bars (striped and animated), spinners and placeholders to give users clear feedback on state and loading.

Interfaces constantly talk back to users: "Saved", "Something went wrong", "3 new messages", "Uploading... 60%", "Loading". Bootstrap has small, focused components for each of those messages. They are lightweight (mostly pure CSS), they share the theme colors you already know, and they take only a few classes.

This lesson covers alerts, badges, progress bars, spinners and placeholders. Only the dismissible alert needs JavaScript. For the colors, review Colors and Backgrounds.

Alerts

An alert is a message box: alert plus a variant such as alert-success, alert-danger, alert-warning, alert-info, alert-primary, alert-secondary, alert-light or alert-dark. Give it role="alert" so assistive technology announces it when it appears.

Pick the variant by meaning, not by looks: green for success, red for errors, yellow for cautions, blue for neutral information. Do not rely on color alone to convey meaning, though. Use a clear word ("Error:", "Saved") or an icon so users who cannot see color differences still understand.

Inside an alert, use alert-heading for a title, alert-link for links that should match the alert's color, and ordinary hr and p for longer messages. Utilities like d-flex align-items-center help you place an icon.

Dismissible alerts

Add alert-dismissible fade show, then a close button with data-bs-dismiss="alert". The fade show pair gives the fade-out animation; alert-dismissible adds right padding so the text does not run under the button. This needs the Bootstrap script.

Alert variants and a dismissible alert

Click the close button: the alert fades and is removed from the DOM. If you need to show it again, you have to create it again; dismissal does not just hide it.

Badges

A badge is a small label for counts and status. The class is badge, combined with a background through text-bg-* (the 5.3 way) such as text-bg-primary. Badges scale with the text around them, so they fit inside headings and buttons. Make them pill-shaped with rounded-pill.

HTML
<h2>Inbox <span class="badge text-bg-secondary">4</span></h2>
<button class="btn btn-primary position-relative">
  Messages
  <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill text-bg-danger">
    9+ <span class="visually-hidden">unread messages</span>
  </span>
</button>

The second example positions a badge over the corner of a button using the position utilities from Display and Visibility. The visually-hidden text matters: a screen reader user cannot see that "9+" means unread messages, so you add words only they will hear.

Progress bars

A progress bar has two layers: an outer progress (the track) and an inner progress-bar (the fill). You set the fill width with a width style or a utility like w-50, and you describe it for assistive technology with role="progressbar", aria-valuenow, aria-valuemin and aria-valuemax. Give the track an aria-label too.

Add a label by putting text inside the bar. Change the height by setting style="height: 20px" on the outer progress. Color it with bg-success, bg-warning and friends. To stack several segments, place multiple progress-bar elements in one progress-stacked container (each wrapped in a progress with its own width).

Two modifiers add motion: progress-bar-striped draws diagonal stripes, and progress-bar-animated makes them move. Both are on the inner bar. Users who prefer reduced motion get a stopped animation automatically.

Progress bars, striped and animated

The script shows the one rule that is easy to forget: when the visual width changes, update aria-valuenow as well, or assistive technology will keep reporting the old value.

Spinners

A spinner says "something is happening, but I do not know how long". There are two kinds: spinner-border (a rotating ring) and spinner-grow (a pulsing dot). They take the current text color, so text-primary or text-danger colors them. Size them with spinner-border-sm or custom width and height styles.

Give each spinner role="status" and include a visually-hidden "Loading..." so its purpose is announced. Spinners also fit inside buttons, a common pattern while a form submits.

HTML
<button class="btn btn-primary" type="button" disabled>
  <span class="spinner-border spinner-border-sm" aria-hidden="true"></span>
  <span role="status">Loading...</span>
</button>

For long waits, a determinate progress bar is friendlier than a spinner, because it tells the user how much is left.

Placeholders

Placeholders (sometimes called skeleton screens) show the shape of content while it loads, which feels faster than a blank space. Elements with the class placeholder become gray blocks; you set their width with the column classes (col-6) and size with placeholder-lg, placeholder-sm or placeholder-xs. Add placeholder-glow or placeholder-wave on the parent for animation. Mark the area with aria-hidden="true" so screen readers skip the empty shapes.

Spinner and placeholder card

Swapping the placeholders for real content is just a DOM update; here a click stands in for the arrival of data.

Choose the loading indicator by wait time: a spinner for short, unknown waits; a progress bar for measurable work; placeholders when you know the page layout in advance.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which classes turn an alert into one that users can close?

    alert-dismissible adds room for the button, fade show enables the animation and data-bs-dismiss closes the alert via the Bootstrap script.
  2. 2. Why add a visually-hidden span inside a notification badge?

    A bare number has no context for someone who cannot see the layout; hidden text supplies it.
  3. 3. What must you do besides changing the width when a progress bar moves?

    The ARIA value is separate from the CSS width and has to be kept in sync.
  4. 4. Which pair of classes makes a striped bar whose stripes move?

    Both go on the inner .progress-bar element.
  5. 5. What is the purpose of the placeholder classes?

    Placeholders build skeleton screens; they are not related to the HTML placeholder attribute.

Summary

  • Alerts use alert alert-{variant} with role="alert"; make them dismissible with alert-dismissible fade show and data-bs-dismiss="alert".
  • Badges are badge text-bg-*, optionally rounded-pill; add visually hidden text when a count needs context.
  • Progress bars are an outer progress and an inner progress-bar; add ARIA values, and optional progress-bar-striped and progress-bar-animated.
  • Spinners (spinner-border, spinner-grow) need role="status" and hidden text; placeholders create skeleton screens with placeholder and placeholder-glow.
  • Never rely on color alone; pair it with words or icons.

Next lesson: List Groups, Breadcrumbs and Pagination.