×

CSS Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash.

Rules of CSS3 @keyframes

The animation can be made inside the rule of @keyframe. It can be used to handle various intermediate steps within the sequence of CSS animation.

Working of Animation

An animation enables a component change gradually through a single style to other styles. We can include the properties as much as we wish to include. Also, we can describe the modifications in percentage. 100 percent describes the animation completion, and 0 percent represents the animation start.

How it works

If animation is made within the rule of @keyframe, then it should be restrained with the selector. Otherwise, that animation will not have any effect.

An animation is restrained to a selector by describing at least the following two properties:

  1. The animation name
  2. The animation duration

Properties of Animation

PropertyDescription
@keyframesThis property is applied to describe the animation.
AnimationIt is the shorthand attribute, applied to set every property, excluding the animation-fill-mode and the animation-play-state attributes.
animation-delayThis property describes at the time of the start of an animation.
animation-directionThis property describes whether or not an animation must play inside alternate or reserve cycle.
animation-durationThis attribute describes the duration of time taken via animation for completing one cycle.
animation-fill-modeThis attribute depicts the component’s static style. (If an animation isn’t playing.)
animation-iteration-countThis attribute represents the time’s number; an animation must be played.
animation-play-stateThis property depicts when an animation is paused o running.
animation-nameThis property describes the @keyframes animation name.
animation-timing-functionThis attribute depicts the animation’s speed curve.

Consider the following examples.

  1. Example: background-color changing

Let’s take a simple animation illustration that modifies the rectangle’s background color from LIME to GREEN.

<!DOCTYPE html> 
<html> 
<head> 
<style>  
div { 
width: 100px; 
height: 100px; 
background: lime; 
-webkit-animation: myfirst 6s; /* Opera, Safari, Chrome */ 
animation: myfirst 5s; 
} 
/* Opera, Safari, Chrome */ 
@-webkit-keyframes myfirst { 
from {background: lime;} 
to {background: green;} 
} 
/* Standard syntax */ 
@keyframes myfirst { 
from {background: lime;} 
to {background: green;} 
} 
</style> 
</head> 
<body> 
<p><b>Note:</b> The IE 9 and it's earlier versions do not provide support for animation properties of CSS3. </p> 
<p> Look at the rectangle background color from lime to green. </p>
<div></div> 
</body> 
</html> 

Output:

CSS Animation
  • Example: Rectangle moving

Let’s take other illustrations to show animation along with the percentage value.

<!DOCTYPE html> 
<html> 
<head> 
<style>  
div { 
width: 100px; 
height: 100px; 
background: red; 
position: relative; 
-webkit-animation: myfirst 5s; /* Opera, Safari, Chrome */ 
animation: myfirst 5s; 
} 
/* Opera, Safari, Chrome */ 
@-webkit-keyframes myfirst { 
0% {background:pink; left:0px; top:0px;} 
25% {background:red; left:300px; top:0px;} 
50% {background:lime; left:200px; top:300px;} 
75% {background:yellow; left:0px; top:200px;} 
100% {background:blue; left:0px; top:0px;} 
} 
/* Standard syntax */ 
@keyframes myfirst { 
0% {background:pink; left:0px; top:0px;} 
25% {background:red; left:300px; top:0px;} 
50% {background:lime; left:300px; top:200px;} 
75% {background:yellow; left:0px; top:200px;} 
100% {background:blue; left:0px; top:0px;} 
} 
</style> 
</head> 
<body> 
<p><b>Note:</b> The Internet Explorer 9 and its earlier versions don't support this illustration. </p> 
<div></div> 
</body> 
</html> 

Output:

CSS Animation

Related Topics

CSS Padding

CSS Padding: The padding property in CSS is addressed to describe the space among the element border and the element content.This property is quite different from the margin property of...

2 minutes read.

CSS :root selector

CSS :root selector This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree. An element of <html> is always a...

2 minutes read.

CSS Background-blend-mode

Background-blend-mode The background-blend-mode property sets a blend mode of the component’s every background layer (color/image). It illustrates how the element background image blends well with the element background color. We can...

9 minutes read.

CSS Units

CSS Units In CSS, there are so many units available to show the length’s measurement. A single unit of CSS is applied to examine the size of the property, which the...

5 minutes read.

CSS Image Gallery

Image galleries are used to display and store collections of images. Example Let’s discuss an example. CSS was used to create the following image gallery: Code is as follows: <html> <head> <style> div.gallery {   margin: 6px;   border: 1px...

5 minutes read.

How to edit CSS in WordPress

Developing a new website or blog is a difficult undertaking. We want to create something that the consumers will adore, but we might not want the setting to take months and...

7 minutes read.

CSS Arrow

CSS Arrow The arrow of CSS is applied to insert an array with the tooltip. It can make a tooltip as any speech bubble. Also, this arrow can be illustrated in...

4 minutes read.

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

3 minutes read.

Hover condition for a:before and a:after in CSS

In CSS, :after and :before are used to add content after and before the element. The CSS pseudo-class is used to add some styling effect to the webpage. We can change...

4 minutes read.

CSS Links

CSS Link is used to create styled link. There are various ways to implement it. Styling Links Link can be styled with CSS properties like color, font-family, background etc. Example: <!DOCTYPE html>   <html>   <head>   <style>   a {       color: red;   }   </style>   </head>   <body>   <p><b><a href="https://www.tutorialandexample.com"    target="_blank">This is a link</a></b></p>   </body>   </html> Try Now There are four...

2 minutes read.

CSS 2d Transform

Transform property in CSS is used mainly for enhancing the effects and to animate the elements. Transform is an effect that changes the shape, size and position of an element. It helps...

6 minutes read.

CSS Math Functions

The CSS math functions permit numerical articulations. These CSS math functions can particularly be utilized in maybe unforeseen ways, for example, inside angles and shading the functions and in the...

3 minutes read.

CSS Checkbox style

CSS Checkbox style: It is an element of HTML, which is used to get input from various users. It is difficult to style any checkbox. However, some elements make it...

6 minutes read.

How to create blinking text using CSS

The blinking effect is used in HTML and CSS to take the attraction towards a particular word or section. If you need such a blinking effect, we need to take...

2 minutes read.

CSS Page-break-after Attribute

Page-break-after Attribute As its name implies, the page-break-before property is applied to include page break after any element, at the time of printing a document. It adds the page break after...

5 minutes read.

CSS Outline

CSS Outline: This CSS property is quite similar to the border property of CSS. It eases us to design an additional border around the element to obtain visual attention. The...

4 minutes read.

Combine Background Image with Gradient Overlay

With the help of CSS gradient, we can display smooth and straightforward transitions between two or more colours. We can add the background images by combining the gradient property with...

2 minutes read.

Internal CSS

What is CSS? CSS stands for Cascading Style Sheet.CSS is used to apply styling the HTML elements.With CSS user can add or change color, font-size and font-family. It also helps user...

4 minutes read.

CSS z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

CSS: nth-selector

CSS nth selector can be used to match an element according to its position regardless of its parent’s type. Here, n can be any number, formula, or a keyword. These selectors...

2 minutes read.