Skip to content
Tutorials Center
My learning

Topics

Lesson 95 of 139 Β· UI Components

Spinners, Loaders and Skeletons

Build CSS loading spinners, styled progress bars and shimmering skeleton screens, and make them accessible with role="status", aria-busy and reduced motion.

Waiting is part of every app. Search results take a moment, a file uploads, a dashboard fetches its numbers. During that time, the page has one job: reassure people that something is happening and, if possible, how long it will take. A blank screen or a frozen button makes users click again, reload, or leave.

CSS gives you everything you need to build loading indicators without images or libraries. In this lesson you will build spinners, style progress bars, and create skeleton screens, the gray placeholder shapes that many modern sites show while content loads. You will also make them accessible and respectful of people who prefer less motion.

Which indicator when?

Indicator Use it when Example
Spinner The wait is short and you don't know how long it will be Submitting a form, loading a small panel
Progress bar You know how much is done (determinate) or the task is long (indeterminate) File upload, installation step
Skeleton screen You're loading content whose layout you already know A feed of cards, a profile page

Some timing rules of thumb help too. Under about 300 ms, show nothing: an indicator that flashes for a split second feels more jarring than the wait. From there to a few seconds, a spinner or skeleton is fine. For anything longer, show real progress if you can, or at least a message about what's happening.

Spinners

The classic CSS spinner is a circle with a border on all sides, where one side has a different color, rotated forever with an animation:

CSS
.spinner {
  width: 2.5rem;
  aspect-ratio: 1;
  border: 4px solid #e2e8f0;
  border-top-color: #4f46e5;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to { rotate: 1turn; }
}

linear timing matters: with the default ease, the spinner would speed up and slow down every turn, which looks like stuttering. 1turn is the same as 360deg but easier to read.

A smoother look uses a conic gradient that fades from transparent to solid, with a mask that cuts out the middle to make a ring. And a third style uses three dots that pulse one after another, thanks to staggered animation-delay values.

Three CSS spinners

All three animate only rotate, scale and opacity. Those properties can be handled by the compositor, so the spinner keeps turning smoothly even while JavaScript is busy loading data. Motion and Performance explains why.

Making spinners accessible

A spinning circle means nothing to a screen reader. Two pieces of markup fix that:

HTML
<div role="status">
  <span class="spinner" aria-hidden="true"></span>
  <span class="visually-hidden">Loading results…</span>
</div>
  • role="status" creates a polite live region: when text appears or changes inside it, screen readers announce it without interrupting. Put the status element in the page before loading starts, then add or change the text. A live region inserted together with its text is often not announced.
  • Visually hidden text gives the announcement words. The spinner itself is decorative, so it gets aria-hidden="true". The .visually-hidden class is explained in Accessible CSS.

When loading finishes, update the same status element ("12 results loaded") or clear it.

Spinners inside buttons

A common pattern shows a small spinner inside a button after it's clicked, so people don't click twice. Size the spinner in em units and give it border-color: currentColor with a transparent side, and it fits any button size and color automatically.

Progress bars

When you know how far along a task is, show it. HTML has a dedicated element for that, <progress>, which screen readers announce with its percentage:

HTML
<progress value="0.65">65%</progress>  <!-- determinate -->
<progress>Loading…</progress>           <!-- indeterminate: no value -->

The quickest way to brand it is one line, accent-color, covered in Styling Forms. For full control you need appearance: none and some vendor pseudo-elements, because the parts of a progress bar aren't standardized yet:

Part Chromium and Safari Firefox
The track ::-webkit-progress-bar the <progress> element itself
The filled part ::-webkit-progress-value ::-moz-progress-bar

Write the two filled-part selectors as separate rules. A selector list with a pseudo-element the browser doesn't know is invalid as a whole, so combining them with a comma would break both.

The indeterminate state (no value) can be targeted with :indeterminate. You can draw your own animated stripe for it:

Styled progress bars

Each bar has a visible <label>, so it's announced with a name ("Uploading photos.zip, 35 percent"). Use the native element whenever you can; if you build a bar from <div>s, you must add role="progressbar" with aria-valuenow, aria-valuemin, aria-valuemax and a label yourself.

Skeleton screens

A skeleton screen shows the shape of the content before the content itself: gray bars where text will be, a circle where the avatar goes, a block for the image. It makes waiting feel shorter, and when the real content arrives, nothing jumps around, because the skeleton already reserved the right space.

