CSS Advanced: Variables, Motion and Modern CSS
Transitions and Transforms
In this lesson you will
- Write a transition with property, duration and easing
- Use translate, scale and rotate transforms
- Respect users who prefer reduced motion
Introduction
When you hover over a button and its colour jumps instantly, it feels abrupt. When it fades smoothly, it feels polished. Transitions smooth out changes. Transforms move and reshape elements without disturbing the layout around them.
Transitions
A transition tells the browser: "When this property changes, do not jump. Animate it over a short time."
The transition shorthand is: property, duration, timing function, delay. Put it on the element's normal state, not only on :hover, so it animates both in and out.
ease,ease-in,ease-outandlinearare common timing functions.- Use
transition: all 0.3sonly while experimenting. Naming exact properties is cleaner and faster.
Quick quiz
Where should you usually put the transition declaration?
Transforms
transform changes how an element is drawn without moving other elements.
translate(x, y)moves it.scale(n)makes it bigger or smaller.rotate(angle)turns it.
You can combine transforms by listing them one after another.
Why transform and opacity are special
Browsers can animate transform and opacity very smoothly because they do not force the page layout to be recalculated. Animating width, height, top or margin can feel jerky. When you have a choice, animate transform and opacity.
Respect reduced motion
Some people feel unwell from motion on screens. They can turn on a "reduce motion" setting, and CSS can detect it:
HTML
@media (prefers-reduced-motion: reduce) {
* { transition: none !important; animation: none !important; }
}Honouring this setting is an easy way to make your site kinder.
Quick quiz
Which two properties are usually the smoothest to animate?
Try it yourself
Add a rotate(6deg) to the hover transform, and change the timing to 0.5s.
Summary
transitionanimates a change over time: property, duration, timing, delay.transformmoves, scales or rotates without disturbing layout.- Prefer animating
transformandopacity. - Respect
prefers-reduced-motion.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading