Skip to content

Tailwind CSS - Will Change

# Modern Guide to Tailwind CSS: Will Change

Section titled “# Modern Guide to Tailwind CSS: Will Change”

The will-change utilities are a performance optimization tool. They act as a hint to the browser, informing it about properties on an element that are likely to change in the future. This allows the browser to set up optimizations before they are needed, potentially preventing lag and creating smoother animations.

A Word of Caution: The Dangers of Premature Optimization

Section titled “A Word of Caution: The Dangers of Premature Optimization”

This is an advanced utility that should be used sparingly. The will-change property is often misunderstood and overused. Applying it incorrectly can actually harm performance by consuming memory for optimizations that are never used.

Rule of Thumb: Do not use will-change unless you are experiencing a noticeable performance problem (jank, flickering, stuttering) during a transition or animation and have identified will-change as a potential solution.

These utilities tell the browser which property you expect to animate.

ClassCSS PropertiesWhen to Use
will-change-scrollwill-change: scroll-position;For an element whose scroll position is expected to animate.
will-change-contentswill-change: contents;For an element whose content is expected to change.
will-change-transformwill-change: transform;Crucial for elements that will be animated using transform (e.g., scale, rotate, translate).
will-change-autowill-change: auto;The default value, which applies no special optimizations.

## Practical Example: Smoothing a Transform Animation

Section titled “## Practical Example: Smoothing a Transform Animation”

A common and valid use case for will-change is to smooth out CSS transform animations. By adding will-change-transform, we suggest to the browser that it should promote the element to its own compositor layer, making transform and opacity changes much cheaper to process.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern Will Change Example</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-100 dark:bg-slate-900 p-8 grid place-content-center min-h-screen font-sans">
<div class="text-center">
<h1 class="text-2xl font-bold text-slate-900 dark:text-white mb-6">Optimizing Animations</h1>
<div class="flex space-x-8">
<!-- Without will-change -->
<div>
<p class="mb-2 font-semibold text-slate-700 dark:text-slate-300">Standard Transform</p>
<div class="w-32 h-32 bg-rose-500 rounded-lg flex items-center justify-center text-white font-bold transition-transform duration-500 ease-in-out hover:scale-125 hover:rotate-12">
Hover Me
</div>
</div>
<!-- With will-change-transform -->
<div>
<p class="mb-2 font-semibold text-slate-700 dark:text-slate-300">With `will-change-transform`</p>
<div class="w-32 h-32 bg-indigo-500 rounded-lg flex items-center justify-center text-white font-bold transition-transform duration-500 ease-in-out hover:scale-125 hover:rotate-12 will-change-transform">
Hover Me
</div>
<p class="text-xs text-slate-500 mt-2 max-w-xs">May appear smoother on complex pages or slower devices.</p>
</div>
</div>
</div>
</body>
</html>
  • Be Specific: Use the most specific utility possible. If you are animating a transform, use will-change-transform, not will-change-contents.
  • Apply and Remove: In an ideal world, you would add the will-change property via JavaScript right before an animation begins and remove it after the animation completes. For simple hover effects, leaving it in the class list is an acceptable trade-off.
  • Test, Don’t Guess: Use your browser’s developer tools (like the Performance tab in Chrome DevTools) to measure the impact of will-change. If it doesn’t improve your frame rate, remove it.
  • transform and opacity: The biggest performance gains are seen when animating the transform and opacity properties, as these can be handled entirely by the GPU if the element is on its own compositor layer. will-change-transform is the key to unlocking this.