Sub topic 11
Keeping motion smooth: transform, opacity and layout cost
This chapter assumes you can write a basic transition and a @keyframes animation, and that you know what transform and opacity do. It explains why some animated properties run smoothly and others stutter, and how to choose between them.
How the browser draws a frame
To show a frame, the browser works through up to three stages:
- Layout calculates the size and position of every affected box.
- Paint fills in pixels for each box: text, colors, borders, shadows.
- Composite stacks the painted layers into the final image, usually on the GPU.
A display refreshing at 60 Hz gives the browser about 16 ms per frame. If the work takes longer, frames are dropped and the motion looks choppy, which is often called jank.
The properties you animate decide which stages repeat on every frame.
Cost by property
| Property | Stages re-run each frame | Cost |
|---|---|---|
| `width`, `height`, `top`, `left`, `margin` | Layout, paint, composite | High |
| `background-color`, `box-shadow`, `color` | Paint, composite | Medium |
| `transform`, `opacity` | Composite only | Low |
Changing left can move neighboring elements, so the browser must recalculate layout for them too. Changing transform moves the element's painted pixels without affecting anything else on the page.
Prefer transform and opacity
To move an element, use translate in transform instead of top or left. To resize it visually, use scale instead of width and height.
/* Slower: triggers layout every frame */
.panel-slow {
position: relative;
left: 0;
transition: left 300ms ease-out;
}
.panel-slow.open {
left: 200px;
}
/* Smoother: composite only */
.panel-fast {
transform: translateX(0);
transition: transform 300ms ease-out;
}
.panel-fast.open {
transform: translateX(200px);
}Both panels end up in the same visual place. The second one lets the browser skip layout and paint during the movement.
A transform does not change the space the element takes in the layout. Neighbors stay where they were, so it suits effects such as a sliding drawer, but it will not push other content out of the way.
Fading a shadow
Animating box-shadow repaints on every frame, which can be costly for large shadows. A common workaround is to put the final shadow on a pseudo-element and animate its opacity instead.
.card {
position: relative;
}
.card::after {
content: "";
position: absolute;
inset: 0;
box-shadow: 0 8px 24px rgb(0 0 0 / 0.3);
opacity: 0;
transition: opacity 200ms ease-out;
pointer-events: none;
}
.card:hover::after {
opacity: 1;
}Using will-change carefully
The will-change property tells the browser that a property is about to change, so it can prepare a separate layer ahead of time.
.drawer {
will-change: transform;
}Each extra layer uses memory. Apply will-change only to elements that you have measured to be slow, and remove it when the motion is done. Do not apply it to many elements by default.
Common mistakes
- Animating
heightto open an accordion on a long page. Layout runs on every frame. Consider a smaller, targeted layout change or a fade instead. - Animating
box-shadoworfilter: blur()on many elements at once. - Assuming
transformis free. Very large or many layers still cost memory. - Judging smoothness by feel only. Record a trace in your browser's performance panel and check for long frames.
Exercise
A sidebar currently opens by transitioning left from -300px to 0. Name the stages that run on every frame, then describe a change that avoids most of that work.
Show answer
Changing left runs layout, paint and composite on every frame. Replace it with transform: translateX(-300px) as the closed state and transform: translateX(0) as the open state, and transition transform instead. This needs only the composite stage.
Exercise
Why does moving an element with transform: translateX(200px) not push its neighbors aside?
Show answer
A transform changes only how the element is drawn. It does not change the space the element occupies in layout, so neighboring elements keep their positions.