Performant CSS animations with transform and opacity
44 Animate only what is cheap
fairAnimate a panel sliding in and a card lifting, using only the two properties the compositor can handle alone. Say what the expensive alternatives would have cost.
Stuck? Three hints, smallest first
- 1.
transformandopacitycan be animated on the compositor, off the main thread. - 2. Animating
width,height,top,leftormarginforces layout on every frame. - 3.
will-changeis a hint, not a fix. Adding it to everything costs memory and helps nothing.
<article class="card" tabindex="0">
<h3>Abuja</h3>
<p>Hover or focus me: transform and opacity only.</p>
</article>
<div class="tray">
<p class="slide">Slides in when the card above is hovered or focused.</p>
</div>
.tray {
overflow: hidden;
margin-top: 10px;
border: 1px dashed #c9cad1;
border-radius: 8px;
padding: 8px;
}
.slide {
margin: 0;
font-size: 0.8rem;
color: #5c5c66;
/* transform and opacity: no layout, no paint of anything else. */
transform: translateX(-8px);
opacity: 0.45;
transition: transform 200ms ease, opacity 200ms ease;
}
.card {
box-sizing: border-box;
max-width: 260px;
padding: 12px;
border: 1px solid #c9cad1;
border-radius: 10px;
background: #ffffff;
font-size: 0.8rem;
transition: transform 180ms ease, box-shadow 180ms ease;
}
.card h3 { margin: 0 0 4px; font-size: 0.9rem; }
.card p { margin: 0; color: #5c5c66; }
.card:hover,
.card:focus-visible {
transform: translateY(-4px);
box-shadow: 0 8px 20px rgb(23 23 26 / 12%);
}
/* The tray follows the card in the source, so a sibling combinator reaches it.
It would not work the other way round: CSS cannot select backwards. */
.card:hover ~ .tray .slide,
.card:focus-visible ~ .tray .slide,
.tray:hover .slide {
transform: none;
opacity: 1;
}
.card:focus-visible {
outline: 2px solid #3055d8;
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
.slide, .card { transition: none; }
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
// Which properties are named in every transition list.
const animated = [...css.matchAll(/transition:\s*([^;]+);/g)]
.flatMap((m) => m[1].split(',').map((part) => part.trim().split(/\s+/)[0]))
.filter((p) => p !== 'none');
const expensive = ['width', 'height', 'top', 'left', 'margin', 'padding'];
test('three properties are transitioned in all', animated.length, 4);
test('and they are only transform, opacity and box-shadow', [...new Set(animated)].sort(), ['box-shadow', 'opacity', 'transform']);
test('nothing that forces layout is animated', animated.some((p) => expensive.includes(p)), false);
test('the movement is a transform, not a position offset', /transform:\s*translateY\(-4px\)/.test(css), true);
test('and the fade is opacity, not a colour change', /opacity:\s*0\.45/.test(css), true);
test('will-change is not sprayed around as a fix', /will-change/.test(css), false);
test('reduced motion turns the transitions off', /prefers-reduced-motion: reduce/.test(css), true);
test('keyboard focus triggers the same states as hover', /\.card:focus-visible/.test(css), true);
Output
transform and opacity can be handed to the compositor and animated without touching layout or paint, so they stay smooth even while the main thread is busy. Animating left or width instead forces the browser to re-lay-out and repaint on every frame, which is where dropped frames come from — and the visual result is identical, so there is nothing to trade off. box-shadow is a mild exception: it repaints but does not reflow, which is usually acceptable for a hover. will-change is a promise to the browser to allocate resources ahead of time; on every element it is just memory spent for nothing.