Open In App

How to pause/play animation using CSS ?

CSS helps to animate HTML elements without using JavaScript. You can play and pause the animation applied to HTML elements using animation-play-state property of CSS.

The animation-play-state property has 2 values:

Follow these steps :

Example 1: HTML and CSS code to play the animation.






<!DOCTYPE html>
<html>
  
<head>
    <style>
        div {
            position: relative;
            animation: geeks 5s infinite;
            animation-play-state: paused;
        }
          
        div:hover {
            animation-play-state: running;
        }
          
        @keyframes geeks {
            from {
                left: 0px;
            }
            to {
                left: 100px;
            }
        }
    </style>
</head>
  
<body>
    <p>
        Hover over the GeeksforGeeks 
        to run the animation.
    </p>
  
    <div>GeeksforGeeks</div>
</body>
  
</html>

Output:

Play animation when hovered

Example 2: HTML and CSS code to pause the animation.






<!DOCTYPE html>
<html>
  
<head>
    <style>
        div {
            position: relative;
            animation: geeks 5s infinite;
        }
          
        div:hover {
            animation-play-state: paused;
        }
          
        @keyframes geeks {
            from {
                left: 0px;
            }
            to {
                left: 100px;
            }
        }
    </style>
</head>
  
<body>
    <p>
        Hover over the GeeksforGeeks 
        to stop the animation.
    </p>
  
    <div>GeeksforGeeks</div>
</body>
  
</html>

Output:

Pause animation when hovered


Article Tags :