Favorites

Beginners Guide to CSS Animations

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Guide to Animations

What are CSS animations?

CSS animations let you move, fade, spin, grow or recolour elements over time, using CSS alone. They're great for loading spinners, attention-grabbing badges, content that gently appears as a page loads, and all kinds of small touches that make a site feel polished.

Creating an animation takes two steps:

  1. Define the keyframes, which describe what the animation looks like at different points, using the @keyframes rule.
  2. Apply the animation to an element, saying how long it lasts, how many times it plays and so on, using the animation property.

Animations or transitions? CSS also has transitions, which smoothly change a property from one value to another when something changes, such as on hover. Animations are more powerful: they can have many steps, play automatically, repeat and run in reverse. Use a transition for a simple A-to-B change, and an animation for anything more.

Defining keyframes

The @keyframes rule gives your animation a name, then lists keyframes: points in the animation, written as percentages, with the styles the element should have at each point. The browser smoothly fills in everything in between.

@keyframes bounce {
  0% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-24px);
  }
  100% {
    transform: translateY(0);
  }
}

This animation has three keyframes:

  • At 0% (the start), the element is in its normal position.
  • At 50% (halfway), translateY(-24px) moves it 24 pixels up. Negative values move up, and positive values move down.
  • At 100% (the end), it's back where it started.

Shortcuts

You can use from and to instead of 0% and 100%, and list several percentages together when they share the same styles. This version of bounce does exactly the same thing:

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-24px);
  }
}

Tip: if you leave out the 0% or 100% keyframe, the browser uses the element's normal styles there. So an animation that just fades something in only needs from { opacity: 0; }.

Applying an animation

To play the animation, add the animation property to an element, with the animation's name and how long it should last:

.ball {
  animation: bounce 1s ease-in-out infinite;
}
Results

This tells the browser to play bounce over 1 second, speeding up and slowing down smoothly (ease-in-out), forever (infinite).

Why not on hover? The original version of this post started the animation with :hover. That works, but if the pointer leaves the element mid-animation, the animation stops dead and the element jumps back. It also never plays on touch screens, which don't have hover. For simple hover effects, a transition is usually the smoother choice.

The animation properties

The animation shorthand combines several individual properties, which you can also set separately:

PropertyWhat it doesExample
animation-nameWhich @keyframes to usebounce
animation-durationHow long one cycle takes1s or 500ms
animation-timing-functionThe speed curve (how it speeds up and slows down)ease-in-out
animation-delayA wait before it starts0.5s
animation-iteration-countHow many times it plays3 or infinite
animation-directionForwards, backwards or alternatingalternate
animation-fill-modeWhich styles apply before it starts and after it endsforwards
animation-play-statePauses or resumes the animationpaused

In the animation shorthand, the order mostly doesn't matter, but the first time value is always the duration and the second is the delay:

/* name | duration | timing function | delay | iteration count | direction | fill mode */
animation: slide 2s ease-in-out 0.5s infinite alternate both;

Timing functions

The timing function controls the feel of an animation. Each dot below travels the same distance in the same time, but with a different speed curve:

Results
linear
ease
ease-in-out
steps(5)
cubic-bezier()
  • linear moves at a constant speed, which can feel mechanical.
  • ease (the default) starts quickly and slows down gently at the end.
  • ease-in-out starts and ends slowly, which feels natural for things moving back and forth.
  • steps(5) jumps in five steps instead of moving smoothly, like a ticking clock.
  • cubic-bezier() lets you design your own curve. These values overshoot the end slightly, then settle back, for a springy feel.

Direction

By default, each cycle plays from start to finish, then jumps back to the start. With alternate, every other cycle plays in reverse, so the movement flows back and forth:

Results
normal
alternate

Fill mode

When an animation finishes, the element normally snaps back to its original styles. Setting animation-fill-mode: forwards keeps the styles from the final keyframe instead. Press the button to play both animations once:

Results
none
forwards

Fill mode also matters with a delay: backwards applies the first keyframe's styles during the delay, and both combines forwards and backwards. For a fade-in with a delay, both is usually what you want, so the element stays hidden until it starts.

Practical examples

Here are four animations you'll find on real websites. The first button pauses them all, using animation-play-state. The fade-in plays once, so use the second button to see it again:

Results
Loading
Spinner
Pulse
Skeleton
One
Two
Three
Fade-in

Loading spinner

@keyframes spin {
  to { rotate: 360deg; }
}

.spinner {
  width: 36px;
  height: 36px;
  border: 4px solid #dbeafe;
  border-top-color: #2563eb;  /* one coloured edge makes the spin visible */
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

Give the spinner role="status" and some visually hidden text such as "Loading", so screen reader users know something is happening too.

Pulsing notification dot

@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgb(220 38 38 / 0.6); }
  100% { box-shadow: 0 0 0 14px rgb(220 38 38 / 0); }
}

.dot {
  animation: pulse 1.5s ease-out infinite;
}

Skeleton loading shimmer

@keyframes shimmer {
  from { background-position: 100% 0; }
  to   { background-position: 0 0; }
}

.skeleton {
  background: linear-gradient(90deg, #e5e7eb 40%, #f9fafb 50%, #e5e7eb 60%);
  background-size: 300% 100%;
  animation: shimmer 1.6s linear infinite;
}

Staggered fade-in

@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(16px);
  }
}

.item {
  animation: fade-up 0.6s ease-out both;
}

.item:nth-child(2) { animation-delay: 0.2s; }
.item:nth-child(3) { animation-delay: 0.4s; }

The fade-in only needs a from keyframe, because it ends at the element's normal styles.

Keeping animations smooth

Some properties are much cheaper for the browser to animate than others:

  • Animate transform and opacity wherever possible. Browsers can usually animate these on the graphics card, without recalculating the page layout, so they stay smooth even on slower devices.
  • Avoid animating layout properties such as width, height, top, left and margin. Changing them makes the browser work out the position of everything around the element on every frame, which can cause stuttering. To move something, use transform: translate(); to grow it, use scale.
  • Use will-change sparingly. will-change: transform hints that an element is about to animate, but applying it to lots of elements uses extra memory. Only add it if you've noticed a problem.

Tip: CSS now has separate translate, rotate and scale properties, as used in the spinner above. They're handy because you can animate one without overwriting the others, which is what happens when everything is combined in a single transform.

Accessibility

Movement on screen can cause dizziness, nausea or headaches for some people, especially those with vestibular (inner ear and balance) disorders. Many people turn on a "reduce motion" setting on their device for this reason, and CSS can detect it with the prefers-reduced-motion media query:

@media (prefers-reduced-motion: reduce) {
  .ball,
  .dot,
  .item {
    animation: none;
  }

  /* Keep essential animations, but calm them down */
  .spinner {
    animation-duration: 3s;
  }
}

Try it: the demos in this post use this technique. If you turn on your device's reduce motion setting and reload the page, the decorative demos stop moving, and the spinner slows down.

A few more guidelines:

  • Give people a way to stop movement. Anything that moves automatically for more than five seconds alongside other content should have a pause button, like the one in the examples above.
  • Never flash content more than three times a second, as rapid flashing can trigger seizures.
  • Keep it purposeful. Animations work best when they guide attention or show that something is happening, not when they're everywhere.

Scroll-driven animations

Normally an animation plays over a set time. Scroll-driven animations link its progress to scrolling instead, so it moves forwards as you scroll down and backwards as you scroll up, with no JavaScript. Scroll inside the box below to see a reading progress bar:

Results

Scroll down inside this box. The blue bar at the top grows as you scroll, showing how far through the content you are.

Scroll-driven animations link an animation's progress to a scroll position instead of to time.

Scroll back up, and the animation runs in reverse. No JavaScript is involved at all.

This makes effects like reading progress bars, parallax backgrounds and reveal-on-scroll content possible with CSS alone.

The animation itself is a simple @keyframes rule that scales the bar from 0 to its full width.

You've reached the end, so the bar should now stretch across the whole box.

@keyframes grow {
  from { transform: scaleX(0); }
  to   { transform: scaleX(1); }
}

.progress {
  position: sticky;
  top: 0;
  height: 6px;
  background: #2563eb;
  transform-origin: left;
  animation: grow linear both;
  animation-timeline: scroll(nearest block);  /* follow the nearest scrolling box */
}
  • animation-timeline: scroll() links the animation to how far a box has been scrolled. Use scroll(root) to follow the whole page.
  • animation-timeline: view() links it to an element's position as it passes through the screen, which is perfect for fading content in as it scrolls into view.
  • No duration is needed, because scrolling controls the timing.

Good to know: set animation-timeline after the animation shorthand, because the shorthand resets it. Scroll-driven animations work in Chrome and Edge (115 onwards), Safari (26 onwards) and Firefox (159 onwards). In older browsers the animation simply doesn't run, so make sure your content still makes sense without it.

Wrapping up

Every CSS animation comes down to two parts: @keyframes to describe the movement, and the animation property to play it. From there, timing functions shape how it feels, fill modes control where it ends, and scroll-driven timelines link it to the page. Stick to animating transform and opacity, respect prefers-reduced-motion, and your animations will be smooth and comfortable for everyone.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee