CSS Transitions
This example demonstrates how to create smooth visual changes using CSS transitions. Explore transitions involving colors, dimensions, opacity, transforms, borders, shadows, timing functions, and delays.
Background Color Transition
The background-color property changes smoothly between two colors.
The transition takes 0.4 seconds to complete.
Width Transition
The width property increases the horizontal size of the element.
The element expands from 100 pixels to 180 pixels.
Height Transition
The height property increases the vertical size of the element.
The element grows from 60 pixels to 100 pixels.
Opacity Transition
The opacity property changes the transparency of the element.
The element becomes partially transparent when hovered.
Scale Transition
The scale() function increases the visual size of the element.
The element enlarges without changing its layout dimensions.
Rotate Transition
The rotate() function rotates the element around its center.
The element rotates 12 degrees when hovered.
Movement Transition
The translateY() function moves the element vertically.
The element moves upward by 15 pixels.
Border Radius Transition
The border-radius property changes the shape of the element's corners.
The corners gradually become more rounded.
Box Shadow Transition
The box-shadow property creates a smooth shadow effect.
A larger shadow appears around the element.
Multiple Property Transitions
Several CSS properties can transition at the same time.
The background color, size, and corner radius change together.
Try It: Move your mouse over each box to see its transition effect. Move the pointer away to watch the element return to its original appearance. You can also use the Tab key to focus the demonstration boxes.
CSS Transition Timing Functions
The transition-timing-function property controls how the speed of a transition changes during its duration.
Move your mouse over each white track to compare the timing functions. Each transition takes 1.5 seconds.
The ease function starts slowly, accelerates, and slows near the end.
The linear function maintains a constant speed.
The ease-in function starts slowly and accelerates.
The ease-out function starts quickly and slows down.
The ease-in-out function starts and ends slowly.
Transition Delay
The transition-delay property specifies how long the browser waits before starting a transition.
This example waits 0.5 seconds before changing the background color.
Button Transition
CSS transitions are commonly used to create smooth hover effects for buttons and other interactive elements.
Hover This Button
The button changes its background color, moves upward, and displays a larger shadow.
How CSS Transitions Work
CSS transitions allow property values to change smoothly over a specified duration instead of changing instantly.
A transition typically requires a property that can be interpolated, a duration, and a change in the property's value.
The following example creates a background color transition:
.demo-color {
background-color: #146c94;
transition: background-color 0.4s ease;
}
.demo-color:hover {
background-color: #247a45;
}
The transition declaration is applied to the element's normal state so the effect works when the pointer enters and leaves the element.
CSS Transition Properties
CSS provides several properties for controlling transitions.
The transition-property property identifies which CSS properties should transition.
The transition-duration property specifies how long the transition takes to complete.
The transition-timing-function property controls the rate of change during the transition.
The transition-delay property specifies how long to wait before starting the transition.
The transition shorthand property combines these settings into a single declaration.
transition: background-color 0.4s ease 0.2s;
This declaration transitions the background color over 0.4 seconds using the ease timing function after a 0.2-second delay.
Transitioning Multiple Properties
Multiple CSS properties can transition simultaneously by separating their declarations with commas.
The following example transitions the background color, transform, and border radius:
transition: background-color 0.4s ease,
transform 0.4s ease,
border-radius 0.4s ease;
Each property can have its own duration, timing function, and delay.
Specifying individual properties can make transitions easier to understand and maintain.
CSS Transition Accessibility
Transitions should improve the user experience without creating unnecessary distractions.
Use the prefers-reduced-motion media query to respect visitors who prefer less animation.
For example, the following CSS disables transitions when reduced motion is requested:
@media (prefers-reduced-motion: reduce) {
.transition-box {
transition: none;
}
}
Important information should remain available even when transitions are disabled.
Hover effects should not be the only way to communicate information, since touchscreen devices may not provide conventional hover interactions.
Interactive elements should also provide visible keyboard focus indicators.
Customizing CSS Transitions
You can customize transitions by modifying their duration, timing function, delay, and affected properties.
Shorter durations create faster changes, while longer durations produce slower effects.
Different timing functions change how the transition accelerates or decelerates.
Use transition-delay when an effect should begin after a short pause.
Combine transitions with background colors, borders, shadows, opacity, and transforms to create different visual effects.
For a consistent design, use similar transition durations and timing functions throughout your website.