Skip to main contentSkip to docs navigation

Chassis

Indicate the loading state of a component or page with spinners built with HTML and CSS only, in a rotating border and a growing variant.

@layer components
Context tokens

Introduction

Spinners indicate that a component or a page is loading. They are built with HTML and CSS only: a single element, animated by a keyframe animation and sized and colored through custom properties. Showing and hiding a spinner is left to the JavaScript of the project.

Basic structure

A spinner is one empty element with a spinner class. Add role="status" and a nested .visually-hidden label so assistive technologies announce the loading state.

Border spinner

.spinner-border draws a ring with one transparent side and rotates it.

Loading...
HTML
<div class="spinner-border" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

Growing spinner

.spinner-grow draws a filled circle that scales up from zero and fades out, repeatedly.

Loading...
HTML
<div class="spinner-grow" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

Default spinner

.spinner is an alias of one of the two variants. The Sass variable $default-spinner selects it, and is spinner-border by default.

Loading...
HTML
<div class="spinner" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

Colors

A spinner takes its color from --cx-spinner-color, which falls back to --cx-icon-color (the Sass variable $spinner-color). The .spinner-{context} classes set it to a context color.

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
HTML
<div class="spinner spinner-default" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-alternate" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-primary" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-secondary" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-neutral" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-danger" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-success" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-warning" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-info" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-black" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-white" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

Sizes

A spinner is as large as --cx-spinner-size, which falls back to --cx-icon-size (the Sass variable $spinner-size). Size classes, custom properties and the adaptive class change it.

Icon sizes

The .spinner-{size} classes set the size of a spinner to one of the icon sizes, the keys of the $spinner-sizes map.

HTML
<span class="spinner spinner-2xs"></span>
<span class="spinner spinner-xs"></span>
<span class="spinner spinner-sm"></span>
<span class="spinner spinner-md"></span>
<span class="spinner spinner-lg"></span>
<span class="spinner spinner-xl"></span>
<span class="spinner spinner-2xl"></span>
<span class="spinner spinner-3xl"></span>
<span class="spinner spinner-4xl"></span>

Custom dimensions

Set the custom properties of a spinner in a stylesheet or inline for any other dimension: --cx-spinner-size on the spinner or on a parent, and --cx-spinner-border-radius, --cx-spinner-animation-speed and --cx-spinner-border-width for the shape, the duration of a cycle and the thickness of the ring.

HTML
<div style="--cx-spinner-size: 3rem;">
  <span class="spinner-grow"></span>
  <span class="spinner-grow" style="--cx-spinner-border-radius: 0;"></span>
  <span class="spinner-border" style="--cx-spinner-animation-speed: 2s"></span>
  <span class="spinner-border" style="--cx-spinner-border-width: .25rem;"></span>
</div>

Adaptive

.spinner-adaptive sets the size of a spinner to 1em, so it follows the font size of its parent and matches the text next to it.

Heading 1

Heading 2

Heading 3

HTML
<p class="font-h1">
  <span class="spinner spinner-adaptive"></span>
  Heading 1
</p>
<p class="font-h2">
  <span class="spinner spinner-adaptive"></span>
  Heading 2
</p>
<p class="font-h3">
  <span class="spinner spinner-adaptive"></span>
  Heading 3
</p>

Opacity

The color classes of a spinner carry a separate alpha channel, so the opacity of a context color changes at runtime without a new color class.

How it works

A .spinner-{context} class derives --cx-spinner-color from the context color with the CSS relative color syntax, and reads the alpha channel from --cx-spinner-opacity, which defaults to 1. A .spinner-opacity-* class sets that property on the element.

CSS
.spinner {
  --cx-color: var(--cx-spinner-color, var(--cx-icon-color));
}
.spinner-primary {
  --cx-spinner-color: oklch(from var(--cx-primary) l c h / var(--cx-spinner-opacity, 1));
}
.spinner-opacity-subtle {
  --cx-spinner-opacity: 0.5;
}

Opacity values

Override --cx-spinner-opacity in a stylesheet or inline, or apply a .spinner-opacity-* class. The classes are generated from the keys of the $spinner-opacities map: a numeric class such as .spinner-opacity-50 exists only when the map holds that key. The opacity applies to spinners with a .spinner-{context} class only.

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
HTML
<span class="spinner"></span>
<span class="spinner spinner-primary" style="--cx-spinner-opacity: .6;"></span>

<span class="vr mx-md"></span>

<div class="spinner spinner-primary spinner-opacity-100" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-primary spinner-opacity-75" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-primary spinner-opacity-50" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-primary spinner-opacity-25" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-primary spinner-opacity-10" role="status">
  <span class="visually-hidden">Loading...</span>
</div>
<div class="spinner spinner-primary spinner-opacity-0" role="status">
  <span class="visually-hidden">Loading...</span>
</div>

Context opacity

$spinner-opacities is the map of the icon opacities by default, so .spinner-opacity-subtle and .spinner-opacity-slight match the subtle and slight icons of a context.

HTML
<span class="spinner-border"></span>
<span class="spinner-border spinner-primary spinner-opacity-subtle"></span>
<span class="spinner-border spinner-primary spinner-opacity-slight"></span>

