How to create high-performance CSS animations  |  Articles  |  web.dev (2024)

How to create high-performance CSS animations | Articles | web.dev (1)

Kayce Basques

How to create high-performance CSS animations | Articles | web.dev (2)

Rachel Andrew

This guide teaches you how to create high-performance CSS animations.

See Why are some animations slow? to learn thetheory behind these recommendations.

Browser compatibility

All the CSS properties that this guide recommends have good cross-browsersupport.

transform

Browser Support

  • 36
  • 12
  • 16
  • 9

Source

opacity

Browser Support

  • 1
  • 12
  • 1
  • 2

Source

will-change

Browser Support

  • 36
  • 79
  • 36
  • 9.1

Source

Move an element

To move an element, use the translate or rotation keyword values of thetransform property.

For example, to slide an item into view, use translate.

.animate { animation: slide-in 0.7s both;}@keyframes slide-in { 0% { transform: translateY(-1000px); } 100% { transform: translateY(0); }}

Use rotate to rotate elements. The following example rotates an element360 degrees.

.animate { animation: rotate 0.7s ease-in-out both;}@keyframes rotate { 0% { transform: rotate(0); } 100% { transform: rotate(360deg); }}

Resize an element

To resize an element, use the scale keyword value of thetransform property.

.animate { animation: scale 1.5s both;}@keyframes scale { 50% { transform: scale(0.5); } 100% { transform: scale(1); }}

Change an element's visibility

To show or hide an element, use opacity.

.animate { animation: opacity 2.5s both;}@keyframes opacity { 0% { opacity: 1; } 50% { opacity: 0; } 100% { opacity: 1; }}

Avoid properties that trigger layout or paint

Before using any CSS property for animation (other than transform and opacity),determine the property's impact on the rendering pipeline.Avoid any property that triggers layout or paint unless it's absolutely necessary.

Force layer creation

As explained in Why are some animations slow?,placing elements on a new layer lets the browser repaint them without needing torepaint the rest of the layout.

Browsers can usually make good decisions about which items should be placed on anew layer, but you can manually force layer creation with thewill-change property.As the name suggests, this property tells the browser that this element is goingto be changed in some way.

In CSS, you can apply will-change to any selector:

body > .sidebar { will-change: transform;}

However, the specificationsuggests that you should only do this for elements that are always about tochange. For example, this might be true for a sidebar the user can slide in andout. For elements that don't change frequently, we recommend applyingwill-change using JavaScript when a change is likely to happen. Make sure togive the browser enough time to perform the necessary optimizations, and removethe property when the change has stopped.

If you to force layer creation in a browser that doesn't support will-change(most likely Internet Explorer), you can set transform: translateZ(0).

Debug slow or glitchy animations

Chrome DevTools and Firefox DevTools have lots of tools to help you figure outwhy your animations are slow or glitchy.

Check whether an animation triggers layout

An animation that moves an element using something other than transform islikely to be slow. The following example compares an animation using transformto an animation using top and left.

Don't

.box { position: absolute; top: 10px; left: 10px; animation: move 3s ease infinite;}@keyframes move { 50% { top: calc(90vh - 160px); left: calc(90vw - 200px); }}

Do

.box { position: absolute; top: 10px; left: 10px; animation: move 3s ease infinite;}@keyframes move { 50% { transform: translate(calc(90vw - 200px), calc(90vh - 160px)); }}

You can test this in the following two Glitch examples,and explore performance using DevTools.

Chrome DevTools

  1. Open the Performance panel.
  2. Record runtime performancewhile your animation is happening.
  3. Inspect the Summary tab.

If you see a nonzero value for Rendering in the Summary tab, it mightmean your animation is making the browser do layout work.

How to create high-performance CSS animations | Articles | web.dev (3)
How to create high-performance CSS animations | Articles | web.dev (4)

Firefox DevTools

In Firefox DevTools the Waterfallcan help you understand where the browser is spending time.

  1. Open the Performance panel.
  2. Start recording performance while your animation is happening.
  3. Stop the recording and inspect the Waterfall tab.

If you see entries for Recalculate Style,that means the browser has to return to the start of therendering waterfallto render the animation.

Check for dropped frames

  1. Open the Rendering tab in Chrome DevTools.
  2. Enable the FPS meter checkbox.
  3. Watch the values while your animation runs.

Pay attention to the Frames label at the top of the FPS meter UI.This shows values like 50% 1 (938 m) dropped of 1878. A high-performanceanimation has a high percentage, such as 99%, meaning that few frames arebeing dropped and the animation looks smooth.

How to create high-performance CSS animations | Articles | web.dev (5)
How to create high-performance CSS animations | Articles | web.dev (6)

Check whether an animation triggers paint

Some properties are more expensive for the browser to paint than others. Forexample, anything that involves a blur (like a shadow, for example) takse longerto paint than drawing a red box. These differences aren't always obvious in theCSS, but browser DevTools can help you to identify which areas need to berepainted, as well as other painting-related performance issues.

Chrome DevTools

  1. Open the Rendering tab in Chrome DevTools.
  2. Select Paint Flashing.
  3. Move the pointer around the screen.
