🥳
Black Friday Sale is on: Get Amazing deals on tools and resourcess
postImage

Introduction to CSS Animations

blogImage

By Emmanuel Chinonso

Web Developer

CSS Animations

An animation lets an element gradually change from one style to another. CSS allows animation of HTML elements without using JavaScript or Flash.

Contrast Bootstrap UI Kit

An animation lets an element gradually change from one style to another. You can change as many CSS properties you want, as many times you want. To use CSS animation, you must first specify some keyframes for the animation. Keyframes hold what styles the element will have at certain times.

The @keyframes Rule

When you specify CSS styles inside the @keyframes rule, the animation will gradually change from the current style to the new style at certain times. To get an animation to work, you must bind the animation to an element. The following example binds the "example" animation to the <div> element. The animation will last for 4 seconds, and it will gradually change the background-color of the <div> element from "red" to "yellow":

CSS Code:

1/* The animation code */
2
3@keyframes example {
4 from {background-color: red;}
5 to {background-color: yellow;}
6}
7
8/* The element to apply the animation to */
9
10div {
11 width: 100px;
12 height: 100px;
13 background-color: red;
14 animation-name: example;
15 animation-duration: 4s;
16}

The animation-duration property defines how long time an animation should take to complete. If the animation-duration property is not specified, no animation will occur, because the default value is 0s (0 seconds). The following example will change the background-color of the '<div> element when the animation is 25% complete, 50% complete, and again when the animation is 100% complete:

CSS Code:

1/* The animation code */
2
3@keyframes example {
4 0% {background-color: red;}
5 25% {background-color: yellow;}
6 50% {background-color: blue;}
7 100% {background-color: green;}
8}
9
10/* The element to apply the animation to */
11
12div {
13 width: 100px;
14 height: 100px;
15 background-color: red;
16 animation-name: example;
17 animation-duration: 4s;
18}

The following example will change both the background-color and the position of the <div> element when the animation is 25% complete, 50% complete, and again when the animation is 100% complete:

CSS Code:

1/* The animation code */
2@keyframes example {
3 0% {background-color:red; left:0px; top:0px;}
4 25% {background-color:yellow; left:200px; top:0px;}
5 50% {background-color:blue; left:200px; top:200px;}
6 75% {background-color:green; left:0px; top:200px;}
7 100% {background-color:red; left:0px; top:0px;}
8}
9/* The element to apply the animation to */
10div {
11 width: 100px;
12 height: 100px;
13 position: relative;
14 background-color: red;
15 animation-name: example;
16 animation-duration: 4s;
17}

Delay an Animation

The animation-delay property specifies a delay for the start of an animation. The following example has a 2 seconds delay before starting the animation:

CSS Code:

1div {
2 width: 100px;
3 height: 100px;
4 position: relative;
5 background-color: red;
6 animation-name: example;
7 animation-duration: 4s;
8 animation-delay: 2s;
9}

Negative values are also allowed. If using negative values, the animation will start as if it had already been playing for N seconds. In the following example, the animation will start as if it had already been playing for 2 seconds:

CSS Code:

1div {
2 width: 100px;
3 height: 100px;
4 position: relative;
5 background-color: red;
6 animation-name: example;
7 animation-duration: 4s;
8 animation-delay: -2s;
9}

Set How Many Times an Animation Should Run: The animation-iteration-count property specifies the number of times an animation should run.

The following example will run the animation 3 times before it stops:

CSS Code:

1div {
2 width: 100px;
3 height: 100px;
4 position: relative;
5 background-color: red;
6 animation-name: example;
7 animation-duration: 4s;
8 animation-iteration-count: 3;
9}

Run Animation in Reverse Direction or Alternate Cycles: The animation-direction property specifies whether an animation should be played forwards, backwards or in alternate cycles. The animation-direction property can have the following values:

normal - The animation is played as normal (forwards). This is default

reverse - The animation is played in reverse direction (backwards)

alternate - The animation is played forwards first, then backwards

alternate-reverse - The animation is played backwards first, then forwards

The following example will run the animation in reverse direction (backwards):

CSS Code:

1div {
2 width: 100px;
3 height: 100px;
4 position: relative;
5 background-color: red;
6 animation-name: example;
7 animation-duration: 4s;
8 animation-direction: reverse;
9}

Specify the fill-mode For an Animation CSS animations do not affect an element before the first keyframe is played or after the last keyframe is played. The animation-fill-mode property can override this behavior.

The animation-fill-mode property specifies a style for the target element when the animation is not playing (before it starts, after it ends, or both). The animation-fill-mode property can have the following values:

none - Default value. Animation will not apply any styles to the element before or after it is executing

forwards - The element will retain the style values that is set by the last keyframe (depends on animation-direction and animation-iteration-count)

backwards - The element will get the style values that is set by the first keyframe (depends on animation-direction), and retain this during the animation-delay period

both - The animation will follow the rules for both forwards and backwards, extending the animation properties in both directions

The following example lets the <div> element retain the style values from the last keyframe when the animation ends:

CSS Code:

1div {
2 width: 100px;
3 height: 100px;
4 background: red;
5 position: relative;
6 animation-name: example;
7 animation-duration: 3s;
8 animation-fill-mode: forwards;
9}

The following example lets the <div> element get the style values set by the first keyframe before the animation starts (during the animation-delay period).

CSS Code:

1div {
2 width: 100px;
3 height: 100px;
4 background: red;
5 position: relative;
6 animation-name: example;
7 animation-duration: 3s;
8 animation-delay: 2s;
9 animation-fill-mode: backwards;
10}

The following example lets the <div> element get the style values set by the first keyframe before the animation starts, and retain the style values from the last keyframe when the animation ends.

CSS Code:

1div {
2 width: 100px;
3 height: 100px;
4 background: red;
5 position: relative;
6 animation-name: example;
7 animation-duration: 3s;
8 animation-delay: 2s;
9 animation-fill-mode: both;
10}

Contrast Bootstrap UI Kit

Create Stunning websites and web apps

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Try contrast pro

ad-banner

Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast

Related Posts

Comments

...