CSS transitions: smooth hover effects and timing functions
Add smooth, tasteful motion with transitions: properties, duration, timing functions, performance and reduced motion.
In this lesson you will
- Write a transition
- Choose duration and easing
- Animate the cheap properties
- Respect reduced-motion settings
About 15 minutes
The difference between snapping and feeling smooth
Hover over a button on a cheap website and its colour snaps instantly from one shade to another. Hover over a button on a well-made site and the colour glides over a fraction of a second. You may not notice the glide consciously, but you feel the difference: one seems mechanical, the other alive. The tool for that glide is the CSS transition, and it is probably the single best return on effort in all of front-end styling.
The idea
Normally, when a property changes, such as the background on hover, the browser jumps straight to the new value. A transition tells the browser: “when this property changes, take a little time to travel from the old value to the new one.” You write the transition on the element in its normal state, and then change the property in a state like :hover.
.btn {
background: #5A28C8;
transition: background-color 0.25s ease;
}
.btn:hover {
background: #3F1A9A;
}
Press Run, then hover or tap the button. It already changes colour instantly. Add a transition of 0.3s to make the change smooth.
<style>
.btn { padding: 12px 24px; border: 0; border-radius: 999px; background: #5A28C8; color: #fff; font-weight: 700; cursor: pointer; /* add a transition */ }
.btn:hover { background: #E8409F; }
</style>
<button class="btn">Hover me</button>
Add transition: background-color 0.3s ease; inside .btn.
<style>
.btn { padding: 12px 24px; border: 0; border-radius: 999px; background: #5A28C8; color: #fff; font-weight: 700; cursor: pointer; transition: background-color 0.3s ease; }
.btn:hover { background: #E8409F; }
</style>
<button class="btn">Hover me</button>
The four parts of a transition
.card {
transition-property: transform, box-shadow;
transition-duration: 0.25s;
transition-timing-function: ease-out;
transition-delay: 0s;
}
/* shorthand: property duration timing delay */
.card { transition: transform 0.25s ease-out, box-shadow 0.25s ease-out; }
- Property: which property to animate. You can list several, separated by commas. The word
allanimates everything, but it is wasteful and can surprise you. - Duration: how long it takes. Use seconds (
0.3s) or milliseconds (300ms). Between 150 and 400 milliseconds feels natural for interface elements. - Timing function: the speed curve, explained next.
- Delay: a wait before it starts. Rarely needed.
Timing functions: the personality of motion
Things in the real world do not move at a constant speed. A ball speeds up as it falls and slows as it lands. Timing functions give your animations that character.
linear: constant speed. Feels robotic, but is right for things like progress bars.ease: starts slowly, speeds up, ends slowly. The default.ease-out: starts fast and slows at the end. Great for things entering the screen, since they feel responsive straight away.ease-in: starts slow and ends fast. Good for things leaving.ease-in-out: slow at both ends.cubic-bezier(.2, .8, .2, 1): your own custom curve. Online curve editors let you draw one and copy the values.
What can be transitioned?
Any property with a value that can be calculated between two states: colours, sizes, opacity, shadows, transforms, spacing. A property such as display cannot be, because there is no halfway between none and block. To fade something in and out, animate opacity and visibility instead.
.tooltip {
opacity: 0;
visibility: hidden;
transform: translateY(6px);
transition: opacity .2s, transform .2s, visibility .2s;
}
.trigger:hover .tooltip,
.trigger:focus-visible .tooltip {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
Notice that the hidden state uses visibility: hidden as well. Together with the opacity, it removes the tooltip from keyboard and screen reader reach until it appears.
Performance: animate the cheap properties
Not all changes are equal for the browser. Changing width, height, margin or top forces it to recalculate the layout of the page, which is expensive and may stutter on phones. Changing transform and opacity is handled by the graphics chip and stays smooth. The rule of thumb: animate transform and opacity, and avoid animating layout properties.
Make the card lift smoothly on hover: add a transition for transform and box-shadow (0.25s), and in the hover state move it up 6px with translateY(-6px) and deepen the shadow.
<style>
body { background:#F4F1FC; padding:24px; font-family:Arial; }
.card { width:200px; padding:20px; background:#fff; border-radius:14px; box-shadow:0 4px 12px rgba(20,17,46,.08); /* transition */ }
.card:hover { /* lift */ }
</style>
<div class="card"><h3>Course</h3><p>Hover to lift me.</p></div>
Normal: transition: transform .25s ease-out, box-shadow .25s ease-out. Hover: transform: translateY(-6px); box-shadow: 0 14px 28px rgba(20,17,46,.16).
<style>
body { background:#F4F1FC; padding:24px; font-family:Arial; }
.card { width:200px; padding:20px; background:#fff; border-radius:14px; box-shadow:0 4px 12px rgba(20,17,46,.08); transition: transform .25s ease-out, box-shadow .25s ease-out; }
.card:hover { transform: translateY(-6px); box-shadow: 0 14px 28px rgba(20,17,46,.16); }
</style>
<div class="card"><h3>Course</h3><p>Hover to lift me.</p></div>
Put the transition on the base state
A subtle but important point. If you put the transition only on :hover, it animates on the way in but snaps back when the pointer leaves, because the transition belongs to the state being entered. Put it on the base rule and both directions animate. If you want different speeds in each direction, you can set a different transition in each state.
.menu-item { transition: background .3s; } /* slow out */
.menu-item:hover { background: #EFEAFD; transition-duration: .1s; } /* fast in */
Respect people who prefer less motion
Some people feel dizzy or distracted by animation. Their system has a setting to reduce motion, and your CSS can respect it with one block.
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
transition-duration: 0.01ms !important;
animation-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
This is one of the few places where !important is reasonable. You are deliberately overriding every animation on the page for a user who asked for it.
Good taste in motion
- Keep it quick. If people notice the animation waiting for them, it is too slow.
- Use it to give feedback or guide attention, not to decorate.
- Be consistent. Use the same few durations and curves throughout the site.
- Never use motion as the only way to show something important.
Using transition: all 1s on everything. It animates properties you did not mean to, slows the interface, and makes pages feel sluggish. Name the properties you want, and keep durations short.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Where should the transition declaration normally go?
- Only on :hover
- On the element’s base state
- In the HTML
- On the body
On the base state both directions animate.
Which property can you NOT transition between none and block?
opacitydisplaytransformcolor
display has no in-between values.
Which pair is cheapest and smoothest to animate?
widthandheighttransformandopacitymarginandtopfloatandclear
They do not force a layout recalculation.
What does ease-out do?
- Starts slow and ends fast
- Starts fast and slows at the end
- Constant speed
- Reverses
It decelerates, which feels responsive.
What does prefers-reduced-motion let you do?
- Speed up animations
- Respect users who want less motion
- Block hover
- Print faster
It reads the user’s system setting.
Summary
- A transition smooths a property change over time.
- Put it on the base state, not only on hover.
- Animate transform and opacity for smooth results.
- Honour prefers-reduced-motion.
Questions
Why does my transition not work?
The property may not be animatable (like display), the transition may be on the wrong state, or the value may not change.
How long should a transition be?
Usually 150 to 400 milliseconds for interface changes.
Can I transition between two gradients?
Not directly, but you can fade between them by animating the opacity of a layer on top, or use the newer @property feature.
