CSS Transitions
A CSS transition smoothly animates an element between two states — typically triggered by a user action like hover, focus, or a class change. Instead of snapping instantly from state A to state B, the browser interpolates values over a specified duration following a timing curve. Four sub-properties give you complete control: what to animate, how long, how fast/slow at each point, and when to start.
The transition Property
The transition shorthand takes
four values in order. All four are optional — only the duration is
commonly required:
what to animate
how long
speed curve
when to start
/* Shorthand */
.el { transition: property duration timing-function delay; }
/* Examples */
.el { transition: all .3s ease; } /* all animatable properties */
.el { transition: background .3s ease; } /* background only */
.el { transition: transform .3s ease-out .1s; }
/* Individual sub-properties */
.el {
transition-property: transform, opacity;
transition-duration: .3s, .2s;
transition-timing-function: ease-out, linear;
transition-delay: 0s, .05s;
}
/* Multiple transitions — comma separated */
.el {
transition:
background .3s ease,
transform .3s cubic-bezier(.34,1.56,.64,1),
box-shadow .3s ease;
}
/* IMPORTANT: put transition on BASE state, not :hover */
.btn { transition: background .3s ease; } /* ✓ correct */
.btn:hover { transition: background .3s ease; } /* ✗ only animates on hover, snaps back */
transition-duration — How Long
Duration sets how long the transition takes. Accepts seconds
(s) or milliseconds (ms). Human perception is key: 100–300ms feels snappy, 300–600ms
feels smooth, over 1s starts feeling slow for UI.
transition-timing-function — The Speed Curve
The timing function controls how fast or slow the
animation moves at each point during its duration — not how long
it takes. All keyword values are shortcuts for
cubic-bezier() curves:
/* Keyword values (shortcuts for cubic-bezier) */
.el { transition-timing-function: linear; } /* cubic-bezier(0,0,1,1) */
.el { transition-timing-function: ease; } /* cubic-bezier(.25,.1,.25,1) */
.el { transition-timing-function: ease-in; } /* cubic-bezier(.42,0,1,1) */
.el { transition-timing-function: ease-out; } /* cubic-bezier(0,0,.58,1) */
.el { transition-timing-function: ease-in-out; } /* cubic-bezier(.42,0,.58,1) */
.el { transition-timing-function: step-start; } /* instant jump at start */
.el { transition-timing-function: step-end; } /* instant jump at end */
/* custom cubic-bezier(x1, y1, x2, y2) */
.spring { transition-timing-function: cubic-bezier(.34,1.56,.64,1); } /* overshoot */
.snap { transition-timing-function: cubic-bezier(.2,.8,.2,1); } /* fast snap */
.gentle { transition-timing-function: cubic-bezier(.4,0,.2,1); } /* Material Design */
.decelerate{ transition-timing-function: cubic-bezier(0,0,.2,1); } /* elements entering screen */
.accelerate{ transition-timing-function: cubic-bezier(.4,0,1,1); } /* elements leaving screen */
/* Tool: https://cubic-bezier.com for visual editor */
transition-delay — When to Start
transition-delay waits before
starting the transition. Staggering delays on sibling elements
creates a cascading wave effect without JavaScript:
transition-delay: -.3s starts
the transition already 0.3s into its progress. Useful for making a
long transition appear to start from a mid-point instantly.
/* Basic delay */
.el { transition: transform .3s ease .15s; } /* wait 0.15s then animate */
/* Stagger — different delay per item */
.item { transition: transform .3s ease, opacity .3s ease; }
.item:nth-child(1){ transition-delay: 0s; }
.item:nth-child(2){ transition-delay: .1s; }
.item:nth-child(3){ transition-delay: .2s; }
.item:nth-child(4){ transition-delay: .3s; }
/* Negative delay — start already mid-transition */
.el { transition: transform 1s ease -.3s; }
/* Asymmetric — different on hover vs leave */
.tooltip { transition: opacity .2s ease .5s; } /* show after 0.5s */
.tooltip.visible{ transition: opacity .2s ease 0s; } /* hide immediately */
Transitioning Multiple Properties
You can transition different properties at different speeds and
with different easings in a single declaration. Use
all as a shortcut — but it's
less efficient and harder to control:
transition: background .3s ease, transform .3s spring, border-radius .3s ease, box-shadow .3s ease;
/* Multiple specific transitions */
.card {
transition:
background .3s ease,
transform .3s cubic-bezier(.34,1.56,.64,1),
box-shadow .3s ease,
border-color .2s ease;
}
.card:hover {
background: #264de4;
transform: translateY(-6px) scale(1.02);
box-shadow: 0 12px 28px rgba(38,77,228,.3);
border-color: #264de4;
}
/* transition: all — handy shortcut but broad */
.btn { transition: all .25s ease; }
/* ⚠ all includes width, height, font-size — may cause
unexpected layout transitions. Be specific when possible */
/* Different duration per property */
.link {
transition:
color .15s ease, /* fast color change */
transform .3s ease-out; /* slower movement */
}
/* Reduced motion — always handle */
@media (prefers-reduced-motion: reduce) {
* { transition-duration: .01ms !important; }
}
Real-World UI Patterns
These are the most common CSS transition patterns used in production UI. Hover each element to see the transition:
Transition Property Reference
| Property | Values | Default |
|---|---|---|
| transition-property |
all, property name,
none
|
all |
| transition-duration |
0s,
200ms,
.3s…
|
0s |
| transition-timing-function | ease, linear, ease-in, ease-out, ease-in-out, cubic-bezier(), steps() | ease |
| transition-delay |
0s,
.1s, negative values…
|
0s |
| transition (shorthand) | property duration timing delay (comma-separated for multiple) | — |
Not all properties are animatable. Properties with discrete
values (like display,
visibility,
position,
font-family) cannot
interpolate — they snap instantly. CSS Level 4 introduces
transition-behavior: allow-discrete
that lets you transition between discrete values (including
display: none ↔ block),
enabling proper enter/exit animations without JavaScript.
Try It Yourself
All transition patterns in one editor — hover each element to see them live, and experiment by changing values:
Quick Check — Chapter 44
:hover, the forward
animation (entering hover) is smooth but the reverse animation
(leaving hover) snaps instantly — because when the mouse
leaves, the :hover state
is removed and with it the transition rule, so the browser has
no transition to apply on the way back.When the transition is on the base state, it applies in both directions: the property smoothly interpolates whether the state is being entered or exited.
Example:
.btn { transition: background .3s ease; }
— smooth both ways ✓.btn:hover { transition: background .3s ease; }
— smooth hover-in, snap hover-out ✗Exception: you can deliberately use different transitions for enter vs exit by defining transitions on both:
.btn { transition: background .5s ease; }
— slow exit.btn:hover { transition: background .2s ease; }
— fast enter
ease-in — starts slow, ends fast (accelerates). Like a car pulling away from a stop. Feels like the element is "reluctant" to move, then rushes to its destination. Works well for elements leaving the screen.
ease-out — starts fast, ends slow (decelerates). Like a car braking to a stop. Feels natural and snappy for elements entering the screen or responding to user input. The most natural-feeling easing for UI interactions.
ease-in-out — starts slow, reaches peak speed in the middle, ends slow. Symmetric ease. Good for elements that move from one position to another and need to feel balanced and deliberate (like sliding panels).
In practice, ease-out or ease-in-out are most commonly used for UI interactions. Pure
ease-in rarely feels right
for hover effects because the initial slow start makes the UI
feel unresponsive.
cubic-bezier(x1, y1, x2, y2)
defines a speed curve using two control points in a unit
square where (0,0) is start and (1,1) is end:— x1, y1: position of the first control point (affects start of curve)
— x2, y2: position of the second control point (affects end of curve)
Key rules: x1 and x2 must be between 0 and 1 (they control time, which can't go backwards). y1 and y2 can go outside 0–1, which creates overshoot effects.
Common values:
—
cubic-bezier(0,0,1,1) =
linear—
cubic-bezier(.25,.1,.25,1)
= ease—
cubic-bezier(.34,1.56,.64,1)
= spring (y2=1.56 overshoots then settles)—
cubic-bezier(.4,0,.2,1) =
Material Design standard easingUse cubic-bezier.com to visually design and test custom curves.
transition-delay with
different values on sibling elements. Each element has the
same transition duration and easing, but starts progressively
later:.item { transition: transform .3s ease, opacity .3s ease;
}.item:nth-child(1) { transition-delay: 0s; }.item:nth-child(2) { transition-delay: .08s; }.item:nth-child(3) { transition-delay: .16s; }.item:nth-child(4) { transition-delay: .24s; }Trigger it by changing state on the parent:
.parent:hover .item { transform: translateY(0); opacity: 1;
}This creates a wave effect where items cascade in succession. The stagger interval (the difference between delays) controls how pronounced the cascade looks — 50–100ms per item is typical.
Transitions:
— Animate between two states (A → B)
— Require a trigger (hover, focus, class change, JS)
— One step — start value and end value
— Cannot loop on their own
— Simpler syntax: just
transition: property duration easing— Best for: hover effects, state changes, interactive UI
Animations (@keyframes):
— Define multiple states (A → B → C → D…)
— Run automatically — no trigger needed
— Can loop indefinitely
— Can play forward, backward, alternate
— Can be paused, delayed, run a set number of times
— Best for: loaders, decorative motion, background effects, on-load animations
Rule of thumb: if an animation happens because the user did something → transition. If it plays on its own → @keyframes animation.
cubic-bezier(.34, 1.56, .64, 1)
timing function produce?
transition: transform .4s ease-out .2s, what does .2s control?
-
transition shorthand:
transition: property duration timing delay. Put on the base state — not:hover— so the animation runs both ways. -
transition-property: specify which property to
animate. Use
allas a shortcut but be cautious — it catches layout-triggering properties too. - transition-duration: 100–300ms feels snappy for micro-interactions. 300–500ms for larger state changes. Over 600ms starts feeling slow.
-
Timing functions:
ease(default),ease-out(most natural for UI entering),ease-in-out(balanced). Custom:cubic-bezier(.34,1.56,.64,1)for spring effects. -
transition-delay: stagger items by incrementing
delay (
0s, .08s, .16s…). Negative delay starts mid-animation. -
Multiple transitions: comma-separate for
independent easing per property:
transition: background .3s ease, transform .3s cubic-bezier(…). -
Discrete properties:
display,visibilitycan't interpolate. Useopacity+pointer-eventsor the newtransition-behavior: allow-discrete. -
Reduced motion: always wrap transitions in
@media (prefers-reduced-motion: reduce)to disable for accessibility. - Transitions vs animations: transitions need a trigger and animate between 2 states. @keyframes animations run independently and support looping and multi-step sequences.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.