Explain it simply
Explain it simply
Flip a light switch and the lamp is instantly on. Turn a dimmer and the light rises gradually. In CSS, whenever a state changes, through hover, focus or a new class, values jump instantly like the switch by default. A transition turns that into a dimmer: "when background-color changes, take 0.25 seconds to get there".
Transitions are not just decoration: they tell users that a press registered or where a menu came from. Animate everything, though, and the page becomes a disco floor, which is a real problem for people who get dizzy from motion.
Problem → Why → Solution
The problem: the whole page stutters when a panel opens
A side panel opens by changing width from 0 to 320px and its left position, all under transition: all 600ms. It looks fine on your laptop, but on an inexpensive Android phone the panel stutters and the text beside it re-wraps on every frame. Worse, because of transition: all, a responsive font-size change now animates text that was never meant to move.
.panel {
position: fixed;
left: -320px;
width: 0;
transition: all 600ms; /* every changing property animates */
}
.panel.open {
left: 0;
width: 320px; /* layout is recalculated every frame */
}Why does it stutter?
Changing width, left, height or margin makes the browser recompute layout (every element's position and size) and repaint. At 60fps a frame has about 16ms, which weak phones cannot meet. transform: translateX() and opacity leave layout alone; they move or fade an already painted layer, which browsers can usually hand to the compositor (GPU). And transition: all animates properties you never intended to animate.
Core concept: interpolating when a computed value changes
A transition starts when a property's computed value changes: entering :hover, adding a class, JavaScript setting a style. The browser then calculates (interpolates) an in-between value for every frame from the old value to the new one. Four longhands describe it: transition-property (what), transition-duration (how long), transition-timing-function (the speed curve: ease, linear, ease-out, cubic-bezier()) and transition-delay (how long to wait first). In the shorthand, the first time value is the duration and the second is the delay, and several properties are separated by commas.
Where you write the transition matters. On the base state (.button), it runs both when the pointer enters and when it leaves. Inside the :hover rule only, it runs on the way in, but on the way out the transition no longer applies, so the value snaps back instantly. Only properties with in-between values (colors, lengths, transform, opacity) move smoothly; discrete properties such as display switch at once, which is why an element going from display: none to block appears instantly instead of fading in.
| Property changed | Work per frame | For animation |
|---|---|---|
| width, height, top, left, margin, padding | Layout + paint + composite | Avoid |
| color, background-color, box-shadow | Paint + composite | Fine if brief |
| transform, opacity | Composite only (usually) | Prefer |
People who are sensitive to motion, for example with vestibular disorders, can turn on "Reduce motion" in their operating system, and CSS detects it with @media (prefers-reduced-motion: reduce). Inside that query, remove movement such as transforms, while feedback that does not move, like a color change or a fade, can stay. Reduced motion is not the same as no feedback: a button that gives no visible response to a press is a usability bug of its own, so swap motion for a calmer signal rather than deleting the signal.
- State changes: hover, focus, class
- Old and new computed values compared
- Listed in transition-property? Start
- Each frame draws an in-between value
A transition only describes the journey between two states; separate rules define the states themselves.
General pattern
.element {
transition: property duration timing-function delay,
other-property duration timing-function;
}
.element:state { property: new-value; } /* the change that triggers it */
@media (prefers-reduced-motion: reduce) {
.element:state { /* drop movement, keep non-moving feedback */ }
}Put the transition on the base selector and only the new value in the state rule. Name each property explicitly, and always include a reduced-motion block.
Example: a button that changes color and lifts on hover
This example puts the transition on the base state and names exactly two properties. Move the pointer onto the button and away again: it eases smoothly in both directions.
.button {
background-color: #77f5ff;
transform: translateY(0);
transition: transform 0.25s ease, background-color 0.25s ease;
}
.button:hover {
background-color: #ff4dcd;
transform: translateY(-2px);
}Code breakdown
- starting values
transform: translateY(0);Spells out the starting point. The browser could interpolate from transform: none too, but matching functions make the intent obvious.
- transition
transition: transform 0.25s ease, background-color 0.25s ease;Two properties, comma-separated, each over 250ms with ease. Avoiding all means a padding change added later will not animate by accident, and living in the base rule makes it work in both directions.
- :hover
.button:hover {This rule only sets the new values; the transition does not need repeating here. Adding .button:focus-visible to the selector would give keyboard users the same feedback.
- lift
transform: translateY(-2px);Moving up 2px does not touch layout, so neighbors stay still and the animation is cheap. background-color needs a repaint, but over 250ms that is no problem.
On hover, the background eases from cyan (#77f5ff) to pink (#ff4dcd) and the button rises 2px over 250ms; when the pointer leaves, it eases back the same way.Timing functions and interrupted transitions
| Timing function | Feels like | Good for |
|---|---|---|
| ease (default) | Quick start, gentle finish | General hovers |
| ease-out | Fastest at the start, settles slowly | Things entering the screen |
| ease-in | Slow start, fast exit | Things leaving the screen |
| linear | Constant speed | Progress bars, spinners |
If the pointer leaves halfway through a hover transition, the element does not jump to the start; a new transition begins from the current in-between value. When it is heading back to where it began, the browser shortens the return trip: reversing at 500ms into a 1s transition takes only about 500ms to get back. That keeps the UI responsive when users sweep the pointer across it quickly.
In real projects
Where this shows up in real projects
Most product UI transitions fall between 150ms and 300ms: button hovers, focus rings, dropdown fades, toasts. Below about 100ms the change is barely noticed; above 500ms the interface starts to feel slow. Design systems centralize timing with custom properties such as --duration-fast: 150ms and --ease-out: cubic-bezier(...), so the whole app moves consistently.
Many teams add a reduced-motion safety net to the global stylesheet: inside @media (prefers-reduced-motion: reduce), they set a tiny transition-duration and animation-duration with !important on *, *::before and *::after. They use a value like 0.01ms rather than 0 so JavaScript waiting for the transitionend event still works. To check performance, record in the DevTools Performance panel and look for Layout work on every frame.
Common mistakes
Common mistakes
1. Writing the transition only inside :hover
With .card:hover { transition: transform 300ms; transform: translateY(-4px); }, entering is smooth but the card snaps back the instant the pointer leaves, because the :hover rule, and its transition, no longer applies. Move the transition to .card.
2. Animating layout properties
Opening a drawer by animating left or width recomputes layout every frame and stutters on phones. Keep the drawer at its final size and transition transform from translateX(-100%) to translateX(0).
3. Trying to fade in from display: none
Switching a class from display: none; opacity: 0 to display: block; opacity: 1 shows the element instantly with no fade, because a display: none element has no box to animate. Combine opacity with visibility when you need a fade.
Best practices
Best practices
- Name each property instead of writing transition: all.
- Prefer transform for movement and opacity for appearing and disappearing.
- Keep UI feedback within 150-300ms and use a curve that slows at the end, such as ease-out.
- Every time you add a transition, decide what happens under prefers-reduced-motion: reduce.
Mini challenge
Mini challenge: a cheap, motion-aware card
Build a card that lifts on hover or keyboard focus while a shadow and its action buttons fade in. Only transform and opacity may be transitioned.
- .card rises 4px on :hover and :focus-within over 200ms ease-out
- Fade the shadow in through the opacity of an ::after layer, not by transitioning box-shadow
- .actions goes from opacity 0 to 1, and keyboard users reaching it with Tab see it too
- Under prefers-reduced-motion: reduce, drop the lift but keep both fades
<style>
/* TODO: .card - positioning anchor, 240px wide, padding, border, 12px radius,
transition for transform only */
/* TODO: .card::after - a full-size layer carrying the shadow, invisible at
first, fading with opacity, ignoring the pointer */
/* TODO: .card .actions - hidden with opacity, fading in */
/* TODO: hover AND focus-within: lift the card, show the shadow and actions */
/* TODO: reduced motion: keep the fades, remove the lift */
</style>
<div class="card">
<h3>Trip to Bagan</h3>
<div class="actions"><button>Edit</button> <button>Share</button></div>
</div>Expected: On hover, within 200ms the card reaches translateY(-4px) and the shadow layer and actions go from opacity 0 to 1; focusing Edit with Tab does the same. With reduced motion on, the card stays at transform: none while the shadow and actions still fade in.
Show hint
Make ::after cover the card with position: absolute; inset: 0; border-radius: inherit; and give that layer a fixed box-shadow, so only its opacity changes. In the reduced-motion block, just set transform back to none for the hover/focus-within state.
Show solution
<style>
.card {
position: relative;
width: 240px;
padding: 16px;
border: 1px solid #ddd;
border-radius: 12px;
transition: transform 200ms ease-out;
}
.card::after {
content: "";
position: absolute;
inset: 0;
border-radius: inherit;
box-shadow: 0 8px 20px rgb(0 0 0 / 0.15);
opacity: 0;
transition: opacity 200ms ease-out;
pointer-events: none;
}
.card .actions {
opacity: 0;
transition: opacity 200ms ease-out;
}
.card:hover,
.card:focus-within { transform: translateY(-4px); }
.card:hover::after,
.card:focus-within::after,
.card:hover .actions,
.card:focus-within .actions { opacity: 1; }
@media (prefers-reduced-motion: reduce) {
.card:hover,
.card:focus-within { transform: none; }
}
</style>
<div class="card">
<h3>Trip to Bagan</h3>
<div class="actions"><button>Edit</button> <button>Share</button></div>
</div>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- A transition interpolates in-between values when a computed value changes, following its property, duration, timing function and delay.
- Put transitions on the base state so they run both ways, and avoid transition: all.
- transform and opacity skip layout and stay cheap; width, top and margin force layout on every frame.
- Under prefers-reduced-motion: reduce, remove movement but keep feedback such as fades and color changes.
CSS transitions — MDN Web Docs