What are CSS animations?

CSS animations allow you to smoothly transition between two or more style states over time. Unlike CSS transitions (which respond to a trigger like :hover), animations can run automatically on page load without user interaction.

Animations are built on two key concepts:

  • @keyframes: Define what styles an element has at different points in time (0%, 50%, 100%).
  • Animation properties: Tell the browser how to play those keyframes (how long, what timing, how many times, etc.).

Animations are GPU-accelerated, making them fast and smooth โ€” perfect for everything from loading spinners to hero image slide-ins to button hover effects.

Key difference:
Transitions happen between two states (A โ†’ B). Animations define multiple waypoints (A โ†’ B โ†’ C) and can repeat infinitely or run on page load without user action.

The @keyframes rule

The @keyframes rule is where you define an animation's stages. You use percentages (or the keywords from and to) to specify when style changes occur:

animations.css Basic @keyframes syntax

Each keyframe (0%, 50%, 100%) is a snapshot of styles at that point in the animation timeline. The browser smoothly interpolates between keyframes.

  • from is shorthand for 0% (the start)
  • to is shorthand for 100% (the end)
  • You can use any percentage: 0%, 25%, 50%, 75%, 100%
  • You can define multiple properties at each keyframe

Animation properties

Once you've defined a @keyframes animation, you apply it to an element using animation properties:

animations.css All animation properties explained

Animation shorthand

You can combine all animation properties into one line using the animation shorthand. The order is:

animation: name duration timing-function delay iteration-count direction fill-mode;

animations.css Shorthand examples
Tip:
Only animation-name and animation-duration are required. The rest have defaults. If you only use two values, they're interpreted as name and duration.

Try it yourself: Loading spinner & bouncing ball

Edit the code below to see a rotating spinner and a bouncing ball. Pay attention to how animation-iteration-count: infinite keeps the spinner spinning, and how animation-direction: alternate makes the ball bounce up and down.

โœ๏ธ Try It Yourself โ€” Edit & Run
Preview

Slide-in and pulse effects

This editor demonstrates animation-fill-mode: forwards (element keeps final keyframe styles) and a pulsing notification badge. Change the delay or duration to experiment!

โœ๏ธ Try It Yourself โ€” Slide-in & Pulse
Preview

Key takeaways

  • @keyframes define animation stages. Use percentages (0%, 50%, 100%) to specify styles at each point in the timeline.
  • animation-name links an element to a @keyframes rule.
  • animation-duration controls how long the animation takes (in seconds or milliseconds).
  • animation-timing-function controls speed (linear, ease, ease-in, ease-out). Default is ease.
  • animation-delay waits before the animation starts.
  • animation-iteration-count: infinite makes an animation loop forever.
  • animation-direction: alternate plays forward, then backward in each repeat.
  • animation-fill-mode: forwards keeps the final keyframe styles after the animation ends.
  • Use the animation shorthand to combine all properties in one line.
  • Animations are GPU-accelerated โ€” they perform better than JavaScript-based animations.
Challenge: Animated Hero Page

Task: Create an animated landing page with:

  • Text that fades in and slides up from below (animation-fill-mode: forwards)
  • A pulsing CTA button
  • A rotating decorative circle in the background
  • Use animation-delay to stagger the animations

Hint: Define @keyframes for fadeInUp, pulse, and spin. Apply them with different delays to create a cascading effect.

Hint: Create three animations: (1) fadeInUp using opacity and translateY, (2) pulse using scale, (3) spin using rotate. Apply them with staggered animation-delay values (0s, 0.2s, 0.4s) to each element.

Test your knowledge

Question 1 of 5
What defines the stages of an animation in CSS?
Question 2 of 5
How do you make an animation loop forever?
Question 3 of 5
What does animation-direction: alternate do?
Question 4 of 5
What does animation-fill-mode: forwards do?
Question 5 of 5
What is the correct order for the animation shorthand property?
0
/5
Excellent! You understand CSS animations.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just learned CSS animations.
Master the full web development stack.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’