GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing developer.mozilla.org — animation CSS property - CSS | MDN
|
| Initial value | as each of the properties of the shorthand:
|
|---|---|
| Applies to | all elements |
| Inherited | no |
| Computed value | as each of the properties of the shorthand:
|
| Animation type | Not animatable |
animation =
<single-animation>#
<single-animation> =
<'animation-duration'> ||
<easing-function> ||
<'animation-delay-start'> ||
<single-animation-iteration-count> ||
<single-animation-direction> ||
<single-animation-fill-mode> ||
<single-animation-play-state> ||
[ none | <keyframes-name> ] ||
<single-animation-timeline>
<animation-duration> =
[ auto | <time [0s,∞]> ]#
<easing-function> =
<linear-easing-function> |
<cubic-bezier-easing-function> |
<step-easing-function>
<animation-delay-start> =
<time>#
<single-animation-iteration-count> =
infinite |
<number [0,∞]>
<single-animation-direction> =
normal |
reverse |
alternate |
alternate-reverse
<single-animation-fill-mode> =
none |
forwards |
backwards |
both
<single-animation-play-state> =
running |
paused
<keyframes-name> =
<custom-ident> |
<string>
<single-animation-timeline> =
auto |
none |
<dashed-ident> |
<scroll()> |
<view()>
<linear-easing-function> =
linear |
<linear()>
<cubic-bezier-easing-function> =
ease |
ease-in |
ease-out |
ease-in-out |
<cubic-bezier()>
<step-easing-function> =
step-start |
step-end |
<steps()>
<scroll()> =
scroll( [ <scroller> || <axis> ]? )
<view()> =
view( [ <axis> || <'view-timeline-inset'> ]? )
<linear()> =
linear( [ <number> && <percentage>{0,2} ]# )
<cubic-bezier()> =
cubic-bezier( [ <number [0,1]> , <number> ]#{2} )
<steps()> =
steps( <integer> , <step-position>? )
<scroller> =
root |
nearest |
self
<axis> =
block |
inline |
x |
y
<view-timeline-inset> =
[ [ auto | <length-percentage> ]{1,2} ]#
<integer> =
<number-token>
<step-position> =
jump-start |
jump-end |
jump-none |
jump-both |
start |
end
<length-percentage> =
<length> |
<percentage>
Note: Animating CSS box model properties is discouraged as it leads to layout reflow and repaints. Animating any box model property is inherently CPU-intensive; consider animating the transform property instead.
In this example, we demonstrate basic usage of the animation shorthand by animating a yellow sun across a light blue sky. The sun rises to the center of the viewport and then falls out of sight.
We include a single <div> element to represent our sun.
<div class="sun"></div>
We start by creating the sun and the sky. The sky is the :root of the HTML document. We hide any content that is outside the viewport, which in our case will be any part of the sun below the horizon, by setting the overflow to hidden. We also use the justify-content property to center the sun in the background. We make the sun yellow, declare its height to be the height of the viewport (100vh), and set its width to equal its height by setting the aspect-ratio to 1. We turn the square <div> into a circle using the border-radius property.
:root {
overflow: hidden;
background-color: lightblue;
display: flex;
justify-content: center;
}
.sun {
background-color: yellow;
border-radius: 50%;
height: 100vh;
aspect-ratio: 1;
animation: 4s linear 0s infinite alternate sunrise;
}
Next, we define some animation @keyframes that will push the element on which they are applied down past the viewport and then return the element to its default position using CSS transforms:
@keyframes sunrise {
from {
transform: translateY(110vh);
}
to {
transform: translateY(0);
}
}
The last step is to apply the animation! We use the animation shorthand property to apply the sunrise keyframe animation to the .sun <div>. The animation is set to play for infinite iterations, with each one lasting 4 seconds; the animation direction alternates with each iteration:
.sun {
animation: 4s linear 0s infinite alternate sunrise;
}
This example demonstrates applying multiple animations to a single element. Expanding on the previous example, with a sun that rises and falls on a light blue background, here we will gradually rotate the sun through a rainbow of colors. The timing of the sun's position and color are independent.
<div class="sun"></div>
:root {
overflow: hidden;
background-color: lightblue;
display: flex;
justify-content: center;
}
.sun {
background-color: yellow;
border-radius: 50%;
height: 100vh;
aspect-ratio: 1 / 1;
}
@keyframes sunrise {
from {
transform: translateY(110vh);
}
to {
transform: translateY(0);
}
}
We include the same HTML and CSS as in the previous example, and add a second set of animation @keyframes to apply a filter that rotates the hue through all possible values using the hue-rotate() filter function:
@keyframes psychedelic {
from {
filter: hue-rotate(0deg);
}
to {
filter: hue-rotate(360deg);
}
}
We then apply the two animations to our sun. Multiple animations are separated by commas, and each animation's parameters are set independently:
.sun {
animation:
4s linear 0s infinite alternate sunrise,
24s linear 0s infinite psychedelic;
}
This example demonstrates what happens when multiple animations define values for the same property. This example expands upon the basic usage example, with two animations applied that both set a transform value.
<div class="sun"></div>
:root {
overflow: hidden;
background-color: lightblue;
display: flex;
justify-content: center;
}
.sun {
background-color: yellow;
border-radius: 50%;
height: 100vh;
aspect-ratio: 1 / 1;
}
We use the same HTML and CSS as in the first example, including the original sunrise animation, and a second animation named bounce. The two animations declare values for the same property:
@keyframes sunrise {
from {
transform: translateY(110vh);
}
to {
transform: translateY(0);
}
}
@keyframes bounce {
from {
transform: translateX(-50vw);
}
to {
transform: translateX(50vw);
}
}
We apply both animations to the sun. When two animations apply different values to the same property, animations declared later in the cascade override previously-declared animations. In this case, the transform value on the bounce animation "wins" the cascade, and overrides the transform set by sunrise, so the sun will only move horizontally.
.sun {
animation:
4s linear 0s infinite alternate sunrise,
4s linear 0s infinite alternate bounce;
}
The sun bounces between the
left- and right-hand sides of the viewport. The sun remains in the viewport even
though the sunrise animation is defined. The sunrise animation's transform property
is overridden by the bounce animation.
| Specification |
|---|
| CSS Animations Level 1 # animation |
AnimationEvent API