How to create high-performance CSS animations | Articles | web.dev (7)

If you see the whole screen flashing, or areas highlighted that you don't thinkshould change, investigate further.

If you need to determine whether a particular property is causingpainting-related performance issues, the paint profilerin Chrome DevTools can help.

Firefox DevTools

  1. Open Settings and add a Toolbox button forToggle paint flashing.
  2. On the page you want to inspect, toggle the button on and move your mouse orscroll to see highlighted areas.

Conclusion

Where possible, restrict animations to opacity and transform to keepanimations on the compositing stage of the rendering path. Use DevTools to checkwhich stage of the path is being affected by your animations.

Use the paint profiler to see if any paint operations are particularlyexpensive. If you find anything, check whether a different CSS property givesthe same look and feel with better performance.

Use the will-change property sparingly, and only if you encounter a performance issue.

How to create high-performance CSS animations  |  Articles  |  web.dev (2024)

FAQs

How to create high-performance CSS animations  |  Articles  |  web.dev? ›

By changing the value of the animation-duration property, you effectively change the speed at which your animation runs. A higher value results in a slower animation, whereas a smaller value results in a faster animation.

How to increase animation speed in CSS? ›

By changing the value of the animation-duration property, you effectively change the speed at which your animation runs. A higher value results in a slower animation, whereas a smaller value results in a faster animation.

Do CSS animations use GPU? ›

In many cases, you can actually use CSS Transitions to get the job done. This works well because the browser is designed to optimize these effects and use the GPU to handle them smoothly with minimal impact on processor performance.

Do CSS animations slow down a website? ›

Leveraging animation libraries for their complex effects can inadvertently slow down your site. Before reaching for these comprehensive solutions, consider if simpler CSS animations or lightweight JavaScript libraries could achieve similar results more efficiently.

How do you make infinite animation smooth in CSS? ›

The key to creating an infinite animation in CSS is to use the animation-iteration-count property in CSS, which determines how many times the animation will play. By setting the value to infinite, the animation will continue to play indefinitely, creating a seamless and continuous effect.

How to improve CSS animation performance? ›

How to create high-performance CSS animations
  1. Browser compatibility.
  2. Move an element.
  3. Resize an element.
  4. Change an element's visibility.
  5. Avoid properties that trigger layout or paint.
  6. Force layer creation.
  7. Debug slow or glitchy animations. Check whether an animation triggers layout. Check for dropped frames. ...
  8. Conclusion.

How do I increase animation speed? ›

To speed them up, go into Android's Settings -> Developer options. Scroll down to the three scale settings (Window, Transition and Animator scales) and change all three of them from their default of "1x" to ". 5x". That's it.

Is CPU or GPU more important for animation? ›

Therefore, investing in a GPU may be crucial for 3D animations that require advanced rendering, while a strong CPU is essential for efficient multitasking and running various marketing and design software simultaneously.

How much GPU do you need for animation? ›

High-end gaming or professional GPUs, like NVIDIA's RTX or AMD's Radeon series, are suitable. Some animation software can use GPU acceleration for faster rendering. It is recommended to have a dedicated graphics card with at least 4GB of Virtual Random Access Memory (VRAM).

Is Nvidia good for animation? ›

It has been highlighted for its power and reliability. Users seeking variety to match their specific animation software needs might consider laptops with high-performance GPUs such as the Nvidia RTX 4060, RTX 4070, or RTX 4080. These advanced options ensure smoother rendering of complex animations.

What is a good FPS for animation? ›

The standard frame rate for animated videos is often 24 frames per second (fps), but it can vary based on the style and desired effect. Higher frame rates, like 30fps or 60fps, are common for smoother motion, especially in gaming or high-action sequences.

How to make CSS run faster? ›

You can make your CSS files lighter and faster to load by shrinking your CSS – that is, you can remove extra spaces and lines (this is called minifying). Then you can compress these files so they're smaller and quicker for users to download.

Should I use CSS or JavaScript animations? ›

The choice between JavaScript and CSS often comes down to the animations used and the level of interactivity needed. For simple animations, CSS might be sufficient, while more complex and interactive animations may require JavaScript for greater control and functionality.

How do I make animations run smoother? ›

Utilize Easing

Easing, also known as 'slow in and slow out', is a technique where the animation starts and ends slowly, while the middle part is faster. This results in a more natural and smoother animation. Let's see why easing is so effective: Natural Movement: Easing mimics the way things move in the real world.

How do I make my animation more dynamic? ›

Another way to create more dynamic animations is to add motion and timing to your elements, making them move and change in relation to each other and to the viewer.

Is CSS good for animation? ›

There are three key advantages to CSS animations over traditional script-driven animation techniques: They're easy to use for simple animations; you can create them without even having to know JavaScript.

How to set animation interval in CSS? ›

This may be specified in either seconds ( s ) or milliseconds ( ms ). The value must be positive or zero and the unit is required. If no value is provided, the default value of 0s is used, in which case the animation still executes (the animationStart and animationEnd events are fired).

Which CSS property controls the speed of an animation between keyframes? ›

The animation-timing-function CSS property sets how an animation progresses through the duration of each cycle.

Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6399

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.