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.
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:
Each keyframe (0%, 50%, 100%) is a snapshot of styles at that point in the animation timeline. The browser smoothly interpolates between keyframes.
fromis shorthand for 0% (the start)tois 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:
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;
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.
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!
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
animationshorthand to combine all properties in one line. - Animations are GPU-accelerated โ they perform better than JavaScript-based animations.
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
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 โ