01. CSS Transitions (property, duration, timing-function, cubic-bezier)
🟢 Beginner
1. Definition
CSS Transitions interpolate property values smoothly over time (e.g., transition: background-color 0.3s cubic-bezier(0.4, 0, 0.2, 1)) when an element changes states (such as :hover or :focus).
2. Why It Is Used
Transitions provide visual feedback for interactive buttons, navigation links, and hover cards, making web interfaces feel responsive and polished.
3. How It Works
When a state change occurs, the browser calculates intermediate property values frame by frame based on duration and the mathematical acceleration curve of the timing function.
4. Syntax
.button {
transition: background-color 0.3s ease, transform 0.2s ease;
}
5. Timing Function Variations Table
| Timing Function | Acceleration Curve | Best Practice Usage |
|---|---|---|
ease | Slow start, fast middle, slow end | Default general transitions |
linear | Constant speed throughout | Color fades & progress bars |
ease-in-out | Symmetrical slow start & slow end | Smooth UI dialog transitions |
cubic-bezier(...) | Custom mathematical Bezier curve | Spring & bounce animations |
6. Beginner Example
.btn {
background-color: #2563eb;
transition: background-color 0.2s ease;
}
.btn:hover {
background-color: #1d4ed8;
}
7. Intermediate Example
.card {
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-4px);
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}
8. Advanced Multi-Property Delay Example
.nav-drawer {
transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease 0.1s;
}
9. Line-by-Line Code Explanation
transition: transform 0.4s ...: Animates card transform over 0.4 seconds using a custom natural ease-out cubic-bezier curve.
10. Expected Terminal / Browser Output
A card component that lifts smoothly 4px upward with an expanding shadow when hovered.
11. Common Mistakes & Pitfalls
- Animating non-GPU properties: Animating
heightormargincauses CPU reflows. Animatetransformandopacityinstead.
12. Important Rules & Limitations
- Declare
transitionon the base class rule (not the:hoverrule) so the reverse transition plays smoothly when unhovered.
13. Multilingual Explanation: English
CSS Transitions animate property changes between states. Declare transitions on the base rule and animate transform for 60fps performance.
14. Multilingual Explanation: Hindi (Roman Script)
Transitions element ke states (jaise hover) ke beech smooth animation banate hain. Fast performance ke liye transform animate karein.
15. Multilingual Explanation: Marathi (Roman Script)
Transitions mule hover kiva focus aalyas smooth effect milto. Always base rule var transition lihava.
16. Practice Questions
- Why should
transitionbe defined on the base rule instead of the:hoverpseudo-class?
17. Practical Coding Challenge
Create a .link-underline::after transition that expands an underline from width: 0% to 100% on hover.