--- url: "https://web.dev/learn/css/animations" titulek: "Animations | web.dev" h1: "Animations Stay organized with collections Save and categorize content based on your preferences." popis: "Animation is a great way to highlight interactive elements, and add interest and fun to your designs. In this module, find out how to add and control animation effects with CSS." jazyk: "en" kanonicka: "https://web.dev/learn/css/animations" drobecky: ["web.dev", "Resources", "CSS"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 03:31:38" znaku: 11399 slov: 1587 kodovani: "iso-8859-2" sha256: "c6626872a7b884bba55be0dafcb3d377635cae431298be3ddc07c2373df1829a" --- Animations Stay organized with collections Save and categorize content based on your preferences. [The CSS Podcast - 022: Animation](https://thecsspodcast.libsyn.com/022-animation) Sometimes you see little helpers on interfaces that when clicked, provide helpful information about that particular section. These often have a pulsing animation to subtly let you know that the information is there and should be interacted with. This module shows you how to create those helpers, and other animations, using CSS. You can use CSS to set an animation sequence with keyframes. These sequences can be basic, one-state animations or complex, time-based sequences. What is a keyframe? In most animation tools, keyframes are the mechanism you use to assign animation states to timestamps on a timeline. For example, here's a timeline for the pulsing "helper" dot. The animation runs for 1 second and has 2 states. There's a specific point where each of these animation states start and end. You map these out on the timeline with keyframes. `@keyframes` Browser Support 43 12 16 9 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/At-rules/@keyframes) CSS [`@keyframes`](https://developer.mozilla.org/docs/Web/CSS/@keyframes) are based on the same concept as animation keyframes. Here's an example with two states: `@keyframes my-animation { from { transform: translateY(20px); } to { transform: translateY(0px); } }` The first important part is the [custom identifier (`custom-ident`)](https://developer.mozilla.org/docs/Web/CSS/custom-ident), the name of the keyframes rule. The identifier in this example is `my-animation`. The custom identifier works like a [function](/learn/css/functions) name, letting you reference the keyframes rule elsewhere in your CSS code. Inside the keyframes rule, `from` and `to` are keywords that represent `0%` and `100%`, which are the start and end of the animation. You could re-create the same rule like this: `@keyframes my-animation { 0% { transform: translateY(20px); } 100% { transform: translateY(0px); } }` You can add as many positions as you like along the timeframe. In the pulsing helper example, there are two states that translate to two keyframes. This means you have two positions inside your keyframes rule to represent the changes for each of these keyframes. `@keyframes pulse { 0% { opacity: 0; } 50% { transform: scale(1.4); opacity: 0.4; } }` The `animation` properties Browser Support 43 12 16 9 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/animation) To use your `@keyframes` in a CSS rule, you can either define various animation properties individually, or use the [`animation`](https://developer.mozilla.org/docs/Web/CSS/animation) shorthand property. `animation-duration` Browser Support 43 12 16 9 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/animation-duration) `.my-element { animation-duration: 10s; }` The [animation-duration](https://developer.mozilla.org/docs/Web/CSS/animation-duration) property defines how long the `@keyframes` timeline should be as a time value. It defaults to 0 seconds, which means the animation still runs, but it'll be too quick for you to see. You can't use negative time values. `animation-timing-function` Browser Support 43 12 16 9 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/animation-timing-function) To help recreate natural motion in animation, you can use timing functions that calculate the speed of an animation at each point. Calculated values are often *curved*, making the animation run at variable speeds over the course of `animation-duration`, and making the element appear to bounce if the browser calculates a value beyond those defined in `@keyframes`. There are several keywords available as presets in CSS, which are used as the value for [animation-timing-function](https://developer.mozilla.org/docs/Web/CSS/animation-timing-function): `linear`, `ease`, `ease-in`, `ease-out`, `ease-in-out`. `.my-element { animation-timing-function: ease-in-out; }` Easing function values appear to curve because easing is calculated using a **Bézier curve**, a type of function used to model velocity. Each of the timing function keywords, such as `ease`, references a predefined Bézier curve. In CSS, you can define a Bézier curve directly using the `cubic-bezier()` function, which accepts four number values: `x1`, `y1`, `x2`, `y2`. `.my-element { animation-timing-function: cubic-bezier(.42, 0, .58, 1); }` These values plot each part of the curve along the X and Y axis. Understanding Bézier curves is complicated. Visual tools, such as this [generator](https://cubic-bezier.com/) by Lea Verou, are very helpful. The `steps` easing function Sometimes you might want to take more granular control of your animation by moving in intervals instead of along a curve. The `steps()` easing function lets you break the timeline into defined intervals of **equal duration**. `.my-element { animation-timing-function: steps(10, end); }` The first argument is the number steps. If there are the same number of keyframes as steps, each keyframe plays in sequence for the exact duration of its step, with no transition between states. If there are fewer keyframes than steps, the browser adds steps between keyframes depending on the second argument. The second argument is the direction. If it's set to `end`, which is the default, the steps finish at the end of your timeline. If it's set to `start`, the first step of your animation completes as soon as it starts, which means it ends one step earlier than `end`. `animation-iteration-count` Browser Support 43 12 16 9 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/animation-iteration-count) `.my-element { animation-iteration-count: 10; }` The [animation-iteration-count](https://developer.mozilla.org/docs/Web/CSS/animation-iteration-count) property defines how many times the `@keyframes` timeline should run during the animation. By default, this is 1, which means that the animation stops when it reaches the end of your timeline. This value can't be a negative number. To make your animation loop, set the iteration count to `infinite`. This is how the pulsing animation from the start of this lesson works. `animation-direction` Browser Support 43 12 16 9 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/animation-direction) `.my-element { animation-direction: reverse; }` You can set which direction the timeline runs over your keyframes with [animation-direction](https://developer.mozilla.org/docs/Web/CSS/animation-direction), which takes the following values: `normal`: the default value, which is forward. `reverse`: runs backward over your timeline. `alternate`: for each animation iteration, the timeline alternates between running forward and running backward. `alternate-reverse`: Like `alternate`, but the animation starts with the timeline running backward. `animation-delay` Browser Support 43 12 16 9 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/animation-delay) `.my-element { animation-delay: 5s; }` The [animation-delay](https://developer.mozilla.org/docs/Web/CSS/animation-delay) property defines how long the browser waits before starting the animation. Like the `animation-duration` property, this accepts a time value. Unlike `animation-duration`, you *can* define `animation-delay` as a negative value, which makes the animation start at the corresponding point in your timeline. For example, if your animation is 10 seconds long and you set `animation-delay` to `-5s`, the animation starts from halfway through your timeline. `animation-play-state` Browser Support 43 12 16 9 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/animation-play-state) `.my-element:hover { animation-play-state: paused; }` The [animation-play-state](https://developer.mozilla.org/docs/Web/CSS/animation-play-state) property lets you play and pause the animation. The default value is `running`. If you set it to `paused`, the animation pauses. `animation-fill-mode` Browser Support 43 12 16 9 [Source](https://developer.mozilla.org/docs/Web/CSS/Reference/Properties/animation-fill-mode) The [animation-fill-mode](https://developer.mozilla.org/docs/Web/CSS/animation-fill-mode) property defines which values in your `@keyframes` timeline persist before the animation starts or after it ends. The default value is `none`, which means that when the animation is complete, the values in your timeline are discarded. Other options include: `forwards`: The last keyframe persists, based on the animation direction. `backwards`: The first keyframe persists, based on the animation direction. `both`: Both the first and last keyframes persist. The `animation` shorthand Instead of defining each property separately, you can define them in an `animation` shorthand, which lets you define the animation properties in the following order: `animation-name` `animation-duration` `animation-timing-function` `animation-delay` `animation-iteration-count` `animation-direction` `animation-fill-mode` `animation-play-state` `.my-element { animation: my-animation 10s ease-in-out 1s infinite forwards forwards running; }` Considerations when working with animation Users can set their operating system to [prefer reduced motion](/articles/prefers-reduced-motion) when interacting with applications and websites. You can detect this preference using the [prefers-reduced-motion](https://developer.mozilla.org/docs/Web/CSS/@media/prefers-reduced-motion) media query: `@media (prefers-reduced-motion) { .my-autoplaying-animation { animation-play-state: paused; } }` This isn't necessarily a preference for no animation. It's a preference for less animation, especially less unexpected animation. You can learn more about this preference and overall performance in our [animation guide](/explore/animations). Check your understanding Test your knowledge of animations Is the **name or custom identifier** of a `@keyframes` animation case sensitive? Yes 🎉 No CSS does not allow 2 animations to have the same name, but it does allow `SWOOP` and `swoop` to coexist. The keywords `from` and `to` are the same as: `start` and `end`. Try again! `0%` and `100%`. `from` is the same as `0%` and `to` is the same as 100%. `0` and `1` Try again! The `animation-timing-function` is also commonly known as: Dynamic timing Try again! Delay Try again! Easing 🎉 What is the minimum number of keyframes required inside a `@keyframes` animation? 1 The browser will take the current state of the element as a keyframe, so at minimum, 1 keyframe is required. 2 Try again! 3 Try again! 4 Try again! [Previous arrow_back Gradients](/learn/css/gradients) [Next Filters arrow_forward](/learn/css/filters) Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2021-05-04 UTC.