Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Performance and Accessibility in CSS Animation | Animation in Practice
CSS Animations and Transitions

bookPerformance and Accessibility in CSS Animation

To create smooth and efficient CSS animations, you need to understand how browsers render changes to your elements. Animating certain CSS properties can trigger expensive operations that slow down your page.

Properties like transform and opacity are hardware-accelerated, meaning the browser can offload their changes to the GPU, resulting in fast, jank-free animations.

In contrast, animating properties such as width, height, top, or left causes layout thrashing, where the browser must recalculate the layout and repaint parts of the page, leading to performance issues.

To help browsers optimize your animations, you can use the will-change property to hint which properties will be animated, allowing the browser to prepare for these changes in advance. However, use will-change sparingly, as overusing it can itself degrade performance.

performant-transform-animation.html

performant-transform-animation.html

copy
layout-thrashing-animation.html

layout-thrashing-animation.html

copy

Performance is not the only concern when designing CSS animations. Accessibility should always be considered, especially for users who may experience motion sickness or distraction from moving elements.

CSS provides the prefers-reduced-motion media query, which allows you to detect if the user has requested minimal motion in their operating system preferences.

You can use this query to disable or simplify animations, ensuring your site is comfortable for everyone. For instance, in the transform-based animation example above, you could wrap the animation styles in a @media (prefers-reduced-motion: no-preference) block and provide reduced or no animation when the user prefers less motion.

question mark

Which of the following are best practices for creating smooth and accessible CSS animations?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you give an example of how to use the `will-change` property?

How do I implement the `prefers-reduced-motion` media query in my CSS?

What are some best practices for balancing performance and accessibility in CSS animations?

Awesome!

Completion rate improved to 8.33

bookPerformance and Accessibility in CSS Animation

Swipe to show menu

To create smooth and efficient CSS animations, you need to understand how browsers render changes to your elements. Animating certain CSS properties can trigger expensive operations that slow down your page.

Properties like transform and opacity are hardware-accelerated, meaning the browser can offload their changes to the GPU, resulting in fast, jank-free animations.

In contrast, animating properties such as width, height, top, or left causes layout thrashing, where the browser must recalculate the layout and repaint parts of the page, leading to performance issues.

To help browsers optimize your animations, you can use the will-change property to hint which properties will be animated, allowing the browser to prepare for these changes in advance. However, use will-change sparingly, as overusing it can itself degrade performance.

performant-transform-animation.html

performant-transform-animation.html

copy
layout-thrashing-animation.html

layout-thrashing-animation.html

copy

Performance is not the only concern when designing CSS animations. Accessibility should always be considered, especially for users who may experience motion sickness or distraction from moving elements.

CSS provides the prefers-reduced-motion media query, which allows you to detect if the user has requested minimal motion in their operating system preferences.

You can use this query to disable or simplify animations, ensuring your site is comfortable for everyone. For instance, in the transform-based animation example above, you could wrap the animation styles in a @media (prefers-reduced-motion: no-preference) block and provide reduced or no animation when the user prefers less motion.

question mark

Which of the following are best practices for creating smooth and accessible CSS animations?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 4
some-alt