AdvancedLesson 23 of 305 min read

CSS keyframe animations: loaders, entrances and recipes

Create motion that runs on its own with @keyframes and the animation properties, plus practical recipes.

In this lesson you will

  • Define and apply keyframe animations
  • Use iteration, direction and fill-mode
  • Stagger a list of items
  • Build common recipes with care

About 16 minutes

When motion runs by itself

A transition needs a trigger, like a hover, and travels between two states. But what about a loading spinner that turns forever, a notification that bounces to get attention, a hero that fades up when the page opens, or a card that pulses to say “click me”? These need motion that starts on its own, has more than two stages and may repeat. CSS has a system for that: keyframe animations.

Two steps: define, then apply

First you describe the animation with @keyframes, giving it a name and stating how the element looks at different moments. Then you attach that animation to an element with the animation properties.

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

.hero h1 {
  animation: fade-up 0.6s ease-out;
}

from and to describe the start and the end. The browser fills in all the frames between. When the page loads, the heading fades in while sliding up 20 pixels.

Press Run to see the heading appear, then change the animation duration to 1.5s to slow it down. Notice it plays once, on load.

<style>
 @keyframes fade-up { from { opacity:0; transform: translateY(20px); } to { opacity:1; transform: translateY(0); } }
 h1 { font-family: Arial; animation: fade-up 0.6s ease-out; }
</style>
<h1>Welcome to Skills in Minutes</h1>

Change 0.6s to 1.5s in the animation shorthand.

<style>
 @keyframes fade-up { from { opacity:0; transform: translateY(20px); } to { opacity:1; transform: translateY(0); } }
 h1 { font-family: Arial; animation: fade-up 1.5s ease-out; }
</style>
<h1>Welcome to Skills in Minutes</h1>

Percentage keyframes

For more than two stages, use percentages of the animation’s duration.

@keyframes pulse {
  0%   { transform: scale(1);    box-shadow: 0 0 0 0 rgba(90, 40, 200, .5); }
  70%  { transform: scale(1.04); box-shadow: 0 0 0 14px rgba(90, 40, 200, 0); }
  100% { transform: scale(1);    box-shadow: 0 0 0 0 rgba(90, 40, 200, 0); }
}
.cta { animation: pulse 2s infinite; }

The animation properties

.box {
  animation-name: pulse;
  animation-duration: 2s;
  animation-timing-function: ease-in-out;
  animation-delay: 0.5s;
  animation-iteration-count: infinite;   /* or a number like 3 */
  animation-direction: alternate;        /* normal | reverse | alternate */
  animation-fill-mode: both;             /* keep the start/end styles */
  animation-play-state: running;         /* or paused */
}
/* the shorthand: name duration timing delay count direction fill-mode */
.box { animation: pulse 2s ease-in-out .5s infinite alternate both; }
  • iteration-count says how many times: a number or infinite.
  • direction: alternate plays forward, then backward, which makes smooth back-and-forth loops without extra keyframes.
  • fill-mode: forwards keeps the final keyframe after the animation ends. backwards applies the first keyframe during the delay. both does both. Without it, an element jumps back to its normal style when the animation finishes.
  • play-state: paused freezes the animation. Handy to pause on hover.

Make a loading spinner: define a keyframe that rotates from 0 to 360 degrees, and apply it with a 1s linear infinite animation to the circle.

<style>
 body { padding:30px; }
 .spinner { width:44px; height:44px; border:5px solid #E1D8FA; border-top-color:#5A28C8; border-radius:50%; /* animation */ }
 /* @keyframes here */
</style>
<div class="spinner"></div>

@keyframes spin { to { transform: rotate(360deg); } } and animation: spin 1s linear infinite;

<style>
 body { padding:30px; }
 .spinner { width:44px; height:44px; border:5px solid #E1D8FA; border-top-color:#5A28C8; border-radius:50%; animation: spin 1s linear infinite; }
 @keyframes spin { to { transform: rotate(360deg); } }
</style>
<div class="spinner"></div>
Good to know

A keyframe with only a to block works: the browser uses the element’s current style as the starting point.

Staggered entrances

A set of items that all pop in together feels flat. Items that appear one after another, with a slight delay each, feel choreographed. You can create the stagger with a custom property or the :nth-child selector.

.list li { opacity: 0; animation: fade-up .5s ease-out forwards; }
.list li:nth-child(1) { animation-delay: .05s; }
.list li:nth-child(2) { animation-delay: .15s; }
.list li:nth-child(3) { animation-delay: .25s; }
.list li:nth-child(4) { animation-delay: .35s; }

The forwards fill mode matters here. Each item starts invisible, plays its animation, then keeps the final visible state.

Useful animation recipes

  • Fade in: opacity from 0 to 1.
  • Slide in: translate from off-screen to 0.
  • Bounce: several keyframes that move up and settle.
  • Shake: small left-right translations to show an error in a form field.
  • Skeleton loading: a moving gradient across grey blocks to show that content is coming.
  • Typing cursor: a border that blinks using a steps() timing function.
@keyframes shimmer { to { background-position: -200% 0; } }
.skeleton {
  background: linear-gradient(90deg, #ECE8F8 25%, #F7F5FF 50%, #ECE8F8 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s linear infinite;
  border-radius: 8px;
  height: 18px;
}

Scroll-driven animations: a peek ahead

A newer feature lets animations be tied to scrolling, with no JavaScript: a reading progress bar, or elements that reveal as they come into view. It uses animation-timeline: scroll() or view(). Browser support is growing but not universal, so use it as an enhancement and always provide a sensible fallback.

Motion with care

  • Do not animate large parts of the screen constantly. It distracts and drains batteries.
  • Infinite animations should be small and easy to ignore, or should stop after a few seconds.
  • Honour prefers-reduced-motion, as you learned in the transitions lesson.
  • Avoid flashing content, which can trigger seizures. Keep flashes below three per second.
  • Pause long animations when the tab is hidden, or let people pause them with a button.

Create a shake animation for an error message: keyframes that move it left and right a few pixels, applied for 0.4s.

<style>
 body { padding:20px; font-family:Arial; }
 .error { display:inline-block; background:#FFE9EF; color:#B0184B; padding:12px 18px; border-radius:10px; /* animation */ }
 /* keyframes */
</style>
<div class="error">That password is incorrect.</div>

@keyframes shake { 0%,100%{transform:translateX(0)} 25%{transform:translateX(-6px)} 75%{transform:translateX(6px)} } and animation: shake .4s;

<style>
 body { padding:20px; font-family:Arial; }
 .error { display:inline-block; background:#FFE9EF; color:#B0184B; padding:12px 18px; border-radius:10px; animation: shake .4s; }
 @keyframes shake { 0%,100%{transform:translateX(0)} 25%{transform:translateX(-6px)} 75%{transform:translateX(6px)} }
</style>
<div class="error">That password is incorrect.</div>
Common mistake

Forgetting animation-fill-mode: forwards on an entrance animation. The element plays its fade-in and then snaps back to invisible, because its normal style was opacity 0.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Which at-rule defines the stages of an animation?

  1. @media
  2. @keyframes
  3. @font-face
  4. @import

@keyframes names and describes the animation steps.

Which value repeats an animation forever?

  1. animation-iteration-count: infinite
  2. animation-loop: on
  3. animation-repeat: 0
  4. animation-end: never

infinite loops without stopping.

What does animation-fill-mode: forwards do?

  1. Plays it backwards
  2. Keeps the final keyframe styles after it ends
  3. Adds a delay
  4. Pauses it

The element stays in its end state.

How do you create a staggered entrance?

  1. Different animation-delay values per item
  2. Different colours
  3. More keyframes only
  4. A bigger font

Delays offset each item’s start.

What is a safe limit for flashing content?

  1. Many per second
  2. Fewer than three flashes per second
  3. Always flashing
  4. No limit

Rapid flashes can trigger seizures.

Summary

  • @keyframes defines stages; animation applies them.
  • fill-mode forwards keeps the end state.
  • Delays create staggered entrances.
  • Keep motion purposeful and accessible.

Next lesson

Next: custom properties, the feature that makes CSS configurable and themeable.

Questions

Transition or animation?

Use a transition for a change between two states triggered by something. Use an animation for automatic or multi-step motion.

Can JavaScript control CSS animations?

Yes. You can add or remove classes, change play state, or use the Web Animations API.

Why does my animation jump at the end?

Probably the fill-mode. Use forwards or both to keep the end state.

Progress is saved in this browser.