Sitemap
Patrick Rogers

Patrick Rogers

This needs more A1 sauce.

CSS Forced Rendering

3 min readDec 2, 2014

--

Ever noticed that short flicker or delay when rendering a webpage that contains a lot of heavy content? Pages that suffer the worst are those with copious amounts of animations and transitions. This delay in processing is a normal limitation in browsers and can be fixed by using the will-change class in CSS3.

This flicker that you see when loading a page is a delay in your browser properly rendering the page due to this ‘excessive’ use of code. The reason I put the word excessive in quotes is because, as CSS3 becomes more prevalent, the animations and transitions touted in CSS3 are more commonplace. However most browsers still struggle optimizing rendering of animations and sometimes need some help.

One solution for this behavior is called hardware acceleration. It means manually telling your graphics card to render that section of the code instead of forcing your CPU to do all of the work. Pushing complicated animations to the GPU has its advantages; it allows the page to access more resources for rendering graphics that your computer already has.

The older (and more hacky) way of coaxing smoother animations out of your browser was by adding a translateZ() (or translate3d()) to the class you wanted rendered smoothly. This would, and still does, force the browser to move that rendering from the CPU to the GPU.

This smoothing was done by adding any 3d value like: transform: translate3d(0, 0, 0);. This is taxing on the browser and shouldn’t be used on every element in the page. The reason is that, by forcing something from to your GPU, you are using up valuable RAM that becomes especially important when viewing webpages on mobile. This especially becomes relevant when you are loading multiple elements and the 3d translations that are used more than once, or even worse, repeated indefinitely.

Pushing animations to the GPU via 3dtransforms also slow down page performance as they are only used right when the complex animation is being used. It’s like asking someone for a favor right when they are walking out of the door. Don’t be that person.

Get Patrick Rogers’s stories in your inbox

Join Medium for free to get updates from this writer.

Here is where the will-change property becomes important. Will-change allows you to nicely let the Chrome, Firefox, and Opera browsers know which elements will be taxing on the CPU on page load and let the browser render them in layers separately from the rest of the page. This also works with repeated layers as the browser has already been informed by the property that the element is graphically heavy. Essentially it tells the browser what elements will be animating before they actually animate.

will-change: transform

Still you should use will-change to hint to the browser about an upcoming transformation can be as simple as adding this rule to the element that you’re expecting to be transformed (see example below):

Want to get even more fancy? Will-change allows you to explicitly define the values that you intend to change from within the property. Say you are going to change the scale and opacity of a given animation (like the one above) you could use:

will-change: scale, opacity;

This tells the browser to pay more attention to the properties that you define in the will-change as ones that are important to render or will require hardware acceleration. Specifying task heavy aspects of the CSS also lets the browser more effectively optimize the page rendering and give you a speed boost without forcing hacking the browser to create layers you may not be using.

Be careful with will-change in your own projects. Browsers already do a great job rendering elements and get upset when there are too many elements that forced to the GPU. Will-change shines it’s brightest when used on a small number of elements that need to be used repeatedly and rendered fast and smoothly. Be sure use will-change on classes before the animation or transition occurs as to give the browser enough time to react to the notification. Be sure to also remove the will-change once the animation is complete. Remember, the property still pushes rendering on the webpage to your GPU and will continue to use RAM until the browser is allowed to return to it’s normal rendering state. Will-changes let you write a hack-free, optimized animations without annoying flickers and should be a tool always in the back of the toolbelt… just in case.

Read more about will-change here.

--

--