Sub topic 11
Keeping animations smooth
This chapter assumes you know Tailwind utility classes, the transition-* and duration-* utilities, and basic CSS transforms. It explains which properties are cheap to animate, which are costly, and how to choose between them.
How the browser renders a change
When a CSS property changes, the browser may need to redo up to three steps:
- Layout works out the size and position of elements.
- Paint fills in pixels such as colors, borders and shadows.
- Composite combines painted layers into the final image, usually on the GPU.
Layout is the most expensive step. A layout change on one element can force the browser to recalculate its neighbors and its parents. To keep 60 frames per second, each frame has about 16 ms of budget. An animation that triggers layout on every frame can exceed that budget, and the result is visible stutter, often called jank.
Cheap and costly properties
| Property type | Tailwind examples | Steps triggered | Cost |
|---|---|---|---|
| Transform | `scale-105`, `translate-x-1`, `rotate-3` | Composite | Low |
| Opacity | `opacity-0`, `opacity-100` | Composite | Low |
| Color, shadow | `bg-indigo-700`, `shadow-lg` | Paint, composite | Medium |
| Size, spacing, position | `w-64`, `h-20`, `p-4`, `top-2` | Layout, paint, composite | High |
Browsers differ in details. Some paint-only properties are still fast in practice, and a box-shadow on a small button is rarely a problem. The practical rule is to prefer transform and opacity for anything that moves, grows, appears or disappears.
Replace layout animations with transforms
A common mistake is animating width or margin-left to move or resize something. Here the card grows by changing its width, which forces layout on every frame:
<div class="w-48 transition-[width] duration-300 hover:w-56">
Card
</div>This version uses a transform instead. The element looks larger, but its layout box stays the same, so neighbors do not move:
<div class="w-48 transition-transform duration-300 hover:scale-105">
Card
</div>The same idea applies to movement. Use translate-x-* and translate-y-* instead of changing left, top or margin.
Note that a transform does not change the space an element takes up. If surrounding content must reflow, a transform will not do that, and you may need a layout change. In that case, keep the animation short or avoid it.
Name the properties you transition
The bare transition utility animates a default list of properties: color, background, border, shadow, transform, opacity and a few others. That is convenient, but it can animate things you did not intend, such as a color change that is a side effect of another class.
Narrow it down when performance matters:
transition-transformanimates only transforms.transition-opacityanimates only opacity.transition-colorsanimates only color-related properties.transition-[width]or another arbitrary value animates a specific property.
<img
src="photo.jpg"
alt="Mountain at sunrise"
class="transition-transform duration-300 ease-out hover:scale-110"
>Avoid transition-all. It animates every property that changes, including layout properties, and it makes future edits to the element risky.
Hint the browser with will-change
Tailwind provides will-change-transform, will-change-scroll and will-change-contents. They tell the browser that a property is about to change, so it can promote the element to its own layer ahead of time.
<div class="will-change-transform transition-transform duration-200 hover:-translate-y-1">
Card
</div>Use this sparingly. Each promoted layer uses GPU memory, and too many layers can make performance worse. Add it only to elements where you have measured jank, and not to every card in a long list.
Measure before you tune
You can check results with the browser's developer tools. In Chrome, open the Performance panel, record while you trigger the animation, and look for long frames and for Layout entries in each frame. A smooth transform animation shows little or no layout work. Test on a slow device or with CPU throttling enabled, because a fast development machine hides problems.
Exercise
A sidebar slides in by transitioning left from -left-64 to left-0 with transition-all. Name two changes that make this animation cheaper.
Show answer
Replace the left change with a transform, for example -translate-x-full moving to translate-x-0, so the browser only composites. Also replace transition-all with transition-transform so that no other property is animated by accident.
Exercise
A card grows on hover using hover:w-56. It pushes its neighbors sideways on every frame. What does hover:scale-105 do differently, and what is the trade-off?
Show answer
scale-105 changes only the rendered size through a transform, so layout is not recalculated and neighbors stay in place. The trade-off is that the card's layout space does not change, so it can overlap its neighbors instead of making room for them.