How to Create a Perfect Loading Spinner

  • CSS loading spinners rely on CSS rules and the `@keyframes` rule to create movement.
  • Performance is key. Use hardware-accelerated properties like `transform` and `opacity` for smooth, efficient animations.
  • Beyond the basic circle, patterns like dot arrays and skeleton loaders improve perceived performance and UX.
  • To use a CSS animation outside of a web page, render it visually and export the final output as a video file (MP4 or GIF).

Understanding the UX Need: Why Every Site Needs a Loading Spinner

A loading spinner is not just decoration. It is a crucial piece of user experience (UX) design. When a user clicks a button, they expect an immediate response. If the content loads slowly, silence creates anxiety.

The spinner serves two main purposes. First, it provides visual feedback, confirming that the system has received the request and is working. Second, it manages user expectations. It tells the user, "Wait, but we are doing something about it." A well-designed animated loading indicator reduces perceived wait time and keeps the user engaged.

The Basics: Creating a Classic CSS Spinner with Keyframes

The most basic spinner uses simple CSS properties and the `@keyframes` rule. This rule allows you to define how an element changes state over time, creating the illusion of motion.

For maximum performance, always use CSS transforms, such as `transform: rotate()`. These operations are handled by the GPU, ensuring the animation runs smoothly even on older devices. Avoid animating properties like `width` or `height` directly, as they can trigger costly layout recalculations.

/* Basic CSS Spinner Example */
.spinner {
    border: 4px solid #f3f3f3; /* Light grey background */
    border-top: 4px solid #3498db; /* Blue moving part */
    border-radius: 50%;
    width: 40px;
    height: 40px;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

This code creates a circular element. The `animation` property applies the `spin` animation over one second, keeping it linear, and repeating it infinitely. The `transform` property handles the rotation.

Advanced Patterns: Beyond the Circle

Relying solely on a spinning circle can feel generic. Modern interfaces use more complex, tailored animated loading indicators. Here are two effective alternatives:

Dot Spinners

Instead of a single element, use multiple small dots that appear to pulse or move sequentially. This pattern adds rhythm and complexity while remaining lightweight. You can achieve this by animating the `opacity` and `transform` of several pseudo-elements (`::before` and `::after`) in sequence.

Skeleton Loaders

Skeleton loaders are the gold standard for perceived performance. Instead of a spinner, they mimic the structure of the content that is about to load. They display grey placeholder blocks in the shape of text, images, and containers. This technique is effective because it gives the user a visual map of the content, making the wait feel purposeful rather than empty.

From Code Snippet to Video Asset: Converting Your CSS Spinner to MP4 or GIF

CSS animations live on the web. To use them in video presentations, marketing materials, or non-web platforms, you must capture them as a video file. You cannot simply copy the CSS animation into an MP4 file.

This process requires a rendering step. You must set up the animation in a controlled environment and then record or export the final visual output. Specialized tools, like those offered by Animation Machine, handle this complexity. They render the CSS animation frame by frame, allowing you to adjust the timing and loop points before exporting the finished product.

When exporting, consider the use case. For looping, a GIF is often sufficient. For high quality or integration into video timelines, MP4 is preferred. Always ensure the exported file uses efficient codecs to maintain quality while keeping the file size manageable.

Why should I use `transform` over animating `left` or `top`?

Transform properties operate on the element's drawing layer. This allows the browser to offload the rendering work to the GPU, which handles these calculations much faster than manipulating layout properties like `top` or `left`. This ensures smooth 60 frames per second performance.

What is the difference between GIF and MP4 for animations?

GIFs are limited to a simple color palette and often result in larger file sizes for complex motion. MP4 (using modern video codecs) supports millions of colors, smoother gradients, and is generally much more efficient for high-quality, looping video assets.

What is animation easing?

Easing controls the rate of change during an animation. Instead of moving at a constant speed (linear), easing allows you to define if the animation should start slowly and accelerate, or start fast and decelerate. This adds a natural, polished feel to the motion.

🎬

Ready to turn your animation into a video?

Animation Machine converts Claude Design, Lottie, GSAP, and CSS animations to MP4 or GIF in seconds. Unlimited renders, background music, 1080p output.

Get started — $5/month