Bootstrap 5 Progress Bars: Colors, Stripes and Stacked Bars

A Bootstrap progress bar is two nested div elements: an outer .progress track and an inner .progress-bar whose width you set. Bootstrap 5.3 changed the recommended markup, so even code written for Bootstrap 5.0 to 5.2 is slightly out of date. The ARIA attributes now go on the outer element, and stacked bars get their own wrapper.

Page setup

Progress bars are pure CSS:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap progress bars</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">
    <!-- progress bars go here -->
  </main>
</body>
</html>

Basic bar

<div class="progress" role="progressbar" aria-label="Upload progress"
     aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">
  <div class="progress-bar" style="width: 25%"></div>
</div>

The role="progressbar" and the aria-value* attributes are what a screen reader announces; the width is only for the eye. Keep the two in sync. The Bootstrap 3 version put a hidden <span class="sr-only">25% Complete</span> inside the bar instead. That class is now .visually-hidden, and with the ARIA attributes in place you do not need the span at all.

To show the value on the bar, put text inside .progress-bar:

<div class="progress" role="progressbar" aria-label="Upload progress"
     aria-valuenow="40" aria-valuemin="0" aria-valuemax="100">
  <div class="progress-bar" style="width: 40%">40%</div>
</div>

Colors, stripes and animation

Color the bar with a background utility. Bootstrap 3’s .progress-bar-success, .progress-bar-info, .progress-bar-warning and .progress-bar-danger are gone; use .bg-success and friends, or .text-bg-warning when the bar has a label and you need dark text on the yellow background.

Stripes moved from the outer element to the bar: .progress-striped became .progress-bar-striped, and the old .active class that animated them is now .progress-bar-animated.

<div class="progress mb-3" role="progressbar" aria-label="Warning example"
     aria-valuenow="50" aria-valuemin="0" aria-valuemax="100">
  <div class="progress-bar text-bg-warning" style="width: 50%">50%</div>
</div>

<div class="progress mb-3" role="progressbar" aria-label="Striped example"
     aria-valuenow="40" aria-valuemin="0" aria-valuemax="100">
  <div class="progress-bar progress-bar-striped" style="width: 40%"></div>
</div>

<div class="progress mb-3" role="progressbar" aria-label="Animated striped example"
     aria-valuenow="90" aria-valuemin="0" aria-valuemax="100">
  <div class="progress-bar progress-bar-striped progress-bar-animated bg-info" style="width: 90%"></div>
</div>

The stripe animation is switched off for users who have asked their operating system to reduce motion, through a prefers-reduced-motion media query in Bootstrap’s CSS.

Height

Set the height on the outer .progress element only. The bar fills it automatically:

<div class="progress" role="progressbar" aria-label="Thin bar"
     aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="height: 4px">
  <div class="progress-bar" style="width: 60%"></div>
</div>

Stacked bars

Several bars in one track used to be several .progress-bar elements inside one .progress. In 5.3 each segment is a complete .progress with its own ARIA attributes, all wrapped in .progress-stacked. The width moves to the outer element of each segment:

<div class="progress-stacked">
  <div class="progress" role="progressbar" aria-label="Passed"
       aria-valuenow="55" aria-valuemin="0" aria-valuemax="100" style="width: 55%">
    <div class="progress-bar bg-success"></div>
  </div>
  <div class="progress" role="progressbar" aria-label="Skipped"
       aria-valuenow="30" aria-valuemin="0" aria-valuemax="100" style="width: 30%">
    <div class="progress-bar bg-warning"></div>
  </div>
  <div class="progress" role="progressbar" aria-label="Failed"
       aria-valuenow="10" aria-valuemin="0" aria-valuemax="100" style="width: 10%">
    <div class="progress-bar bg-danger"></div>
  </div>
</div>

The old markup still renders, but a screen reader then hears one progress bar instead of three labelled segments.

Updating a bar from JavaScript

function setProgress(progress, percent) {
  const value = Math.round(Math.min(100, Math.max(0, percent)));
  progress.setAttribute('aria-valuenow', value);
  progress.querySelector('.progress-bar').style.width = `${value}%`;
}

const upload = document.querySelector('#upload-progress');
setProgress(upload, 72);

Bootstrap animates width changes with a short CSS transition, so frequent updates (from a file upload’s progress event, for example) look smooth without extra code.

Rendering the width from C#

If you render the bar in a Razor view, watch the culture. style="width: @(Model.Percent)%" with a double of 42.5 produces width: 42,5% on a server whose culture uses a decimal comma, such as Turkish or German. Browsers ignore that declaration and the bar shows as empty. Round to an integer, or format the value with CultureInfo.InvariantCulture:

string width = Model.Percent.ToString("0.#", CultureInfo.InvariantCulture);

Related examples

Leave a Comment