You are currently viewing A Great Example to Understand CSS Keyframe Animation
  • Post category:CSS

A Great Example to Understand CSS Keyframe Animation

What is keyframe in animation

In this article, we will see about what is keyframe in animation and how to use it in CSS.


It is a technique, which is generally used for animating thing and they are not specific to CSS, HTML, JavaScript or programming in General. A keyframe is nothing else than a frame to which you assign a certain value of a property. Your animation software or in our case the browser is then automatically animating the property on the regular frames between two keyframes.

If you have an animation consisting of 1000 frames and you want to animate the position of an object. So, beginning of your animation, your object should be on the left side and then it should move to the rightside of the screen. Let’s see an example of keyframe usage.

Example of keyframes


      <div class="container">
         <div class="object"> </div>



            display: flex;
            align-items: center;
            justify-content: center;
            display: flex;
            border: 2px solid black;
            width: 400px;
            height: 400px;
            width: 100px;
            height: 100px;
            background-color: darkred;
            position: relative;
            left: 150px;
            top: 150px;
  1. In the above code, we have creted a <div> with classname “container” and inside it another <div> tag with classname “object”.
  2. Then in CSS, we have set the border, width, and height to containter div. Then we select the “object” using classselector in CSS, set the “position” to “relative”, background-color, width, and height. See, the output, the object is displayed centered inside the container.
    output:what is keyframe in animation
  3. Now, we will create an animation where the object is moving leftside to the rightside.
  4. To do that by using two keyframes, on the 1st keyframes will be first frame of the animation for beginning our red object should be on the leftside. The 2nd keyframe will be the last frame of the animation, where we say that our red object square should be on the rightside. Then the browser is able to generate the animation, where the red object slowly moving from left to right. Let’s see how to create keyframe:
    @keyframes lefttoRight{
                   left: 0px;
                   left: 300px;
  5. Write the keyword “@keyframes” and put the identifier name “lefttoright”.
  6. Iniside the keyframe, we use “from” keyword, which means first frame of our animation should be a keyframe and inside the block, we can specify the CSS property. And we also created “to” for last frame of our animation.
  7. Inside the “from” block, we set “left” to 0px because of the starting position is left. Inside the “to” block, we set “left” to 300px because of the ending position is right.
  8. Then we have to specify the animation to “object” style by using “animation” property to “lefttoRight”, which is animation-name and “2s”, which is animation-duration. And remove the “left” property in object style.
  9. See the Live preview below, the object square is moving left to right in the container.
READ ALSO  CSS Direct Child Selector with Example | Advanced Guide

Live preview

See the Pen
by Wonderdevelop (@wonderdevelop)
on CodePen.

Leave a Reply