What Are CSS Animations?
CSS animations allow you to change CSS property values over time without requiring
JavaScript to control every frame. An animation is defined with the
@keyframes at-rule and then attached to an element
using the animation property or its individual
animation properties.
Unlike a simple CSS transition, an animation can contain multiple intermediate stages. You can define a starting state, one or more intermediate keyframes, and an ending state. This makes animations useful for loading indicators, entrance effects, repeated movement, attention cues, decorative motion, and complex visual sequences.
.box {
animation: slideIn 600ms ease-out;
}
@keyframes slideIn {
from {
transform: translateX(-40px);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
CSS animations are controlled by the browser's animation system, which means you can describe the desired sequence declaratively rather than manually updating styles with JavaScript on every frame.
How Do CSS Animations Work?
A CSS animation has two main parts: the animation configuration applied to the element
and the sequence of styles defined inside @keyframes.
Animation configuration
Controls duration, timing, repetition, direction, delay, fill behavior, and playback state.
@keyframes
Defines the visual states the browser should use throughout the animation.
</pre>
<p>
The <code class="text-cyan-300">0%</code> keyframe represents the beginning of the
animation and <code class="text-cyan-300">100%</code> represents the end.
You can also use the aliases <code class="text-cyan-300">from</code> and
<code class="text-cyan-300">to</code>.
</p>
</section>
<section class="space-y-5">
<h2 class="text-2xl font-bold text-white">What Is @keyframes in CSS?</h2>
<p>
The <code class="text-cyan-300">@keyframes</code> at-rule defines the sequence of an
animation. Each keyframe specifies the CSS property values that should apply at a
particular point in the animation.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:@keyframes moveBox {
0% {
transform: translateX(0);
}
50% {
transform: translateX(200px);
}
100% {
transform: translateX(0);
}
}
You are not limited to two states. Intermediate percentages let you create more complex sequences.
</pre>
<p>
The browser calculates the animation between defined keyframes according to the selected
timing function and the animation property's rules.
</p>
</section>
<section class="space-y-5">
<h2 class="text-2xl font-bold text-white">What Is animation-name?</h2>
<p>
The <code class="text-cyan-300">animation-name</code> property specifies which
<code class="text-cyan-300">@keyframes</code> rule should control an element.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.card {
animation-name: fadeIn;
}
The name connects the element to the keyframe sequence. If the name does not match an
available @keyframes rule, the intended animation
will not run.
Multiple animation names can be supplied as a comma-separated list.
</pre>
<p>
When multiple animations are declared, the other animation properties can also provide
corresponding comma-separated values.
</p>
</section>
<section class="space-y-5">
<h2 class="text-2xl font-bold text-white">What Is animation-duration?</h2>
<p>
The <code class="text-cyan-300">animation-duration</code> property defines how long one
animation cycle takes.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:.fast {
animation-duration: 200ms;
}
.normal {
animation-duration: 800ms;
}
.slow {
animation-duration: 2s;
}
The default duration is 0s. That means an animation
configured without a non-zero duration will not produce visible time-based motion, even
though animation events can still occur.
When an animation uses several names, you can provide several durations.
</pre>
</section>
<section class="space-y-5">
<h2 class="text-2xl font-bold text-white">What Is animation-timing-function?</h2>
<p>
The <code class="text-cyan-300">animation-timing-function</code> controls how an
animation progresses between keyframes. It defines the acceleration curve rather than
the total duration.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:.linear {
animation-timing-function: linear;
}
.ease {
animation-timing-function: ease;
}
.ease-in {
animation-timing-function: ease-in;
}
.ease-out {
animation-timing-function: ease-out;
}
.ease-in-out {
animation-timing-function: ease-in-out;
}
You can also define a custom cubic Bézier curve.
</pre>
<p>
A timing function can also be defined separately for different keyframe intervals.
This allows one portion of an animation to have different motion characteristics from another.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:@keyframes complexMotion {
0% {
transform: translateX(0);
animation-timing-function: ease-out;
}
60% {
transform: translateX(200px);
}
100% {
transform: translateX(120px);
}
}
What Is animation-delay?
The animation-delay property defines how long the
browser waits before starting the animation.
</pre>
<p>
A positive delay creates a wait before the animation starts. A negative delay can make
the animation behave as though it has already been running for part of its duration.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:.loader {
animation:
spin 2s linear infinite;
animation-delay: -500ms;
}
Negative delays are useful when multiple animated elements should appear visually offset without requiring separate keyframe definitions.
What Is animation-iteration-count?
The animation-iteration-count property determines
how many times an animation cycle should repeat.
</pre>
<p>
The <code class="text-cyan-300">infinite</code> value creates a continuously repeating
animation. This is useful for loading indicators and decorative effects, but continuous
motion should be used carefully.
</p>
</section>
<section class="space-y-5">
<h2 class="text-2xl font-bold text-white">What Is animation-direction?</h2>
<p>
The <code class="text-cyan-300">animation-direction</code> property controls whether
the animation runs forward, backward, or alternates direction between iterations.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:.normal {
animation-direction: normal;
}
.reverse {
animation-direction: reverse;
}
.alternate {
animation-direction: alternate;
}
.alternate-reverse {
animation-direction: alternate-reverse;
}
With alternate, the first iteration runs forward and
the next runs backward, producing a back-and-forth effect when the animation repeats.
</pre>
</section>
<section class="space-y-5">
<h2 class="text-2xl font-bold text-white">What Is animation-fill-mode?</h2>
<p>
The <code class="text-cyan-300">animation-fill-mode</code> property controls whether
an animation's styles apply before the animation starts, after it finishes, or both.
</p>
<div class="grid gap-4 md:grid-cols-2">
<div class="rounded-xl border border-slate-800 bg-slate-900/60 p-5">
<h3 class="font-semibold text-white">none</h3>
<p class="mt-2 text-sm text-slate-400">
The animation does not apply its styles before or after its active period.
</p>
</div>
<div class="rounded-xl border border-slate-800 bg-slate-900/60 p-5">
<h3 class="font-semibold text-white">forwards</h3>
<p class="mt-2 text-sm text-slate-400">
The final keyframe is retained after the animation completes.
</p>
</div>
<div class="rounded-xl border border-slate-800 bg-slate-900/60 p-5">
<h3 class="font-semibold text-white">backwards</h3>
<p class="mt-2 text-sm text-slate-400">
The appropriate starting keyframe can apply during the delay period.
</p>
</div>
<div class="rounded-xl border border-slate-800 bg-slate-900/60 p-5">
<h3 class="font-semibold text-white">both</h3>
<p class="mt-2 text-sm text-slate-400">
Combines the before-start and after-finish behavior of backwards and forwards.
</p>
</div>
</div>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:.dialog {
animation:
enter 300ms ease-out forwards;
}
Fill mode is especially useful for entrance animations where the element should remain in the final animated state after the animation ends.
What Is animation-play-state?
The animation-play-state property determines whether
an animation is running or paused.
</pre>
<p>
You can combine this with user interaction or application state. For example, an animation
may normally run while a component is visible and pause while the user interacts with it.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:.preview {
animation: pulse 2s ease-in-out infinite;
}
.preview:hover {
animation-play-state: paused;
}
How Does the animation Shorthand Work?
The animation shorthand combines most animation
configuration properties into a single declaration.
</pre>
<p>
In this example, the values describe the animation name, duration, timing function,
delay, iteration count, direction, and fill mode.
</p>
<p>
Multiple animations can also be defined using commas.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:.element {
animation:
fadeIn 500ms ease,
pulse 2s ease-in-out infinite;
}
When defining multiple animations, keep the order of each animation's values consistent so that the relationship between names and their configuration remains easy to understand.
How Do CSS Animations Use Multiple Keyframes?
One of the biggest differences between transitions and animations is the ability to define multiple intermediate states.
</pre>
<p>
This creates an entrance, a visible resting period, and an exit sequence inside a single
animation definition.
</p>
</section>
<section class="space-y-5">
<h2 class="text-2xl font-bold text-white">What CSS Properties Are Commonly Animated?</h2>
<p>
Many CSS properties can participate in animations when their values support meaningful
animation or interpolation. Common examples include
<code class="text-cyan-300">transform</code>,
<code class="text-cyan-300">opacity</code>,
colors, lengths, filters, and other visual properties.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:@keyframes cardEffect {
from {
opacity: 0;
transform: translateY(20px);
filter: blur(4px);
}
to {
opacity: 1;
transform: translateY(0);
filter: blur(0);
}
}
Not every CSS property interpolates continuously. Some properties use discrete animation behavior and change between states instead of smoothly moving through intermediate values.
For performance-sensitive motion, transforms and opacity are commonly useful choices, but actual rendering performance depends on the page, browser, device, and the properties involved. Measure complex animations rather than relying on blanket assumptions.
How Do CSS Animations Differ from Transitions?
| Feature | CSS Transition | CSS Animation |
|---|---|---|
| Main idea | Move between states | Run a defined keyframe sequence |
| Keyframes | No | Yes |
| Repetition | Not directly | Yes |
| Automatic sequence | Normally triggered by a value change | Can run automatically after being applied |
Use a transition when the UI moves from one state to another, such as a button changing on hover. Use an animation when the interface needs a repeatable or multi-stage sequence.
How Do You Create a CSS Loading Spinner?
A loading spinner is a simple example of a repeating CSS animation. The element is rotated continuously around its center.
</pre>
<p>
The animation repeats indefinitely because of
<code class="text-cyan-300">infinite</code>. For production interfaces, consider how
the animation interacts with reduced-motion preferences and whether users can understand
the loading state without relying exclusively on motion.
</p>
</section>
<section class="space-y-5">
<h2 class="text-2xl font-bold text-white">How Do You Create a Fade-In Animation?</h2>
<p>
A fade-in animation usually changes the element's opacity from transparent to fully visible.
It can also be combined with a small transform to create a more natural entrance effect.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:.content {
animation: fadeUp 500ms ease-out both;
}
@keyframes fadeUp {
from {
opacity: 0;
transform: translateY(16px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
The both fill mode keeps the appropriate starting
styles during the delay and the final styles after the animation finishes.
How Can You Stagger Multiple CSS Animations?
Multiple elements can use the same keyframes with different animation delays to create a staggered sequence. This is useful for lists, navigation items, skeleton interfaces, and repeated content.
</pre>
<p>
The keyframe definition remains shared, while the delay controls when each instance starts.
This keeps the CSS smaller than defining separate animations for every item.
</p>
</section>
<section class="space-y-5">
<h2 class="text-2xl font-bold text-white">What Is animation-composition?</h2>
<p>
When multiple animations affect the same CSS property, the
<code class="text-cyan-300">animation-composition</code> property controls how the animation
effect combines with the underlying property value.
</p>
<p>
Common values are <code class="text-cyan-300">replace</code>,
<code class="text-cyan-300">add</code>, and
<code class="text-cyan-300">accumulate</code>.
The default is <code class="text-cyan-300">replace</code>.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:.icon {
animation:
move 2s ease-in-out infinite,
rotate 1s linear infinite;
animation-composition:
replace,
add;
}
Composition becomes useful when separate animation effects need to contribute to the same property rather than one animation simply replacing another.
What Is animation-timeline?
Traditional CSS animations progress according to the document's time-based animation
timeline. The animation-timeline property can associate
an animation with a different timeline, including scroll-progress and view-progress timelines.
</pre>
<p>
This enables scroll-driven animation patterns where animation progress is controlled by
scrolling or an element's visibility rather than simply by elapsed time.
</p>
<p>
Scroll-driven and view-progress animation features are part of modern CSS, but
<code class="text-cyan-300">animation-timeline</code> is not currently considered
Baseline because browser support is not uniform across widely used browsers. For production
applications, use feature detection and an acceptable non-animated fallback where required.
</p>
</section>
<section class="space-y-5">
<h2 class="text-2xl font-bold text-white">Do CSS Animations Respect Reduced Motion?</h2>
<p>
CSS animations should be designed with accessibility in mind. Users can request reduced
motion through the <code class="text-cyan-300">prefers-reduced-motion</code> media feature.
Your stylesheet can reduce or disable non-essential animation in response.
</p>
<pre class="overflow-x-auto rounded-xl bg-slate-900 p-5 text-sm text-slate-200"><code:.hero {
animation: fadeUp 500ms ease-out both;
}
@media (prefers-reduced-motion: reduce) {
.hero {
animation: none;
}
}
For some interfaces, replacing motion with a simpler fade or shorter effect is preferable to removing all visual feedback. The correct implementation depends on whether the animation is decorative or communicates meaningful state.
Avoid using rapid, repetitive, or large-scale motion when it is not necessary for the user's task.
Why Is My CSS Animation Not Working?
Several common mistakes can prevent an animation from appearing to work.
The keyframe name does not match
animation-name must reference the intended @keyframes rule.
Duration is zero
Without a non-zero animation duration, there is no visible time-based motion.
The animation is paused
Check animation-play-state and any state-specific rules that may override it.
Another animation overrides the same property
Multiple animations targeting the same property require careful control of their composition and ordering.
The property is not behaving as expected
Some properties use discrete animation behavior rather than continuously interpolated values.
Browser DevTools can help by showing computed animation properties, keyframe rules, and the styles currently winning in the cascade.
How Do CSS Animations Interact with the Cascade?
It is important to remember that @keyframes rules do
not behave like ordinary style declarations in the cascade. If multiple
@keyframes definitions use the same animation name,
the CSS cascade determines which keyframe rule has precedence rather than merging the
individual keyframes together.
The browser does not combine these into one keyframe animation containing both sets of
properties. Only the winning @keyframes rule for the
name is used.
Are CSS Animations Better Than JavaScript Animations?
CSS animations are often the simplest choice for presentation-oriented motion such as hover effects, loading indicators, entrance effects, and repeated decorative sequences. They let the browser control the animation declaratively and can be optimized efficiently.
JavaScript becomes useful when animation needs to respond to complex application state, calculations, user-driven interactions, physics, external data, or more advanced control through the Web Animations API.
The choice should be based on the problem being solved rather than a rule that one technology is always faster or better.
Common CSS Animation Mistakes
Animating too many properties
Complex animations can cause unnecessary rendering work. Animate only what contributes to the intended visual effect.
Using infinite animation everywhere
Continuous motion can distract users and may create accessibility problems when it is not necessary.
Forgetting fill-mode
An entrance animation may appear to reset immediately after completing if its final state is not retained when the design requires it.
Ignoring reduced motion
Non-essential motion should provide a reduced-motion experience when appropriate.
Confusing animations with layout
Animations can create visual movement, but they do not replace proper layout with Flexbox, Grid, positioning, or normal document flow.
Best Practices for CSS Animations
Use meaningful keyframes
Define only the states needed to communicate the intended motion.
Prefer transform and opacity for motion
They are often good choices for visual movement, but profile actual performance for complex pages.
Use animation-fill-mode intentionally
Keep the final state when an entrance or exit effect should not visually reset.
Avoid unnecessary continuous motion
Reserve infinite animations for situations where ongoing motion provides a useful purpose.
Support reduced motion
Use prefers-reduced-motion to reduce or disable non-essential animation.
Test real devices
Animation behavior and performance can differ across browsers, devices, screen sizes, and system preferences.
Final Takeaway
CSS animations provide a declarative way to create multi-stage motion using
@keyframes and the animation properties.
The most important properties to understand are
animation-name,
animation-duration,
animation-timing-function,
animation-delay,
animation-iteration-count,
animation-direction,
animation-fill-mode, and
animation-play-state.
Modern CSS also provides more advanced capabilities such as
animation-composition and
animation-timeline. These extend what CSS animations
can do, including combining animation effects and connecting animation progress to
scrolling or element visibility.
Good CSS animation design is not about adding movement everywhere. The goal is to use motion deliberately to communicate state, guide attention, improve feedback, or enhance the interface while keeping performance and accessibility in mind.