← Back to HTML & CSS, page 5

html-css-0084 · Hard senior

What does will-change do, and why is overusing it a problem?

will-change tells the browser which property will change so it can prepare, often by creating a compositor layer. It costs memory and can create a stacking context and containing block, so use it sparingly on transform or opacity, add it just before animating, and remove it afterwards.

Full answer

will-change tells the browser which property of an element is likely to change soon, for example will-change: transform. The browser can prepare ahead of time, usually by moving the element to its own compositor layer. Animations of transform and opacity can then run on the compositor without repainting the page on every frame.

It is a hint and not a speed switch. Using it has costs:

  • Each layer uses graphics memory. Many layers on a phone can slow things down or crash a tab.
  • It can create a stacking context and a containing block, depending on the property. will-change: transform makes the element the containing block for fixed descendants, just like a real transform does. This can break a fixed menu inside it.
  • The browser may drop its own optimizations for elements that you did not need to hint.

Good practice:

  1. Do not add it by default or with *. Use it only after measuring a real jank problem.
  2. Add it just before the change, and remove it afterwards. In CSS, a rule on :hover of the parent can warm up the child. In JavaScript, set it on mouseenter and clear it when the animation ends.
  3. Hint only properties that are cheap to animate: transform and opacity. Animating width, top or box-shadow still triggers layout or paint, and will-change does not remove that.
  4. Do not hint a property that will not animate.

Many animations are already optimized by the browser, so a simple transition: transform is often enough.

The card has no hint at first. When the pointer enters the list, will-change: transform is added, and the card moves up 4px when hovered. The red fixed square inside the hinted box is placed relative to that box, not to the window, which shows the containing block side effect.

Check the result in the DevTools layers or performance panel. Remove the hint if you see no gain.

html
<style>
  body { margin: 0; font-family: sans-serif; }
  .card { width: 100px; height: 60px; background: #cde; transition: transform 0.2s; }
  .list:hover .card { will-change: transform; }
  .card:hover { transform: translateY(-4px); }
  .hinted { will-change: transform; height: 100px; background: #eee; }
  .hinted .fixed { position: fixed; top: 0; left: 0; width: 30px; height: 30px; background: tomato; }
  .spacer { height: 120px; }
</style>
<div class="spacer"></div>
<div class="list" id="list"><div class="card" id="card">Card</div></div>
<div class="hinted" id="hint"><div class="fixed" id="fx"></div></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • will-change is a hint to the browser, not a guaranteed speed-up.
  • It often creates a compositor layer, which costs memory.
  • will-change: transform makes the element a containing block for fixed descendants.
  • Apply it briefly, to transform or opacity, after measuring a problem.
  • Layout properties still cause layout or paint when animated.

Common mistakes

  • Setting will-change on many elements in a global rule, which wastes memory.
  • Leaving will-change set permanently after the animation ends.
  • Expecting will-change to speed up animations of layout properties like width or top.

Follow-ups

  • Why are transform and opacity cheap to animate?
  • How would you add and remove will-change from JavaScript?

Tips

  • Say that it is a hint with a memory cost. Show that you would measure first.
  • Mention the fixed descendant side effect. It is a real bug source.