Without transitions, state changes in CSS (such as hovering over a button or clicking a checkbox) occur abruptly in an instant flash of pixels. CSS Transitions allow developers to interpolate property values smoothly over a specified duration, creating tactile, natural, and responsive micro-interactions.
The transition shorthand bundles 4 distinct timing components:
/* Syntax: transition: [property] [duration] [timing-function] [delay]; */
.btn-primary {
background-color: #d22b2b;
transform: translateY(0);
transition: background-color 0.25s ease, transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
.btn-primary:hover {
background-color: #b91c1c;
transform: translateY(-2px); /* Subtle tactile lift on hover */
}
| Timing Function | Speed Curve Behavior | Best Use Case |
|---|---|---|
ease (Default) |
Starts moderately, speeds up in the middle, slows down at the end. | General UI animations. |
linear |
Maintains exact constant speed from start to finish. | Spinning loaders and progress indicators. |
ease-in |
Starts very slow and accelerates rapidly toward the finish. | Elements leaving or exiting the viewport. |
ease-out |
Starts fast and decelerates gracefully to a soft landing. | Gold standard for interactive hover & entering elements! |
cubic-bezier(p1,p2,p3,p4) |
Custom mathematical 4-point bezier curve (supports elastic spring bounces). | Bouncy modal popups and toggle switches. |
Never animate layout properties like width, height, top, left, or margin! These trigger expensive CPU Reflows and Repaints that cause battery drain and choppy scrolling. Always animate transform and opacity exclusively, which run directly on the GPU Composite layer at a silky-smooth 60–120 FPS.
Boost your CSS and front-end development proficiency by exploring these closely connected topics:
Understand margins, borders, padding, content and the universal box-sizing fix.
Master 1D alignment, justify-content, align-items, flex-wrap and responsive flow.
Build powerful two-dimensional grid layouts with template areas and fr units.
Eliminate styling conflicts and master selector weights from 0,0,0,1 to !important.