Syntax
transition: property duration timing-function delay;Examples
A Basic Hover Transition
Smoothly animating a color and transform change on hover.
.button {
background: #3b82f6;
transform: scale(1);
transition: background 0.2s ease, transform 0.2s ease;
}
.button:hover {
background: #2563eb;
transform: scale(1.05);
}transition: all vs Specific Properties
Why targeting specific properties is usually better than "all".
.risky {
transition: all 0.3s ease; /* animates EVERY property that changes, even unintended ones */
}
.better {
transition: background-color 0.3s ease, box-shadow 0.3s ease; /* explicit and predictable */
}Parameters and values
- transition-property (property name | all): Which property(ies) to animate
- transition-duration (time): How long the animation takes
- transition-timing-function (ease | linear | ease-in-out | cubic-bezier()): The pacing curve of the animation
- transition-delay (time): Delay before the transition starts
Best practices
- Prefer transitioning transform and opacity over properties like width or top - they can be animated by the GPU, resulting in much smoother performance
- List specific properties rather than using transition: all, to avoid accidentally animating unrelated property changes
- Keep UI transitions short (150-300ms) - longer durations start to feel sluggish rather than polished
- Respect prefers-reduced-motion by disabling or reducing non-essential transitions for users who have that preference enabled
At a glance
- Purpose
- Presentation and layout
- File extension
- .css
- Runs in
- Web browsers
- Usually used with
- HTML and JavaScript
Specifications & further reading
Related CSS documentation
Animations & @keyframes
@keyframes defines a named sequence of styles at different points (0% to 100%) of an animation, which is then applied to an element via the animation property. Unlike transitions, which only animate between two states, keyframe animations can define arbitrarily complex multi-step sequences and loop indefinitely.Transforms
transform lets you visually move, rotate, scale, or skew an element without affecting the document flow of surrounding elements - unlike changing position/width directly. Multiple transform functions can be combined in a single declaration, and transform-origin controls the pivot point for rotation and scaling.Filters
The filter property applies graphical effects like blur, brightness, contrast, and grayscale directly to an element, similar to Photoshop-style filters. backdrop-filter applies the same effects to the area behind an element instead, commonly used for frosted-glass UI effects. Multiple filter functions can be chained together.Flexbox
Flexbox (Flexible Box Layout) is a powerful one-dimensional layout system in CSS that makes it easy to design flexible and responsive layouts. It provides an efficient way to distribute space and align items in a container, even when their sizes are unknown or dynamic. Flexbox is particularly useful for creating navigation bars, card layouts, and centering content.
@keyframes defines a named sequence of styles at different points (0% to 100%) of an animation, which is then applied to an element via the animation property. Unlike transitions, which only animate between two states, keyframe animations can define arbitrarily complex multi-step sequences and loop indefinitely.Transforms
transform lets you visually move, rotate, scale, or skew an element without affecting the document flow of surrounding elements - unlike changing position/width directly. Multiple transform functions can be combined in a single declaration, and transform-origin controls the pivot point for rotation and scaling.Filters
The filter property applies graphical effects like blur, brightness, contrast, and grayscale directly to an element, similar to Photoshop-style filters. backdrop-filter applies the same effects to the area behind an element instead, commonly used for frosted-glass UI effects. Multiple filter functions can be chained together.Flexbox
Flexbox (Flexible Box Layout) is a powerful one-dimensional layout system in CSS that makes it easy to design flexible and responsive layouts. It provides an efficient way to distribute space and align items in a container, even when their sizes are unknown or dynamic. Flexbox is particularly useful for creating navigation bars, card layouts, and centering content.