Welcome to Part 13 of the CSS Mastery & Responsive Layouts Course. In this part, we will explore Transitions & Timing Functions.
CSS Transitions allow you to interpolate styles smoothly over a defined duration when an element changes states (like hover, focus, active, or class-toggled states). By defining transition properties, timing functions, and durations, you can avoid abrupt style jumps and create smooth, engaging micro-interactions.
Chapter 86: transition Properties
To configure a transition, use these four base properties:
86.1 Transition Property (transition-property)
Defines the name of the CSS property you want to transition:
.btn {
transition-property: background-color, transform;
}
- Performance Tip: Avoid using the
allkeyword (e.g.transition-property: all). It forces the browser to monitor and parse every styled characteristic, degrading rendering performance.
86.2 Transition Duration (transition-duration)
Sets the animation time in seconds (s) or milliseconds (ms):
transition-duration: 0.3s;or300ms;.
86.3 Transition Delay (transition-delay)
Sets a waiting time before the transition begins:
transition-delay: 100ms;.
Chapter 87: The Transition Shorthand
You can declare all transition properties in a single line:
/* Syntax: transition: [property] [duration] [timing-function] [delay]; */
.card-hover {
transition: transform 0.3s ease-in-out 100ms;
}
/* Declaring Multiple Transitions separated by commas */
.badge {
transition: background-color 0.2s linear, transform 0.3s cubic-bezier(0.25, 1, 0.5, 1);
}
Chapter 88: Timing Functions & Easing Curves
The transition-timing-function property controls the acceleration rate of the transition over its timeline:
linear: Constant speed
ease: Slow start, fast middle, slow end (Default)
ease-in: Slow start, accelerating to end
ease-out: Fast start, decelerating to end
ease-in-out: Slow start, fast middle, slow end
88.1 Custom Easings with cubic-bezier()
You can define custom timing curves using the cubic-bezier(x1, y1, x2, y2) function, representing coordinates on a Bezier curve:
- Values range from
0to1on the X axis, but can exceed bounds on the Y axis to create bouncing effects:.btn-bounce { transition: transform 0.4s cubic-bezier(0.68, -0.6, 0.32, 1.6); }
88.2 Stepped Transitions with steps()
Divides the transition duration into equal-sized steps (useful for sprite-sheet animations or typing indicators):
transition-timing-function: steps(4, end);(snaps values abruptly in 4 intervals).
Chapter 89: Transition Performance: Layout vs. Compositing
Not all properties transition smoothly. Transitioning layout properties causes laggy animations:
- Avoid Transitioning:
width,height,margin,padding,top,left. These trigger Reflow (layout calculations) on every single frame. - Highly Performant:
transform(scale, rotate, translate) andopacity. These are GPU-accelerated and skip layout and paint phases entirely.
/* BAD: Laggy layout transition */
.panel {
transition: left 0.3s ease;
left: 0;
}
.panel:hover {
left: -200px;
}
/* GOOD: Hardware-accelerated smooth transition */
.panel-optimized {
transition: transform 0.3s ease;
transform: translateX(0);
}
.panel-optimized:hover {
transform: translateX(-200px);
}
Chapter 90: Transitions Practice & Self-Check
90.1 Practice Exercises
- Exercise 1: Standard Hover Button: Style a button that morphs background color and scale smoothly on hover.
.btn-smooth { background-color: #3b82f6; transform: scale(1); transition: background-color 0.2s ease, transform 0.2s cubic-bezier(0.2, 1, 0.2, 1); } .btn-smooth:hover { background-color: #2563eb; transform: scale(1.05); } - Exercise 2: Stepped Reveal Indicator: Create a simple reveal block that slides in stepped segments.
.reveal-box { width: 100px; transform: translateX(0); transition: transform 1s steps(5, end); } .reveal-box:hover { transform: translateX(200px); }
90.2 Self-Check Questions
- What is the default transition-timing-function?
- Answer:
ease.
- Answer:
- Which keyword transitions all animatable properties?
- Answer:
all(but avoid using it for performance reasons).
- Answer:
- What are the two most performant properties to transition in CSS?
- Answer:
transformandopacity.
- Answer:
- What coordinate range is allowed on the X-axis for cubic-bezier coordinates?
- Answer: $0$ to $1$.
- Why does transitioning margin-top: 10px cause layout stuttering?
- Answer: It forces browser reflows (recalculating element geometries) on every animation frame.
- What timing function divides durations into distinct snapping frames?
- Answer:
steps().
- Answer:
- What parameters define the transition shorthand ordering?
- Answer: Property, duration, timing-function, delay.
- What is the difference between ease-in and ease-out?
- Answer:
ease-instarts slow and speeds up;ease-outstarts fast and slows down.
- Answer:
- Does transition-delay run before the hover state style is applied, or before the animation starts?
- Answer: It delays the start of the animation transition after the hover state is triggered.
- Can you use negative values in transition-delay?
- Answer: Yes, negative values cause the transition to start immediately but visually skip ahead in the timeline.
Discussion
Loading comments...