<span class="vr mx-md"></span>

<span class="spinner-grow"></span>
<span class="spinner-grow spinner-primary spinner-opacity-subtle"></span>
<span class="spinner-grow spinner-primary spinner-opacity-slight"></span>

Buttons

A spinner inside a .button indicates that the action is in progress. It takes the icon size and the icon color of the button, since both of its custom properties fall back to the ones of icons. Hide the spinner with aria-hidden="true" and label the button: with visible text, or with aria-label when the spinner is its only content.

HTML
<button class="button default" type="button" aria-label="Loading">
  <span class="spinner-border" aria-hidden="true"></span>
</button>
<button class="button default" type="button" disabled>
  <span class="spinner-border" aria-hidden="true"></span>
  Loading...
</button>
<button class="button primary lg" type="button" aria-label="Loading">
  <span class="spinner-border" aria-hidden="true"></span>
</button>
<button class="button primary lg" type="button" disabled aria-label="Loading">
  <span class="spinner-border" aria-hidden="true"></span>
</button>
<button class="button secondary sm" type="button" aria-label="Loading">
  <span class="spinner-grow" aria-hidden="true"></span>
</button>
<button class="button secondary sm" type="button" disabled>
  <span class="spinner-grow" aria-hidden="true"></span>
  Loading...
</button>

Positioning

Spinners are display: inline-block elements, so they sit in a line of text and are placed with the spacing, flexbox, float and text alignment utilities.

Margin

Margin utilities such as .m-xl add space around a spinner.

HTML
<span class="spinner-border m-xl"></span>

Flex

Flexbox utilities center a spinner in its container, or push it to the end of a row next to a status text.

Loading...
HTML
<div class="d-flex justify-content-center">
  <div class="spinner-border" role="status">
    <span class="visually-hidden">Loading...</span>
  </div>
</div>
Loading...
HTML
<div class="d-flex align-items-center">
  <strong role="status">Loading...</strong>
  <div class="spinner-border ms-auto" aria-hidden="true"></div>
</div>

Floats

Float utilities move a spinner to one side of its container.

Loading...
HTML
<div class="clearfix">
  <div class="spinner-border float-end" role="status">
    <span class="visually-hidden">Loading...</span>
  </div>
</div>

Text align

Text alignment utilities position a spinner as they position inline content.

Loading...
HTML
<div class="text-center">
  <div class="spinner-border" role="status">
    <span class="visually-hidden">Loading...</span>
  </div>
</div>

Accessibility

A spinner has no text content, so its loading state reaches assistive technologies only through its markup. A standalone spinner carries role="status", which makes it a polite live region, and a nested <span class="visually-hidden">Loading...</span> as the announced text. A decorative spinner next to a visible status text, or inside a labeled button, carries aria-hidden="true" instead. Some examples on this page leave both out to keep the markup on the class they demonstrate.

With $enable-reduced-motion on, a spinner animates at half its speed under prefers-reduced-motion: reduce and does not stop, since a still spinner no longer reads as loading.

CSS

This component can be customized through Sass variables at compile time.

Custom properties

This component exposes CSS custom properties to control its appearance at runtime.

Spinners read --cx-spinner-color, --cx-spinner-opacity, --cx-spinner-size, --cx-spinner-vertical-align, --cx-spinner-border-radius, --cx-spinner-animation-speed and, for .spinner-border, --cx-spinner-border-width. None of them is set by default: each falls back to its Sass variable.

Sass variables

This component uses Sass variables in scss/config/_defaults.scss to define its defaults.

// Sass only variable to set spinner style for `.spinner` class.
$default-spinner:           spinner-border; // or spinner-grow
// These variables are also reserved as CSS variables.
$spinner-color:             var(--icon-color);
$spinner-size:              var(--icon-size);
$spinner-vertical-align:    -.125em;
$spinner-border-width:      calc(var(--size) / 6);
$spinner-animation-speed:   .75s;
// See scss/maps/_index.scss for $spinner-sizes and $spinner-opacities variables.

The size and opacity classes are generated from two maps, which default to the maps of icons.

$spinner-sizes:     $icon-sizes;
$spinner-opacities:   $icon-opacities;

Utilities

The color, opacity and size classes of spinners are utilities, defined in scss/utilities/_spinner.scss.

"spinner-color": (
  property: --spinner-color,
  class: spinner,
  values: map.merge(
    map-loop($context-colors, opacity-var, #{"$key"}, #{"spinner"}),
    ( "transparent": transparent )
  )
),
"spinner-opacity": (
  property: --spinner-opacity,
  class: spinner-opacity,
  values: $spinner-opacities
),
"spinner-size": (
  property: --spinner-size,
  class: null,
  values: map-rename-keys($spinner-sizes, "spinner-", "start")
),

Keyframes

Each variant has its own keyframe animation, defined in scss/_spinners.scss.

@keyframes spinner-border {
  to { transform: rotate(360deg); }
}
@keyframes spinner-grow {
  0% {
    transform: scale(0);
  }
  50% {
    opacity: 1;
    transform: none;
  }
}