Code snippets
CSS Loading Spinner Examples: Ring, Dots and Skeleton
A CSS loading spinner is an element with a looping keyframe animation, usually a rotating bordered circle, paired with visible or screen-reader text that says what is loading. Animate only transform and opacity, honour prefers-reduced-motion, and use a skeleton instead of a spinner when you already know the shape of the content that is coming.
By CodingMarble · Published · 5 min read

What a CSS spinner actually is
A loading indicator tells a visitor that something is happening and that the page has not frozen. You do not need an image, a GIF or a JavaScript library for this. A CSS spinner is an ordinary element, a few rules to give it a shape, and a @keyframes block that the browser repeats forever with the animation property.
Three pieces are always present. The shape is built from borders, border-radius and background colours. The motion is a keyframes rule that changes transform or opacity. The meaning is text that assistive technology can read, because a spinning circle carries no information for someone using a screen reader. This guide builds three indicators: a rotating ring, a three-dot pulse and a skeleton shimmer, and then covers the accessibility and performance rules that apply to all of them.
The rotating ring
The classic spinner is a circle with a transparent border and one coloured side. Rotating it makes the coloured side appear to chase around the circle. The markup wraps the ring and a text label in a container with role="status", which is a live region: when its text changes, screen readers announce it politely without moving focus.
<div class="spinner" role="status">
<span class="spinner__ring" aria-hidden="true"></span>
<span class="spinner__text">Loading your orders</span>
</div>.spinner {
display: inline-flex;
align-items: center;
gap: 0.75rem;
color: #1f2937;
}
.spinner__ring {
width: 1.25rem;
height: 1.25rem;
box-sizing: border-box;
border: 3px solid #d1d5db;
border-top-color: #2563eb;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}Reading the CSS from the top: inline-flex with a gap lines the ring up beside its label. The ring gets equal width and height, and border-radius: 50% turns the square into a circle. box-sizing: border-box keeps the 3px border inside the stated size so the dimensions stay predictable. Three sides of the border use a pale grey and only the top uses blue, which is what creates the visible gap.
The animation shorthand reads as name, duration, timing function and iteration count. The linear timing function matters here. The default is ease, which slows down at the end of each cycle and makes a spinner look like it stutters once per turn. The keyframes only need a to step because the from state defaults to the element's current style. To customise, change the width and height for size, the border thickness for weight, and the two colours for your palette.
The three-dot pulse
Dots suit inline places such as a button label or a chat bubble where a ring feels heavy. All three dots share one keyframes rule. Each gets a different animation-delay so the pulse travels from left to right. The first dot has no delay, so the animation starts immediately.
<div class="dots" role="status">
<span class="dots__dot" aria-hidden="true"></span>
<span class="dots__dot" aria-hidden="true"></span>
<span class="dots__dot" aria-hidden="true"></span>
<span class="visually-hidden">Sending your message</span>
</div>.dots {
display: inline-flex;
gap: 0.375rem;
}
.dots__dot {
width: 0.625rem;
height: 0.625rem;
border-radius: 50%;
background: #2563eb;
animation: pulse 1.2s ease-in-out infinite;
}
.dots__dot:nth-child(2) { animation-delay: 0.15s; }
.dots__dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes pulse {
0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
40% { transform: scale(1); opacity: 1; }
}
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}The keyframes list 0%, 80% and 100% together because the dot should rest at its small, faded state for most of the cycle and swell only around the 40% mark. The delays are a fraction of the total duration, so the three peaks overlap slightly and read as one wave. The visually-hidden class removes the label from view while leaving it in the accessibility tree, which is why it is preferred over display: none, as that would hide it from screen readers too.
The skeleton shimmer
A skeleton is a grey placeholder drawn in the shape of the content that will replace it. A light band sweeps across it to show that loading is in progress. The trick is a wide gradient background that is moved with background-position.
.skeleton {
display: block;
min-height: 1rem;
border-radius: 0.375rem;
background-color: #e5e7eb;
background-image: linear-gradient(
90deg,
transparent 0%,
rgba(255, 255, 255, 0.7) 50%,
transparent 100%
);
background-size: 200% 100%;
background-repeat: no-repeat;
animation: shimmer 1.4s ease-in-out infinite;
}
@keyframes shimmer {
from { background-position: 200% 0; }
to { background-position: -200% 0; }
}Apply the class to empty block elements and size them with width and height: a short line for a title, three longer lines for a paragraph, a square for an avatar. The gradient is transparent at both ends so the grey base colour shows through, with a translucent white band in the middle. Animating background-position does cause repainting, unlike transform, but for a handful of placeholder blocks that is acceptable. If you have dozens, place the highlight on a pseudo-element and animate its transform instead.
Accessibility and reduced motion
Constant motion can cause discomfort for people with vestibular disorders, and operating systems let users ask for less of it. The prefers-reduced-motion media query exposes that setting to your CSS. The safest response for a loading indicator is to replace spinning with something gentler, such as a slow fade, so the visitor still sees that work is in progress.
@media (prefers-reduced-motion: reduce) {
.spinner__ring {
animation: fade 2s ease-in-out infinite;
}
.dots__dot {
animation: fade 2s ease-in-out infinite;
transform: none;
}
.skeleton {
animation: none;
background-image: none;
}
}
@keyframes fade {
0%, 100% { opacity: 0.35; }
50% { opacity: 1; }
}- Put role="status" on a container that is already in the page before the text inside it changes. Live regions announce changes, and a region inserted together with its text is not always announced.
- Keep the label specific. "Loading your orders" is better than "Loading" because it tells the user what they are waiting for.
- When loading finishes, replace the label text with the result, or remove the indicator and move focus only if the user's task requires it.
- Check contrast: the coloured part of a ring is a graphical object and should have enough contrast against its background to be seen clearly.
- For content areas, mark the container with aria-busy="true" while it loads and remove the attribute when it is ready.
Performance: animate transform and opacity
Browsers can animate transform and opacity without recalculating layout or repainting the element in most cases, because those changes can be handed to the compositor. Properties such as width, height, top, left, margin and box-shadow force more work on every frame. A spinner runs for as long as the user waits, which is often during heavy network and script activity, so it should be as cheap as possible.
That is why the ring rotates with transform rather than animating a border angle, and why the dots scale instead of changing their width. Avoid adding will-change to every indicator on the page by default. It reserves resources, and it is meant for elements you know will animate soon. A single small spinner does not need it.
Also remove indicators when they are no longer needed. An infinite animation on an element that is hidden with opacity: 0 may keep running. Use the hidden attribute or remove the element from the DOM once the content has arrived.
When a skeleton beats a spinner
Use a spinner when the outcome has no predictable shape: submitting a form, saving a setting, running a search whose results could be anything. It communicates only that work is under way.
Use a skeleton when you know the layout that will appear: a feed of cards, a profile page, a table. The placeholder reserves the exact space, which prevents the page from jumping when real content arrives, and it shows the user what kind of content is coming. Whichever you choose, leave the indicator out entirely for waits that are very short, since a spinner that flashes for a fraction of a second is more distracting than no indicator at all. A small delay of a few hundred milliseconds before showing it is a common approach.
Keep reading
- Code snippetsCSS Button Hover Effects: Fill Slide, Press and FocusA good CSS button has distinct hover, focus-visible, active and disabled states, each changing only cheap properties like background, transform and box-shadow. Build the base button first, add a pseudo-element for the fill-slide hover, wrap hover in a media query for devices that can hover, and never remove the focus ring without replacing it.
- Web developmentCore Web Vitals Explained: LCP, INP and CLSCore Web Vitals are three measurements of real user experience: how fast the main content appears (LCP), how quickly the page responds to input (INP) and how much the layout jumps (CLS). Each has a published threshold, a short list of usual causes and a specific fix.
- Code snippetsDark Mode With CSS Variables: A Practical GuideDefine your colours once as CSS custom properties on :root, override them inside a prefers-color-scheme: dark query, and let a data-theme attribute on the html element take priority when the visitor picks a theme manually. A tiny inline script in the head applies the saved choice before the first paint so the page never flashes the wrong theme.
Need this built properly for your business?
CodingMarble is a remote studio in India that builds websites, apps and games on fixed packages with the scope written down before payment. Try the free tools we run, or start a project when you are ready.