Course Content
CSS Basic
0/1
CSS Selectors
0/1
CSS Comments
0/1
CSS Backgrounds
0/1
CSS Borders
0/1
CSS Outline
0/1
CSS Fonts
0/1
CSS Height and Width
0/1
CSS Margins and Paddings
0/2
CSS Icons
0/1
CSS Links
0/1
CSS Lists
0/1
CSS Tables
0/1
CSS Display Properties
0/1
CSS Max-Width Property
0/1
CSS Positioning Elements
0/1
CSS Z-Index Property
0/1
CSS Overflow
0/1
CSS Float
0/1
CSS Opacity
0/1
CSS Forms
0/1
CSS Dropdowns
0/1
CSS Buttons
0/1
CSS Media Queries
0/1
About Lesson

The animation shorthand property in CSS allows you to specify multiple animation-related properties in a single declaration. It includes properties such as animation name, duration, timing function, delay, iteration count, direction, fill mode, and play state. Here’s the syntax for the animation shorthand property:

Example:

CSS
animation: name duration timing-function delay iteration-count direction fill-mode play-state;

Now, let’s break down each property:

  • name: Specifies the name of the animation, defined using @keyframes.
  • duration: Specifies the duration of the animation.
  • timing-function: Specifies the timing function for the animation (e.g., ease, linear, ease-in, ease-out, ease-in-out).
  • delay: Specifies the delay before the animation starts.
  • iteration-count: Specifies the number of times the animation should repeat.
  • direction: Specifies whether the animation should play in reverse (reverse), alternate direction on each cycle (alternate), or both (alternate-reverse).
  • fill-mode: Specifies how the animation should apply styles before and after it is executed.
  • play-state: Specifies whether the animation is running (running) or paused (paused).
HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .out{
            height: 100px;
            width:100px;
            position: relative;
        }

        .in {
            position: absolute;
            width: 40px;
            height: 40px;
            background-color: black;
            position: relative;
            animation-name: example;
            animation-duration: 5s;
            animation-timing-function: linear;
            animation-delay: 2s;
            animation-iteration-count: infinite;
            animation-direction: alternate;
        }

        @keyframes example {
            0% {
                background-color: pink;
                left: 0px;
                top: 0px;
            }

            25% {
                background-color: yellow;
                left: 200px;
                top: 0px;
            }

            50% {
                background-color: red;
                left: 200px;
                top: 200px;
            }

            75% {
                background-color: orange;
                left: 0px;
                top: 200px;
            }

            100% {
                background-color: pink;
                left: 0px;
                top: 0px;
            }
        }
    </style>
</head>

<body>
<div class="out">
    <div class="in"></div>
</div>
    

</body>

</html>

In this example:

  • We have four div elements with the class .box, each with a different animation direction (move-right, move-left, move-up, move-down).
  • The animations are defined using @keyframes and specify different transformations (translation along X or Y axis).
  • Each div element has a different animation applied to it using the animation shorthand property, with different durations, timing functions, and iteration counts.
  • You can observe how each div moves in a different direction based on the animation applied to it.

Output: