CSS Loading Spinners
This example demonstrates how to create loading spinners and animated loading indicators using HTML and CSS. Explore different spinner sizes, speeds, border styles, colors, and animation techniques using the CSS @keyframes rule.
Basic Loading Spinner
A circular spinner rotates continuously to indicate that a process is in progress.
The spinner uses a circular border with one differently colored side.
Small Loading Spinner
A smaller spinner can be used inside buttons or compact interface elements.
The width, height, and border thickness are reduced.
Large Loading Spinner
A larger spinner can be used to indicate loading in a prominent content area.
The spinner measures 85 pixels wide and high.
Fast Loading Spinner
The animation-duration property controls how quickly the spinner rotates.
This spinner completes one rotation every 0.5 seconds.
Slow Loading Spinner
A longer animation duration creates a slower rotation.
This spinner completes one rotation every 2 seconds.
Double Border Spinner
Two colored border sections create a different rotating pattern.
The top and bottom borders are colored while the other borders are transparent.
Dotted Loading Spinner
The border-style: dotted declaration creates a circular pattern of dots.
The dotted border rotates using the same animation as the basic spinner.
Dashed Loading Spinner
The border-style: dashed declaration creates a segmented spinner.
The dashed border creates a rotating pattern of short segments.
Multicolor Loading Spinner
Different border colors create a colorful rotating indicator.
Each side of the circular border uses a different color.
Pulsing Loading Indicator
The scale() function changes the size of a circular element.
The circle grows and shrinks while its opacity changes.
Bouncing Dots Indicator
Three circles move vertically with staggered animation delays.
The animation-delay property creates a wave-like bouncing effect.
Animated Loading Bars
Five vertical bars change height to create an animated loading indicator.
Each bar uses a different animation delay to create continuous movement.
Dual Ring Spinner
Two circular borders rotate in opposite directions.
The ::before and ::after pseudo-elements create the two rings.
Interactive Loading Button
Select the button below to simulate a loading process. The button displays a spinner while the demonstration is running, then displays a completion message.
Start Loading
Select the button to begin the loading demonstration.
This demonstration uses JavaScript to simulate a three-second process. No external data is loaded.
Try It: Compare the different spinner styles, sizes, and speeds. Select the Start Loading button to see a spinner appear during a simulated process. Change the animation duration, border colors, and dimensions to experiment with different loading indicators.
How CSS Loading Spinners Work
CSS loading spinners use animations to indicate that an operation is in progress.
A basic spinner can be created using a circular element with a colored border and a rotation animation.
The following CSS creates a basic loading spinner:
.spinner {
width: 55px;
height: 55px;
border: 6px solid #b5cfdf;
border-top-color: #146c94;
border-radius: 50%;
animation: spinner-rotate 1s linear infinite;
}
The border-radius: 50% declaration creates a circular shape.
The differently colored top border makes the rotation visible.
The animation property applies the rotation continuously.
Using CSS @keyframes
The CSS @keyframes rule defines the stages of an animation.
For a rotating spinner, the element rotates from 0 degrees to 360 degrees.
@keyframes spinner-rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
The browser interpolates the rotation between the starting and ending values.
When combined with an infinite animation iteration count, the spinner rotates continuously.
CSS Animation Properties
Several CSS properties control how loading animations behave.
The animation-name property identifies the keyframes to use.
The animation-duration property specifies how long one animation cycle takes.
The animation-timing-function property controls the rate of change during the animation.
The animation-iteration-count property controls how many times the animation repeats.
The animation-delay property specifies how long to wait before an animation begins.
The animation-direction property controls whether the animation runs forward, backward, or alternates direction.
The animation shorthand property combines several animation settings into a single declaration.
animation: spinner-rotate 1s linear infinite;
This declaration runs the rotation animation continuously, completing one cycle every second at a constant speed.
Customizing Spinner Sizes and Speeds
The width and height properties control the overall spinner dimensions.
The border-width property controls the thickness of the circular border.
The animation-duration property controls the rotation speed.
For example, the following CSS creates a smaller and faster spinner:
.spinner-small {
width: 30px;
height: 30px;
border-width: 4px;
animation-duration: 0.5s;
}
Smaller spinners work well inside buttons, while larger spinners may be more appropriate for full-page loading indicators.
Creating Different Loading Animations
Loading indicators do not have to use circular rotation.
The scale() function can create pulsing circles.
The translateY() function can create bouncing dots.
The scaleY() function can create animated vertical bars.
Animation delays allow several elements to move at different times, creating coordinated effects.
Pseudo-elements can also be used to create multiple animated layers without adding extra HTML elements.
Loading Spinner Accessibility
Loading indicators should communicate their purpose through text as well as animation.
Decorative spinner elements can use aria-hidden="true" to prevent unnecessary announcements by assistive technology.
A nearby status message can explain whether an operation is starting, running, or complete.
The interactive example uses role="status" and aria-live="polite" to announce changes to the loading message.
The prefers-reduced-motion media query disables spinner animations for visitors who request reduced motion.
When motion is disabled, visible text should still communicate the current loading status.
A loading indicator should not remain visible indefinitely without providing meaningful feedback about the operation.
Customizing CSS Loading Spinners
You can customize loading spinners by changing their dimensions, border colors, border styles, and animation settings.
Modify the animation-duration value to make a spinner rotate faster or slower.
Use different border-color values to create multicolor spinners.
Change the border-style property to create dotted or dashed spinner designs.
Combine transforms, opacity changes, and animation delays to create additional loading effects.
For a consistent website design, use similar loading indicator colors, sizes, and animation speeds throughout your interface.