Build a spinner, and provide a static alternative.
What to build
A keyframe animation with a reduced motion fallback.
Requirements
- A
@keyframesrule calledspinthat rotates from0degto360deg. .spinneris32pxby32px, withborder-radius: 50%, a4px solid lightgrayborder, and aborder-top-colorofteal..spinnerplaysspinover1s,linear, forever.- Under
prefers-reduced-motion: reduce,.spinnerhasanimation: none.
Constraints
Do not use any images.
Example
The second code block in the lesson shows @keyframes.
Why it works
A coloured top border on a round box looks like a moving arc when rotated, and the query ensures that those who asked for less motion are not shown it.
Hints
Hint 1
A spinner rotates from 0 to 360 degrees.
Hint 2
animation is shorthand for name, duration, timing and count.
Hint 3
Turn it off inside the reduced motion query.
Solution
Show one correct solution (try it yourself first)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Spinner</title>
<style>
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.spinner {
width: 32px;
height: 32px;
border-radius: 50%;
border: 4px solid lightgray;
border-top-color: teal;
animation: spin 1s linear infinite;
}
@media (prefers-reduced-motion: reduce) {
.spinner { animation: none; }
}
</style>
</head>
<body>
<div class="spinner" role="status" aria-label="Loading"></div>
</body>
</html>