What is CSS will-change
CSS will-change
is a property in CSS that provides a hint to the browser on which properties of an element are likely to change. This allows the browser to optimize the element for animations and other performance-critical updates, making it possible to achieve smoother and more efficient animations.
Example
The will-change
property is easy to use. All you need to do is specify which CSS property or properties will change. For instance, if you want to make an element smoother during an animation that changes its opacity, you can write the code as below:
.animated_item{
will-change: opacity;
}
This will inform the browser that the opacity of the element is likely to change, allowing it to optimize the element for the animation, resulting in smoother and more efficient animations.
To declare multiple values, you can use a comma-separated list, for example:
.animated_item{
will-change: opacity, transform;
}
Syntax
Using the will-change
property can be a powerful tool for improving the performance of CSS animations and other graphical updates. The most common use of will-change
is to improve the performance of animations, but it can also be used to optimize other types of updates, such as scrolling and user interaction.
values | Description |
---|---|
| signifies to the user agent that it should use its usual methods and optimizations |
| signifies to the user agent that there will be a change/animation on the scroll position of the element in the near future |
| signifies to the user agent that there will be a change/animation of the contents of the element in the near future |
| signifies to the user agent that there will be a change/animation of the property with the given name of the element in the near future (for eg: opacity , , left ,…)Note: If a shorthand property is given, it indicates that all the individual properties that the shorthand expands to are expected to change. |
Key Considerations and Points to Remember
1. Not a guarantee of improved performance
- It’s important to note that using
is not a guarantee of improved performance. The browser may choose to ignore the hint, or the optimization may have the opposite effect and slow down performance. To determine the impact of thewill-change
property on your animations, it’s recommended to test the animation in various browsers and devices to see the effect on performance.will-change
2. Use it as a last-resort solution
- The
will-change
property should not be used unless necessary, do not use it to try to achieve small increases in performance, as adding it to elements without performance issues can harm the performance instead of improving it. It is recommended to only use
as a last-resort solution.will-change
3. Do not overuse will-change
- You should be careful not to overuse
, as too many elements with this property can lead to increased memory usage and degraded performance. It’s also important to only usewill-change
on elements that will actually change, as specifying it on elements that will remain unchanged can slow down performance.will-change
5. Allowing sufficient time for browser to do optimization
- When we apply the CSS
will-change
property, it informs the browser that certain properties of an element are expected to change. However, it is important to note that the browser should be given some time ahead for optimizations to occur before changes are made to the element’s properties.
6. Do note on the influence the visual appearance of elements
- When the
will-change
property is used, it can influence the visual appearance of elements in the stacking context by affecting the stacking order. For example, if the
property is set towill-change
opacity
, the browser may choose to create a new stacking context for that element, which could change its position in the stacking order.
Browser compatibility
is supported in most of the modern browser:will-change
Desktop | Mobile |
---|---|
![]() ![]() ![]() ![]() ![]() | ![]() ![]() ![]() ![]() ![]() ![]() |
source: https://developer.mozilla.org/en-US/docs/Web/CSS/will-change#browser_compatibility
Conclusion
In conclusion, the CSS
property provides a powerful way to optimize CSS animations and other performance-critical updates. When used correctly, it can result in smoother and more efficient animations, but it should be used with care, as incorrect usage can have the opposite effect and slow down performance. To ensure the best results, it’s recommended to test the impact of will-change
on your animations in various browsers and devices.will-change
Leave a Reply