The shimmer effect is a light band that sweeps across the gray shapes. It's a gradient wider than the element, with its background-position animated from one side to the other:

CSS
.skeleton {
  background: linear-gradient(90deg, #e2e8f0 40%, #f1f5f9 50%, #e2e8f0 60%)
              0 0 / 300% 100%;
  animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer {
  from { background-position: 100% 0; }
  to   { background-position: 0 0; }
}

Use background-attachment: fixed on all skeleton pieces if you want the shimmer to move across them as one wave rather than separately in each bar.

A skeleton card that loads

The real content lands in exactly the same grid as the skeleton, so the card doesn't change size. That's the whole point: no layout shift when the data arrives.

aria-busy and hiding the bones

Skeletons are visual only. Screen reader users shouldn't hear a dozen empty shapes, so each piece gets aria-hidden="true". On the container, aria-busy="true" tells assistive technology that the region is being updated and it may want to wait before reading it. Set it back to false when the content is ready.

aria-busy is also a convenient styling hook: [aria-busy="true"] .content { opacity: 0.5; } dims a list while it refreshes, keeping the state in one place for both CSS and assistive technology.

Don't over-skeleton. Skeletons work best for content with a predictable layout that loads in a second or two. For tiny pieces, a spinner is simpler. For waits longer than a few seconds, add a message, or people may assume the page is broken.

Reduced motion

Some people get dizzy or nauseous from motion on screen, and their operating system lets them say so. The prefers-reduced-motion media query passes that preference to CSS. For loading indicators, the rule is subtle: the indicator must still show that something is happening. Removing a spinner's animation completely would leave a frozen circle that looks like an error.

Good options:

  • Slow it down rather than stopping it: a gentle rotation every 2 or 3 seconds.
  • Swap movement for a fade: replace a sweep or slide with a soft opacity pulse.
  • Stop decorative shimmer on skeletons, as the demo does; the gray shapes alone still say "loading".
CSS
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.5s; }
  .skeleton { animation: none; }
  progress:indeterminate { animation-duration: 3s; }
}

Delaying a spinner with CSS

To avoid the flash of a spinner on fast responses, hide it for the first few hundred milliseconds. An animation with a delay and animation-fill-mode: both does it without any timer code:

A spinner that waits before appearing

Click Search a few times. Fast responses (250 ms) finish before the spinner fades in, so you only see the result. Slow ones (1.8 s) show the spinner after 400 ms. The both fill mode keeps the spinner invisible during the delay. Note that the status element exists before loading starts, so screen readers announce the text changes.

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. Why should a spinner's rotation use linear timing?

    An infinite rotation needs constant speed. Easing curves speed up and slow down within each turn.
  2. 2. What does aria-busy set to true on a container tell assistive technology?

    aria-busy signals an update in progress. Set it back to false when the content is ready.
  3. 3. Why put each skeleton shape in aria-hidden="true"?

    Skeletons are a visual hint only. The container's aria-busy and a status message carry the information.
  4. 4. When a user prefers reduced motion, what is the best change for a loading spinner?

    The indicator must still communicate that work is happening. A static spinner looks like a frozen page.
  5. 5. Why should the progress bar pseudo-elements for Chromium and Firefox be written as separate rules?

    If one selector in a comma-separated list is invalid for a browser, the browser drops the entire rule.

Summary

  • Pick spinners for short unknown waits, progress bars when you can measure progress, and skeletons for content with a known layout; show nothing for waits under about 300 ms.
  • Build spinners from a bordered circle, a masked conic gradient or staggered dots, animating only rotate, scale and opacity with linear timing for rotation.
  • Announce loading with a role="status" region that exists before loading begins, plus visually hidden text; mark decorative spinners aria-hidden="true".
  • Use the native <progress> element; style it with appearance: none and separate ::-webkit-progress-value and ::-moz-progress-bar rules, and target the unknown state with :indeterminate.
  • Skeletons reserve space with gray shapes and a moving gradient shimmer; hide them from screen readers and set aria-busy on the container.
  • Under prefers-reduced-motion, slow or soften indicators instead of freezing them, and delay spinners with an animation delay to avoid flashes.

Next: User Interface Properties, where you control cursors, text selection, scrollbars and native form styling.