Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CSS3 animation


May 04, 2021 CSS3


Table of contents


CSS3 animation


CSS3 animation

CSS3, we can create animations, which can replace many web animation images, Flash animations, and JAVAScripts.


CSS3
Animation

CSS3 @keyframes rules

The numbers in the table support the first browser version number of the property.

@keyframes rule is to create an animation. @keyframes A CSS style and animation within the rules will gradually change from the current style to the new style.


Browser support

Internet Explorer 10, Firefox, and Opera support @keyframes rules and animation properties.

The number immediately before -webkit-, -ms- or -moz- is the first browser version number that supports the prefix property.

Attributes
@keyframes 43.0
4.0 -webkit-
10.0 16.0
5.0 -moz-
9.0
4.0 -webkit-
30.0
15.0 -webkit-
12.0 -o-
animation 43.0
4.0 -webkit-
10.0 16.0
5.0 -moz-
9.0
4.0 -webkit-
30.0
15.0 -webkit-
12.0 -o-

CSS3 animation CSS3 animation CSS3 animation CSS3 animation CSS3 animation
@keyframes myfirst
{
from {background: red;}
to {background: yellow;}
}

@-webkit-keyframes myfirst /* Safari and Chrome */
{
from {background: red;}
to {background: yellow;}
}



CSS3 animation

When you @keyframe an animation, bind it to a selector, otherwise the animation will have no effect.

Specify that at least two of these CSS3 animation properties are bound to one selector:

  • Specify the name of the animation

  • Specify the length of the animation

CSS3 animation CSS3 animation CSS3 animation CSS3 animation CSS3 animation

Bundle the "myfirst" animation into the div element for 5 seconds:

div
{
animation: myfirst 5s;
-webkit-animation: myfirst 5s; /* Safari and Chrome */
}

Try it out . . .

Note: You must define the name of the animation and the duration of the animation. If you omit the duration, the animation will not run because the default value is 0.


What is a CSS3 animation?

Animation is the effect of gradually changing an element from one style to another.

You can change as many styles as you want.

Use a percentage to specify when the change occurred, or use the keywords "from" and "to" equivalent to 0% and 100%.

0% is the beginning of the animation and 100% is the completion of the animation.

For optimal browser support, you should always define 0% and 100% selectors.

CSS3 animation CSS3 animation CSS3 animation CSS3 animation CSS3 animation

Change the background color when the animation is 25% and 50%, and then change again when the animation is 100% complete:

@keyframes myfirst
{
0%   {background: red;}
25%  {background: yellow;}
50%  {background: blue;}
100% {background: green;}
}

@-webkit-keyframes myfirst /* Safari and Chrome */
{
0%   {background: red;}
25%  {background: yellow;}
50%  {background: blue;}
100% {background: green;}
}

Try it out . . .


CSS3 animation CSS3 animation CSS3 animation CSS3 animation CSS3 animation

Change the background color and position:

@keyframes myfirst
{
0%   {background: red; left:0px; top:0px;}
25%  {background: yellow; left:200px; top:0px;}
50%  {background: blue; left:200px; top:200px;}
75%  {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}

@-webkit-keyframes myfirst /* Safari and Chrome */
{
0%   {background: red; left:0px; top:0px;}
25%  {background: yellow; left:200px; top:0px;}
50%  {background: blue; left:200px; top:200px;}
75%  {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}

Try it out . . .



Animation properties of CSS3

The following table lists @keyframes rules and all animation properties:

Property Describe Css
@keyframes Specified animation. 3
animation Short-case properties for all animation properties, except the animation-play-state property. 3
animation-name Specifies the @keyframes of the animation. 3
animation-duration Specifies the number of seconds or milliseconds it takes for the animation to complete a cycle. The default is 0. 3
animation-timing-function Specifies the speed curve of the animation. The default is "ease". 3
animation-delay Specify when the animation will start. The default is 0. 3
animation-iteration-count Specifies the number of times the animation is played. The default is 1. 3
animation-direction Specifies whether the animation plays in reverse in the next cycle. The default is "normal". 3
animation-play-state Specifies whether the animation is running or pausing. The default is "running." 3

Here are two examples of setting all animation properties:

CSS3 animation CSS3 animation CSS3 animation CSS3 animation CSS3 animation

Run the myfirst animation and set all the properties:

div
{
animation-name: myfirst;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-play-state: running;
/* Safari and Chrome: */
-webkit-animation-name: myfirst;
-webkit-animation-duration: 5s;
-webkit-animation-timing-function: linear;
-webkit-animation-delay: 2s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
-webkit-animation-play-state: running;
}

Try it out . . .


CSS3 animation CSS3 animation CSS3 animation CSS3 animation CSS3 animation

Same as the animation above, but using the short animation animation property:

div
{
animation: myfirst 5s linear 2s infinite alternate;
/* Safari and Chrome: */
-webkit-animation: myfirst 5s linear 2s infinite alternate;
}

Try it out . . .

The above two examples set all the animation properties, you'd better look at them a few times and do it yourself!