A loading spinner doesn't have to be a grey circle. In this tutorial, we'll build a fun spinner from the twelve clock face emojis, so the hands appear to sweep round, using nothing but CSS. Then we'll make a few more variations and make sure they work well for everyone.
How it works
The trick is to animate the content property of a ::before pseudo-element (an extra piece of an element that CSS can create and fill with text). Each keyframe of the animation swaps in a different emoji, so the browser shows one clock face, then the next, and so on, like the frames of a flip book.
Some properties, like content, can't blend smoothly between two values, because there's no "halfway" between two emojis. They're called discrete properties, and they simply switch from one value to the next.
Step 1: choose your emojis
There are twelve clock face emojis, one for each hour:
ππππππππππππ
They run from 12 o'clock (U+1F55B) round to 11 o'clock (U+1F55A). Any sequence of emojis that looks like movement works well: phases of the moon, a spinning globe, or weather changing. You can find a complete list of emojis over at CharacterCodes.net.
Step 2: the HTML
<div class="loading" role="status">
<span class="loader" aria-hidden="true"></span>
<span class="visually-hidden">Loadingβ¦</span>
</div>
.loaderis the element that displays the emoji.aria-hidden="true"stops screen readers (software that reads pages aloud) from reading out each clock face as it changes, such as "twelve o'clock, one o'clockβ¦".- The "Loadingβ¦" text tells screen reader users what's happening instead. It's hidden visually (see the CSS below), but still read aloud.
role="status"makes it a live region, so screen readers announce it when it appears, without interrupting what the person is doing.
Step 3: the CSS
The keyframes
@keyframes clock {
0% { content: "π"; }
8.333% { content: "π"; }
16.667% { content: "π"; }
25% { content: "π"; }
33.333% { content: "π"; }
41.667% { content: "π"; }
50% { content: "π"; }
58.333% { content: "π"; }
66.667% { content: "π"; }
75% { content: "π"; }
83.333% { content: "π"; }
91.667% { content: "π"; }
}
Twelve emojis share the animation equally, so each keyframe is 100 Γ· 12 = 8.333% after the last. There's no need for a 100% keyframe: when the animation repeats, it goes straight back to 12 o'clock at 0%.
The spinner
.loader {
display: inline-block;
width: 1.2em;
height: 1.2em;
font-size: 3rem; /* the size of the emoji */
line-height: 1.2;
text-align: center;
}
.loader::before {
content: "π"; /* shown before the animation starts */
display: block;
animation: clock 1.5s step-end infinite;
}
/* Hide text visually, but keep it for screen readers */
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
1.5sis how long a full lap of the clock takes, so each hour shows for 0.125 seconds.step-endholds each emoji until the very end of its slot, then jumps straight to the next. Every clock face gets exactly the same amount of time on screen.infiniterepeats the animation for as long as the spinner is on the page.- The fixed width and height stop the spinner from changing size slightly as the emoji changes, which could make nearby content jiggle.
More emoji spinners
The same technique works with any sequence of emojis, and you can also animate a single emoji with transform. Here are some ideas:
Moon phases
@keyframes moon {
0% { content: "π"; }
12.5% { content: "π"; }
25% { content: "π"; }
37.5% { content: "π"; }
50% { content: "π"; }
62.5% { content: "π"; }
75% { content: "π"; }
87.5% { content: "π"; }
}
.loader-moon::before {
content: "π";
animation: moon 1.6s step-end infinite;
}
Eight phases means each keyframe is 100 Γ· 8 = 12.5% apart. The globe works the same way with three emojis (πππ), spaced 33.333% apart.
Flipping hourglass
@keyframes flip {
0%, 40% { transform: rotate(0deg); }
50%, 90% { transform: rotate(180deg); }
100% { transform: rotate(360deg); }
}
.loader-hourglass::before {
content: "β³";
animation: flip 2s ease-in-out infinite;
}
This one rotates a single emoji instead of swapping emojis. Holding each position for a while (from 0% to 40%, and from 50% to 90%) creates a pause before each flip, just like turning over a real hourglass.
Bouncing dots
<span class="loader-dots" aria-hidden="true">
<span>π΅</span><span>π΅</span><span>π΅</span>
</span>
@keyframes bounce {
0%, 60%, 100% { transform: translateY(0); }
30% { transform: translateY(-0.4em); }
}
.loader-dots span {
display: inline-block; /* transforms don't work on inline elements */
animation: bounce 1.2s ease-in-out infinite;
}
.loader-dots span:nth-child(2) { animation-delay: 0.15s; }
.loader-dots span:nth-child(3) { animation-delay: 0.3s; }
Giving each dot a slightly later animation-delay makes them bounce one after another, like a wave.
Accessibility
Respect reduced motion
Some people find movement on screen uncomfortable, and turn on a "reduce motion" setting on their device. For those visitors, slow the emoji swaps right down and stop the moving spinners, while still showing that something is loading:
@media (prefers-reduced-motion: reduce) {
.loader::before {
animation-duration: 6s; /* a slow, calm tick */
}
.loader-hourglass::before,
.loader-dots span {
animation: none; /* still visible, just not moving */
}
}
Show and hide the spinner properly
A spinner should only be on the page while something is actually loading. Add it when the work starts, and remove it (or add the hidden attribute) when it's finished, so screen readers announce the loading message at the right moment. Press the button to try it:
button.addEventListener('click', async () => {
button.disabled = true;
status.textContent = '';
spinner.hidden = false; // show the spinner
await loadTheData(); // your loading code here
spinner.hidden = true; // hide it again
status.textContent = 'Loaded!'; // let everyone know it worked
button.disabled = false;
});
Good to know: emojis look different on every device, because each operating system draws its own. Test your spinner on a few devices, and pick emojis that still make sense whichever style appears. Also, unlike ordinary text, colour emojis ignore the CSS color property, so you can't recolour them to match your brand.
Customising your spinner
- Speed: change the animation duration.
1sspins faster, and3sis more relaxed. - Size: change the
font-sizeon.loader. Because the width and height useemunits, they grow with it. - Inside text or buttons: set
font-size: 1emto match the surrounding text, as in this button: Saving⦠- Your own emojis: any sequence works. Count your emojis, divide 100 by that number, and space the keyframes by the result.
Wrapping up
By animating the content property with step-end, you can turn any sequence of emojis into a playful loading spinner with just a few lines of CSS. Keep the emojis hidden from screen readers with a proper "Loading" message instead, give the spinner a fixed size, and calm things down for people who prefer reduced motion.