Sub topic 11
Keeping animations smooth
This chapter assumes you can write Tailwind transitions with transition-*, duration-* and ease-*, and that you know the hover and motion-safe variants from earlier chapters. It explains why some animations stay smooth and others stutter, and how to choose utilities that cost the browser little work.
What the browser does each frame
To show a page, the browser runs up to four steps. A smooth animation needs a new frame about every 16.7 ms (60 frames per second), so every step that runs on each frame eats into that budget.
- Style: work out which CSS rules apply to each element.
- Layout: work out the size and position of every box.
- Paint: fill in pixels for text, colors, borders and shadows.
- Composite: combine painted layers into the final image, usually on the GPU.
Changing a property restarts the pipeline at the earliest step that property affects. The later the restart point, the cheaper the frame.
The three cost tiers
| Tier | Example properties | Tailwind utilities | Cost per frame |
|---|---|---|---|
| Layout | `width`, `height`, `margin`, `padding`, `top`, `left`, `font-size` | `w-*`, `h-*`, `m-*`, `p-*`, `top-*`, `left-*` | Highest. Layout, paint and composite all run again, and neighbors may move. |
| Paint | `background-color`, `border-color`, `box-shadow`, `color` | `bg-*`, `border-*`, `shadow-*`, `text-*` | Medium. Pixels are redrawn, but nothing moves. |
| Composite | `transform`, `opacity` | `translate-*`, `scale-*`, `rotate-*`, `opacity-*` | Lowest. The browser moves or fades an already painted layer. |
The tiers are rules of thumb. Browsers differ, and a small element with a color change costs almost nothing. The tiers matter most for large elements, long lists, low-powered phones and animations that run for a long time.
A practical rule: move things with translate-*, resize them with scale-*, and fade them with opacity-*. Use layout properties only when the layout genuinely has to change.
Replace layout animation with transforms
Sliding instead of moving
Animating left or margin-left forces layout on every frame. Translating the element gives the same visual result at composite cost.
<!-- Slower: animates a layout property -->
<div class="relative left-0 transition-[left] duration-300 hover:left-8">Panel</div>
<!-- Faster: animates a composite property -->
<div class="transition-transform duration-300 hover:translate-x-8">Panel</div>The visible difference is that a translated element still occupies its original space in the layout. Its siblings do not shift. This is usually what you want for hover effects, but it matters if you expected the surrounding content to reflow.
Growing a bar without animating width
A progress bar is a common place to animate width. Scale the bar on the X axis instead, and anchor it to the left edge with origin-left.
<div class="h-2 w-64 overflow-hidden rounded-full bg-slate-200">
<div
class="h-full origin-left rounded-full bg-indigo-600 transition-transform duration-500 ease-out"
style="transform: scaleX(0.6)"
></div>
</div>Here the inline style stands in for a value your code updates. With Tailwind v4 you can instead use scale-x-60, because v4 accepts any number for scale utilities. In v3, use an arbitrary value such as scale-x-[0.6].
Scaling distorts the element's contents, so it suits plain shapes such as bars and highlights. Do not scale an element that contains text or rounded corners you want to keep crisp. Corners stretch along with the scale. For those cases, put the visual shape on a child that you scale, and keep the text outside it.
Animating height without animating height
Accordions and collapsible panels need the content below them to move, so a transform is not enough. A modern approach is to animate a grid row between 0fr and 1fr.
<div class="grid grid-rows-[0fr] transition-[grid-template-rows] duration-300 ease-out has-[:checked]:grid-rows-[1fr]">
<label class="cursor-pointer">
<input type="checkbox" class="peer sr-only" />
Toggle details
</label>
<div class="overflow-hidden">
<p class="p-4">Extra details appear here.</p>
</div>
</div>In practice you would toggle grid-rows-[1fr] with JavaScript or a framework state. The key points are overflow-hidden on the inner element (without it, content spills out of the zero-height row) and an explicit transition-[grid-template-rows]. This still runs layout on each frame, but it avoids hard-coding a pixel height, and it is the layout-triggering option that handles content of unknown size. Use it for small panels, not for large pages.
Be specific about what you transition
The bare transition utility animates a fixed list of properties. transition-all animates every property that changes. That is convenient, but it has costs.
- A later change to an unrelated property, such as
widthin a responsive variant, will animate when you did not intend it to. - Layout-triggering properties may animate by accident.
- The browser must watch every property for changes.
Prefer the narrowest utility that does the job:
| Utility | Animates | Use for |
|---|---|---|
| `transition-colors` | Colors, borders, fills | Hover and focus color changes |
| `transition-opacity` | `opacity` | Fades |
| `transition-transform` | `transform` and the individual transform properties | Slide, scale, rotate |
| `transition-shadow` | `box-shadow` | Shadow changes |
| `transition-[grid-template-rows]` | Only that property | Collapsible panels |
The earlier card example uses the default transition, which includes box-shadow. That is acceptable for one card. In a grid of fifty cards, each with a large blurred shadow, the paint cost adds up.
Cheaper shadows
A box-shadow transition repaints the shadow on every frame, and large blurs are costly. A common workaround is to draw the larger shadow once on a pseudo-element, then fade that element in with opacity.
<div class="relative rounded-lg bg-white p-6 shadow transition-transform duration-300 ease-out after:pointer-events-none after:absolute after:inset-0 after:rounded-lg after:opacity-0 after:shadow-xl after:transition-opacity after:duration-300 hover:-translate-y-1 hover:after:opacity-100">
Card content
</div>The after: element is drawn once with its shadow, and only its opacity changes. pointer-events-none stops the overlay from intercepting clicks. This adds markup complexity, so use it only where you have measured a problem, such as many large shadowed items.
will-change and forcing a layer
Browsers usually promote an element to its own compositing layer when it starts a transform or opacity animation. Tailwind exposes two related hints.
will-change-transformandwill-change-opacitytell the browser in advance that the property will change, so it can prepare a layer.transform-gpuforces the element onto the GPU with a 3D transform.
Both consume memory, because each layer is a stored bitmap. Used on many elements, they can make performance worse and, on mobile devices, can exhaust graphics memory.
<div class="will-change-transform transition-transform duration-200 hover:scale-110">
Item
</div>Apply a hint only to an element that you have measured to be janky, and ideally only while an animation is about to happen, for example by adding the class on pointerenter. Do not add will-change-transform to every card in a list.
Other causes of dropped frames
- Long durations on large areas. A 1-second fade over a full-screen overlay repaints or recomposites a large area for 60 frames. Keep large-area transitions short.
- Infinite animations out of view.
animate-spinoranimate-pulseon offscreen elements still costs work in some browsers. Remove them, or render them only when visible. - Many simultaneous animations. Animating hundreds of elements at once taxes the main thread even for cheap properties. Stagger them with
delay-*, or animate a container instead of each child. - Filters and blurs.
blur-*andbackdrop-blur-*are expensive, especially when animated or placed over moving content. Avoid transitioning blur radius on large surfaces. - Layout thrash from JavaScript. If script reads layout values such as
offsetHeightbetween writes, the browser runs layout repeatedly. Batch reads and writes.
Respect reduced motion as part of performance and comfort. motion-reduce:transition-none removes the transition entirely for users who ask for it, which is both an accessibility choice and a way to cut work.
<button class="transition-transform duration-200 hover:scale-105 motion-reduce:transition-none motion-reduce:hover:scale-100">
Buy now
</button>Measuring instead of guessing
Do not optimize on a hunch. Chrome and Edge DevTools provide the tools to check your changes.
- Open Rendering (from the DevTools command menu) and enable Paint flashing. Green flashes show regions being repainted. A hover effect that flashes the whole card is paint-bound.
- Record in the Performance panel while triggering the animation. Look for long purple (layout) and green (paint) bars in each frame, and for frames that exceed 16.7 ms.
- Use the Layers panel to count compositing layers and see how much memory they use.
- Test with CPU throttling (4x or 6x slowdown) to approximate a mid-range phone. An animation that is smooth on your laptop may drop frames there.
Common mistakes
- Using
transition-allon elements whose size or position changes for other reasons. - Animating
width,heightortopfor a move or resize that a transform could do. - Scaling elements that contain text, which blurs or stretches the content.
- Adding
will-change-*ortransform-gpueverywhere "just in case". - Forgetting that transforms do not change the layout box, so a scaled element can overlap its neighbors.
- Transitioning large blurred shadows on many items at once.
Exercise
A sidebar currently slides in with transition-[left] duration-300, switching from -left-64 to left-0. Rewrite the classes so it animates with a composite-only property, and say why the new version is cheaper.
Show answer
Keep the sidebar at left-0 and move it with a transform: use -translate-x-full for the hidden state, translate-x-0 for the open state, and transition-transform duration-300. For example: fixed inset-y-0 left-0 w-64 -translate-x-full transition-transform duration-300 ease-out data-[open]:translate-x-0. It is cheaper because left triggers layout, paint and composite on every frame, while a transform only changes how an already painted layer is composited.
Exercise
You add transition-all to a product card that has hover:shadow-lg and w-full md:w-1/2. Name one problem this creates and give a more specific replacement.
Show answer
transition-all also animates width when the viewport crosses the md breakpoint or the card is resized, which triggers layout on every frame and causes an unintended animation. Replace it with transition-shadow (or transition-[box-shadow,transform] if the card also lifts), so only the intended properties animate.
Exercise
In the Performance panel, a hover animation on a card shows a green paint bar in every frame, and Paint flashing highlights the whole card. The animation uses transition with hover:shadow-2xl. Suggest a change and the trade-off it brings.
Show answer
Move the large shadow onto an after: pseudo-element and fade it in with after:opacity-0 hover:after:opacity-100 after:transition-opacity, so the shadow is painted once and only opacity changes. The trade-off is extra markup and a relative positioned parent plus pointer-events-none on the overlay, so use it only when the paint cost is measured and significant.