CSS Animations
In this page:
@keyframes animation-name {
from { property: value; }
to { property: value; }
}
selector {
animation: animation-name duration timing-function iteration-count;
}
Declaring @keyframes Timelines
The @keyframes rule is used to define the states and timeline of your animation. You specify keyframes using percentage indicators (from 0% to 100%) or simple keyword hooks (from and to) to define which CSS properties change at different moments.
- Use percentage indicators in your keyframes to build complex, multi-state animations.
- Accepted values:
- animation-name — name of a @keyframes rule
- animation-duration — time such as 2s or 500ms
- animation-timing-function — ease | linear | ease-in | ease-out | ease-in-out | cubic-bezier()
- animation-iteration-count — number | infinite
- animation-direction — normal | reverse | alternate | alternate-reverse
- animation-fill-mode — none | forwards | backwards | both
Example: Declaring @keyframes Timelines
<style>
@keyframes fade {
0% { opacity: 0; }
100% { opacity: 1; }
}
.box {
background: teal;
color: white;
padding: 10px;
animation: fade 2s;
}
</style>
<div class="box">Fades in using the keyframes above</div>
Applying custom Animations
Once you have declared your keyframe timeline, you apply it to an element using the animation-name and animation-duration properties. This instructs the browser to run your custom animation timeline immediately.
Adding a timing function such as animation-timing-function: ease-in-out alongside animation-name and animation-duration controls the pacing of the motion, easing it in and out rather than moving at a constant linear speed.
- Always specify a defined duration value, or your animation will default to 0 and fail to run.
- Accepted values:
- animation-name: <keyframes-name> — the @keyframes rule to use
- animation-duration: <time> — length of one cycle
- animation-timing-function: ease | linear | cubic-bezier() — pacing
- animation-delay: <time> — wait before starting
- animation: name 2s ease 0s — shorthand
Example: Applying custom Animations
<style>
@keyframes fade {
from { opacity: 0; }
to { opacity: 1; }
}
.box {
background: teal;
color: white;
padding: 10px;
animation-name: fade;
animation-duration: 1s;
animation-timing-function: ease-in-out;
}
</style>
<div class="box">Fades in on load</div>
Controlling Iteration Counts
By default, animations run exactly once and then stop. The animation-iteration-count property allows you to specify how many times you want the animation to repeat, or set it to infinite to loop it forever.
The animation-play-state property lets you pause and resume an animation at any point, most commonly toggled to paused on :hover so a user can freeze a looping animation to inspect it.
- Use infinite loops on subtle loading spinners to keep them spinning continuously.
- Accepted values:
- animation-iteration-count: 1 — default, plays once
- animation-iteration-count: <number> — plays that many times (decimals allowed)
- animation-iteration-count: infinite — loops forever
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Controlling Iteration Counts
<style>
@keyframes spin {
to { transform: rotate(360deg); }
}
.spinner {
background: coral;
color: white;
width: 60px;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
animation: spin 1s infinite;
}
.spinner:hover {
animation-play-state: paused;
}
</style>
<div class="spinner">Spins forever, hover to pause</div>
Controlling Directions
The animation-direction property specifies how your animation timeline should play, offering normal, reverse (plays backward), alternate (plays forward then backward), and alternate-reverse options.
- Use the alternate direction setting to create smooth, natural-looking back-and-forth animations.
- Accepted values:
- normal — default, forwards each cycle
- reverse — backwards each cycle
- alternate — forwards then backwards
- alternate-reverse — backwards then forwards
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Controlling Directions
<style>
@keyframes slide {
from { transform: translateX(0); }
to { transform: translateX(50px); }
}
.box {
background: royalblue;
color: white;
padding: 10px;
animation: slide 1s alternate infinite;
}
</style>
<div class="box">Plays forward, then backward, then forward again</div>
Managing Fill Modes (animation-fill-mode)
By default, when an animation finishes playing, the element snaps instantly back to its original pre-animated styles. The animation-fill-mode property allows you to instruct the browser to apply the animation's start or end styles permanently after it plays.
- Set your fill-mode to forwards to make your elements keep their final animated styles after the animation finishes playing.
- Accepted values:
- none — default, no styles applied outside the animation
- forwards — keeps the last keyframe after the end
- backwards — applies the first keyframe during the delay
- both — applies both behaviors
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Managing Fill Modes (animation-fill-mode)
<style>
@keyframes slideIn {
from { transform: translateX(-100px); }
to { transform: translateX(0); }
}
.box {
background: royalblue;
color: white;
padding: 10px;
animation: slideIn 1s forwards;
}
</style>
<div class="box">Keeps its final position after the animation ends</div>
- Forgetting to specify a defined animation-duration, leaving the animation disabled at 0.
- Allowing elements to snap back awkwardly by neglecting animation-fill-mode settings.
- Attempting to auto-play rapid, flashing animations that can distract users or trigger accessibility issues.
- The @keyframes rule is used to define the states and timeline of your custom animation.
- Configure your animations using properties: animation-name, duration, iteration-count, direction, and fill-mode.
- Set your animation-fill-mode to forwards to make your elements keep their final animated styles after the animation finishes playing.
Standard CSS keyframe animations and core control properties are supported natively by all modern web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Gradients
- CSS Shadows
- CSS Filters
- CSS Blend Modes
- CSS Transforms
- CSS 2D Transforms
- CSS 3D Transforms
- CSS Transitions
- CSS Animations
- CSS Image Styling
- CSS Image Effects
- CSS Hover Overlays
- CSS Image Modal
- CSS Image Centering
- CSS Image Shapes
- CSS Buttons
- CSS Columns
- CSS Clip Path
- CSS Shapes
- CSS Scroll Snap
- CSS Architecture: BEM
- CSS Custom Properties