×

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 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.

CSS Position

CSS Position: The position property in CSS is applied to set the position of the elements. It can be used to position the backside of an element. It is also...

3 minutes read.

CSS Reference

The CSS Reference lists all of the basic standard CSS properties, pseudo-classes, pseudo-elements, and data types, for all intents and purposes functional notations, and at-rules alphabetically.  The site also includes...

5 minutes read.

CSS Vertical Align

Vertical Align The vertical align CSS property describes the cell box of the table and an inline alignment vertically. This property is a self-explanatory and an essential property in CSS. But,...

2 minutes read.

CSS Browser Support

The desk underneath lists of all CSS properties and the way each belongs to it is supported in the generally unique browsers: - The quantity to the right of the browser...

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 Order

CSS Order CSS order property in CSS describes the flex item’s order inside the flex or any grid container. It is applied to order any flex item. Note: If any component is...

3 minutes read.

CSS Navigation bar

CSS Navigation bar: A navigation system or navigation bar comes upon GUI that can help many visitors to access the information. A Navigation bar is a UI component on the...

11 minutes read.

CSS translate() Function

Translate() CSS function This function in CSS is a CSS in-built function. It relocates the element in the horizontal direction or in the vertical direction. It can move the items from...

2 minutes read.

CSS Tutorial

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

9 minutes read.

CSS Syntax

The CSS syntax contains the selector and the declaration block, which will be clearer with the following example: Selector: A CSS selector specifies the HTML tag we wish to style. A selector can be...

2 minutes read.

CSS Text-stroke

Text-stroke Text-stroke CSS property inserts the stroke to any text and gives a decoration option to them also. It describes the stroke’s width and color for various text characters. It is...

2 minutes read.

Untitled Reusable Block

  ...

1 minute read.

CSS Justify Content

Justify Content: These properties are applied for the alignment of items of a flexible box container if any item does not cover every available space over the main-axis, i.e., horizontally....

4 minutes read.

CSS @keyframes rule

CSS @keyframes rule It describes the rule of animation that depicts the properties of CSS for the components at every state along with the timeline. We may make the properties of complex...

4 minutes read.

CSS Margin

CSS Margin: The CSS margin property is employed to describe the space throughout the elements. It clears the range throughout the element. These properties do not have a background color...

2 minutes read.

CSS Text Effects

Text Effects in CSS We can use distinct text effects used inside the HTML document. Few attributes can be applied to insert the text effects. We can also style various web documents...

5 minutes read.

CSS Images

Images in CSS: Images are a critical part of a web application. It includes various types of images in the web application that cannot be recommended. However, it is essential...

4 minutes read.

CSS Pseudo-elements

Pseudo-elements The pseudo-classes in CSS can be represented as any keyword which is connected to the selector that represents any selected element’s unique state. On the other hand, pseudo-elements can be...

6 minutes read.