DEV Community

Cover image for CSS Animations Tutorial: A Beginner's Guide
Usama Nazir
Usama Nazir

Posted on

CSS Animations Tutorial: A Beginner's Guide

CSS animations can really help your website or brand pop by adding dynamic, eye-catching effects. If you're new to them, they might feel intimidating at first, but once you grasp the key properties, they're straightforward and fun to work with. This guide breaks down everything from the video, explaining the essential CSS animation properties step by step. We'll cover how to create animations, when to use them over transitions, and even how to make them interactive. Let's dive in!

When to Use Animations vs. Transitions

Before jumping into animations, it's worth asking: Do I really need a full animation, or would a simple transition do the trick?

  • Transitions are ideal for straightforward changes from one state to another, like hovering over an element to alter its color, size, or position. They handle single-step shifts smoothly, no matter how many properties you're changing.

  • Animations, on the other hand, shine when you need a sequence of multiple steps or an ongoing, looping effect (e.g., something spinning in the background). The animation property is a shorthand that combines several sub-properties to define these more complex behaviors.

If it's a one-and-done change, stick with transitions. For multi-step or continuous motion, animations are your go-to.

Setting Up Your First Animation with @keyframes

To create an animation, start by defining it with the @keyframes rule. This is where you outline the sequence of changes over time.

  1. Name Your Animation: Choose a descriptive name, like spin. It's totally up to you—just make it memorable.

  2. Define the Sequence: Use percentages to mark keyframes:

    • 0%: The starting point.
    • 100%: The ending point.
    • Anything in between (e.g., 50%): Intermediate steps.

For example, to spin an element 360 degrees and turn it into a circle by the end:

   @keyframes spin {
     0% {
       /* Initial state (implied if not specified) */
     }
     100% {
       transform: rotate(360deg);
       border-radius: 50%;
     }
   }
Enter fullscreen mode Exit fullscreen mode

Want to add complexity? At 50%, scale it up:

   @keyframes spin {
     0% {
       /* Starting point */
     }
     50% {
       transform: scale(2);
     }
     100% {
       transform: rotate(360deg) scale(2);  /* Keep the scale for the full effect */
       border-radius: 50%;
     }
   }
Enter fullscreen mode Exit fullscreen mode

Key tip: If you're changing a property mid-animation (like scale), you must carry it forward to later keyframes if you want the change to persist. Otherwise, it'll revert smoothly to the next defined state.

To separate growing from spinning:

   @keyframes spin {
     0% {
       /* Square and unrotated */
     }
     50% {
       transform: rotate(0deg) scale(2);
       border-radius: 0;
     }
     100% {
       transform: rotate(360deg) scale(2);
       border-radius: 50%;
     }
   }
Enter fullscreen mode Exit fullscreen mode

CSS will interpolate (blend) between keyframes automatically if something isn't specified, creating smooth transitions.

Applying the Animation to an Element

Once your keyframes are set, attach the animation to your HTML element (e.g., a div):

.element {
  animation-name: spin;
  animation-duration: 3s;  /* How long the full cycle takes */
}
Enter fullscreen mode Exit fullscreen mode

On page load, it'll run once and snap back to the start. Now, let's tweak it with more properties for control.

Fine-Tuning with Animation Properties

These properties let you customize timing, speed, repetition, and more. They can all be shorthand-ed into one animation line, but we'll break them down.

animation-duration

Sets the length of one cycle. Shorter for quick effects, longer for deliberate ones.

  • Example: animation-duration: 0.5s; (fast) vs. 3s (slower).

animation-timing-function

Controls the speed curve, how the animation accelerates or decelerates.

  • Default: ease (slows at each step for smoothness).
  • linear: Constant speed throughout.
  • Others: ease-in (starts slow), ease-out (ends slow), ease-in-out (slow at both ends).

Easing makes things feel more natural, like in video editing.

animation-delay

Pauses before starting.

  • Example: animation-delay: 1s; (waits 1 second).

animation-iteration-count

How many times it repeats.

  • Default: 1.
  • A number like 3 for finite loops.
  • infinite for endless playback (great for backgrounds).

animation-direction

Changes playback order.

  • Default: normal (0% to 100%).
  • reverse: Backwards (100% to 0%).
  • alternate: Switches direction each cycle (forward, then reverse) for multi-iterations.

animation-fill-mode

Decides what happens after the animation ends.

  • Without it: Snaps back to initial state.
  • forwards: Holds the final keyframe's styles (e.g., stays as a circle).

This gives animations a permanent impact.

animation-play-state

Makes animations interactive!

  • Default: running.
  • Set to paused on events like :hover:
  .element:hover {
    animation-play-state: paused;
  }
Enter fullscreen mode Exit fullscreen mode

Or vice versa: Pause by default, run on hover.

  • With JavaScript: Add buttons to toggle via element.style.animationPlayState = 'paused' or 'running'. Simple click listeners create play/pause controls.

Using the Animation Shorthand

Combine everything in one property for cleaner code:

animation: spin 3s ease 1s infinite alternate forwards running;
Enter fullscreen mode Exit fullscreen mode

Order: name, duration, timing-function, delay, iteration-count, direction, fill-mode, play-state.

Practice Exercise: Building a Loading Animation

Let's apply this to a real example. A glowing square that rotates in 3D for a loading spinner.

HTML:

<div class="loading"></div>
Enter fullscreen mode Exit fullscreen mode

CSS Setup:

.loading {
  width: 100px;
  height: 100px;
  border: 2px solid blue;
  border-radius: 10px;
  box-shadow: 0 0 10px blue, 0 0 20px blue;  /* Glow effect */
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;
  animation: loading 2s ease-in-out infinite;
}
Enter fullscreen mode Exit fullscreen mode

Keyframes:

@keyframes loading {
  0% {
    transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  }
  33% {
    transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  }
  67% {
    transform: rotateX(180deg) rotateY(180deg) rotateZ(0deg);
  }
  100% {
    transform: rotateX(180deg) rotateY(180deg) rotateZ(180deg);
  }
}
Enter fullscreen mode Exit fullscreen mode

It rotates on X, Y, then Z axes. Since 180° looks like 0°, the loop resets seamlessly without visible jumps.

There you have it. A complete rundown on CSS animations. Experiment with these properties to bring your designs to life! If you're into web development, check out more tutorials for deeper dives.

Top comments (0)