Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära 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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 8.33

bookPerformance and Accessibility in CSS Animation

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4
some-alt