Educational Article

What Is CSS Animation? CSS animation is a way to make HTML elements move, change, fade, rotate, scale, or visually transform over time using only CS...

whatcssanimation?

What Is CSS Animation?


CSS animation is a way to make HTML elements move, change, fade, rotate, scale, or visually transform over time using only CSS. Instead of relying on JavaScript for every visual effect, CSS lets you define animations declaratively with properties like animation, transition, transform, and @keyframes.


In this guide, you’ll learn what CSS animation is, how it works, when to use it, and how to write practical animations that improve user experience without making your website feel distracting or slow.


How CSS Animation Works

Free Tool

Image to Base64

Convert an image to a Base64 data URI and back — copy, embed in CSS/HTML, or decode

Try it free

CSS animation works by changing an element’s styles over a period of time. For example, a button might slowly change color when hovered, a card might slide into view, or a loading spinner might rotate continuously.


There are two main ways to create motion in CSS:


  • Transitions
  • Keyframe animations

  • They are related, but they solve slightly different problems.


    CSS Transitions


    A CSS transition animates a change from one state to another. Usually, the change is triggered by something like hover, focus, or adding a class.


    Example:


    cssCODE
    .button {
      background: blue;
      transition: background 0.3s ease;
    }
    
    .button:hover {
      background: purple;
    }

    Here, the browser smoothly animates the button’s background color from blue to purple over 0.3s.


    Transitions are best for simple state changes, such as:


  • Hover effects
  • Focus effects
  • Expanding menus
  • Changing opacity
  • Scaling buttons or cards

  • CSS Keyframe Animations


    Keyframe animations are more powerful. They let you define multiple stages of an animation using @keyframes.


    Example:


    cssCODE
    @keyframes fadeIn {
      from { opacity: 0; }
      to { opacity: 1; }
    }
    
    .card {
      animation: fadeIn 0.5s ease-in;
    }

    This animation makes the .card element fade in over half a second.


    Keyframes are useful when you need:


  • Repeating animations
  • Multi-step movement
  • Loading indicators
  • Entrance effects
  • Attention-grabbing UI hints

  • The Core CSS Animation Properties


    To understand CSS animation properly, you need to know the main properties involved. Most animations are built from a small group of CSS rules.


    animation-name


    This connects an element to a @keyframes rule.


    cssCODE
    animation-name: fadeIn;

    The value must match the keyframe name.


    animation-duration


    This controls how long the animation takes.


    cssCODE
    animation-duration: 1s;

    Common units include s for seconds and ms for milliseconds.


    animation-timing-function


    This controls the speed curve of the animation.


    cssCODE
    animation-timing-function: ease-in-out;

    Common values include:


  • linear — same speed from start to end
  • ease — starts slow, speeds up, then slows down
  • ease-in — starts slow
  • ease-out — ends slow
  • ease-in-out — smooth start and end

  • animation-delay


    This delays the start of an animation.


    cssCODE
    animation-delay: 0.5s;

    This is useful when sequencing multiple elements, such as cards appearing one after another.


    animation-iteration-count


    This controls how many times an animation repeats.


    cssCODE
    animation-iteration-count: infinite;

    You can use a number like 1, 2, or 3, or use infinite for continuous animation.


    animation-direction


    This controls whether the animation plays forward, backward, or alternates.


    cssCODE
    animation-direction: alternate;

    For example, alternate makes the animation go forward, then backward, then forward again.


    The Shorthand animation Property


    Instead of writing each property separately, you can use the shorthand:


    cssCODE
    .loader {
      animation: spin 1s linear infinite;
    }

    This means:


  • Animation name: spin
  • Duration: 1s
  • Timing: linear
  • Iteration count: infinite

  • A Practical Example: Creating a Loading Spinner


    A loading spinner is one of the most common CSS animation examples. It gives users feedback that something is happening, such as fetching data from an API or uploading a file.


    Here is a simple spinner:


    cssCODE
    @keyframes spin {
      to { transform: rotate(360deg); }
    }
    
    .spinner {
      width: 32px;
      height: 32px;
      border: 4px solid #ddd;
      border-top-color: #4f46e5;
      border-radius: 50%;
      animation: spin 0.8s linear infinite;
    }

    This works because the element has a circular border, but the top border has a different color. Rotating the whole element creates the spinner effect.


    In HTML, you would use:


    markupCODE
    <div class="spinner"></div>

    This example demonstrates an important animation principle: animate transforms instead of layout properties whenever possible. The `transform` property is usually much more performant than animating properties like `width`, `height`, `top`, or `left`.


    Why CSS Animation Matters


    CSS animation is not just about decoration. Used correctly, it improves usability, communicates state, and makes interfaces feel more responsive.


    It Provides Visual Feedback


    Users need to know when their actions have been recognized. A button hover effect, a pressed state, or a loading animation tells the user that the interface is responding.


    For example:


  • A button scales slightly when hovered
  • A form field border changes color when focused
  • A modal fades in instead of appearing suddenly
  • A dropdown slides open smoothly

  • These details make interfaces feel polished and easier to understand.


    It Helps Guide Attention


    Animation can direct the user’s eye toward important changes. If a notification appears, a subtle slide or fade can help users notice it without being startled.


    This is especially useful in:


  • Dashboards
  • Forms
  • Onboarding flows
  • Error messages
  • Shopping carts
  • Interactive tutorials

  • However, attention-guiding animation should be subtle. If everything moves, nothing stands out.


    It Reduces Perceived Waiting Time


    A well-designed loading animation does not make your site faster, but it can make waiting feel less frustrating. Skeleton loaders, progress bars, and spinners reassure users that the system has not frozen.


    CSS is often enough for these effects, especially when the animation is purely visual and does not require complex state management.


    CSS Animation vs JavaScript Animation


    CSS animation and JavaScript animation often overlap, but they are not identical tools.


    When CSS Animation Is Better


    Use CSS animation when the effect is mostly visual and does not require complex logic.


    Good CSS animation use cases include:


  • Hover effects
  • Fade-ins
  • Slide transitions
  • Loaders
  • Pulsing icons
  • Menu open/close effects
  • Simple page entrance animations

  • CSS is concise, easy to maintain, and usually performs well for common UI animations.


    When JavaScript Animation Is Better


    Use JavaScript when animation depends on dynamic calculations, user input, or complex sequencing.


    JavaScript may be better for:


  • Drag-and-drop interactions
  • Scroll-driven effects with precise control
  • Canvas animations
  • Games
  • Advanced timelines
  • Physics-based motion
  • Animations tied to real-time data

  • In many modern apps, CSS and JavaScript work together. JavaScript may add or remove a class, while CSS handles the actual animation.


    Example:


    cssCODE
    .modal {
      opacity: 0;
      transition: opacity 0.3s ease;
    }
    
    .modal.open {
      opacity: 1;
    }

    JavaScript can toggle the open class, but CSS controls the animation.


    Best Practices for CSS Animation


    Good animation should feel natural, fast, and purposeful. Bad animation can make a site feel slow, distracting, or unprofessional.


    Animate transform and opacity When Possible


    The most performant properties to animate are usually:


  • transform
  • opacity

  • These properties can often be handled efficiently by the browser’s compositor.


    Prefer this:


    cssCODE
    .card:hover {
      transform: translateY(-4px);
    }

    Avoid relying heavily on this:


    cssCODE
    .card:hover {
      margin-top: -4px;
    }

    Animating layout properties like margin, width, height, top, and left can trigger layout recalculations, which may cause jank.


    Keep Durations Short


    Most UI animations should be quick.


    Useful timing guidelines:


  • 100–200ms for small hover and focus effects
  • 200–400ms for menus, modals, and cards
  • 500ms+ only for large transitions or decorative effects

  • If an animation is too fast, users may not notice it. If it is too slow, the interface feels sluggish.


    Use Easing Carefully


    Real-world movement rarely starts and stops instantly. Easing makes animation feel more natural.


    A good default for many UI animations is:


    cssCODE
    transition: transform 0.2s ease, opacity 0.2s ease;

    For mechanical or continuous movement, such as a spinner, linear often works better.


    Respect Reduced Motion Preferences


    Some users experience motion sensitivity. Operating systems allow users to request reduced motion, and your CSS should respect that.


    cssCODE
    @media (prefers-reduced-motion: reduce) {
      * {
        animation: none !important;
        transition: none !important;
      }
    }

    This is an important accessibility practice. Animation should enhance the experience, not make the site uncomfortable to use.


    Avoid Infinite Animations Unless Necessary


    Infinite animations can distract users and consume resources. Use them carefully.


    Good uses of infinite animation:


  • Loading spinners
  • Progress indicators
  • Subtle background motion, if optional
  • Status indicators

  • Poor uses include constantly bouncing buttons, pulsing text, or decorative effects that compete with content.


    Common CSS Animation Use Cases


    CSS animation appears in nearly every modern website or app interface. Here are some practical examples.


    Button Hover Effects


    A button can become more interactive with a subtle transform, color shift, or shadow change.


    cssCODE
    .button {
      transition: transform 0.2s ease;
    }
    
    .button:hover {
      transform: scale(1.04);
    }

    If you are designing hover states with shadows, a tool like the CSS Box Shadow Generator can help you quickly create shadow values and then fine-tune them in your animation.


    Card Entrance Effects


    Cards can fade or slide into view when a page loads. This is common in dashboards, portfolios, and product grids.


    Example idea:


    1. Start the card slightly lower with transform: translateY(12px).

    2. Set opacity: 0.

    3. Animate to transform: translateY(0) and opacity: 1.


    This creates a smooth entrance without affecting layout.


    Animated Backgrounds


    CSS gradients can be animated to create subtle background motion. This works well for landing pages, hero sections, or decorative banners.


    For example, you might create a colorful gradient background and animate its background position. If you need help generating the initial gradient, the CSS Gradient Generator is useful for building a clean starting point before adding animation.


    Loading States


    Loaders, skeleton screens, and progress indicators are excellent CSS animation use cases. They help communicate that work is happening in the background.


    A skeleton loader might animate a light gradient across placeholder blocks to simulate content loading.


    How to Get Started With CSS Animation


    If you are new to CSS animation, start small. You do not need to memorize every property immediately.


    A good learning path is:


    1. Learn transitions first

    Add hover and focus effects to buttons, links, and cards.


    2. Practice `transform` and `opacity`

    Try scale(), translateX(), translateY(), rotate(), and opacity.


    3. Write simple keyframes

    Create fade, slide, spin, and pulse animations.


    4. Experiment with timing functions

    Compare linear, ease, ease-in, and ease-out.


    5. Add accessibility checks

    Use prefers-reduced-motion for animations that are not essential.


    6. Refine instead of adding more

    A small number of thoughtful animations is better than many flashy effects.


    For example, a beginner-friendly animation project could be:


  • A button that scales on hover
  • A card that fades in on page load
  • A spinner for loading state
  • A notification that slides in and fades out

  • These examples cover most of the CSS animation concepts you will use in real projects.


    Common Mistakes to Avoid


    CSS animation is easy to start with, but there are several common mistakes that can hurt usability and performance.


    Animating Too Many Elements at Once


    Animating dozens or hundreds of elements simultaneously can make a page feel busy and slow. Use animation where it has a purpose.


    Making Important Content Wait


    Avoid long entrance animations that delay access to content. Users came for the content or functionality, not to watch UI elements perform.


    Using Motion Without Meaning


    Every animation should answer a question:


  • Did something change?
  • Did the user trigger an action?
  • Is something loading?
  • Where should the user look next?

  • If the animation does not support usability, consider removing it.


    Ignoring Mobile Performance


    Mobile devices may have less processing power than desktop machines. Test animations on real devices when possible, especially if your design includes large backgrounds, shadows, filters, or multiple moving elements.


    Frequently Asked Questions


    What is CSS animation in simple terms?


    CSS animation is a way to change an element’s appearance over time using CSS. For example, you can make a button fade, a box move, an icon spin, or a menu slide open without writing JavaScript for the animation itself.


    What is the difference between transition and animation in CSS?


    A transition animates a change between two states, such as a button changing color on hover. A keyframe animation can define multiple stages and can run automatically, repeat infinitely, or play in more complex sequences. Use transitions for simple interactions and keyframes for more controlled animations.


    Do CSS animations need JavaScript?


    No, CSS animations do not require JavaScript. You can create many effects using only CSS. However, JavaScript is often used to trigger animations by adding or removing CSS classes, especially in interactive apps.


    Are CSS animations bad for performance?


    CSS animations are not automatically bad for performance. They are usually efficient when you animate transform and opacity. Performance problems are more likely when animating layout-heavy properties like width, height, top, left, or when running too many animations at the same time.


    Can CSS animations be accessible?


    Yes, but they should be designed carefully. Avoid flashing effects, excessive motion, and long animations that block content. You should also support the prefers-reduced-motion media query so users who prefer less motion can browse comfortably.


    When should I use CSS animation instead of JavaScript?


    Use CSS animation for simple visual effects like hover states, fades, slides, spinners, and subtle UI feedback. Use JavaScript when animation depends on complex logic, user gestures, physics, scroll position, or dynamic calculations.

    Related Tools

    Related Articles