w3resource

CSS Properties: How to play the animation two times?

Go to Exercise page

Solution:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" /><!-- Declare character encoding -->
<title>How to play the animation two times</title><!-- Set the title of the HTML document -->
</head>
<body>
<div class="animation">
<div class="w3r">
w3resource Tutorial
</div>
</div>
<style type="text/css"> /* CSS style start*/
.w3r {
color: white; /* Set text color */
float: left; /* Float the text to the left */
margin-right: 2%; /* Set right margin */
font-size: 20px; /* Set font size */
}
.animation {
background-color: #CC3333; /* Set background color */
height: 50px; /* Set height */
width: 100%; /* Set width */
overflow: hidden; /* Hide overflow content */
}
div {
width: 100px; /* Set width */
height: 100px; /* Set height */
background: #33FFFF; /* Set background color */
position: relative; /* Set position to relative */
-webkit-animation: move 5s infinite; /* Chrome, Safari, Opera */ /* Apply animation to Webkit browsers */
-webkit-animation-iteration-count: 5s; /* Chrome, Safari, Opera */ /* Set iteration count for Webkit browsers */
animation: move 5s infinite; /* Apply animation */
animation-iteration-count: 2; /* Set iteration count for animation */
}
/* Chrome, Safari, Opera */
@-webkit-keyframes move {
    from {top: 0px;} /* Define initial position */
    to {top: 200px;} /* Define final position */
}

@keyframes move {
    from {top: 0px;} /* Define initial position */
    to {top: 200px;} /* Define final position */
}
</style>
</body>
</html>

Explanation:

  • The HTML document contains a container div with a child div for animated text.
  • CSS is used to style the elements, setting text color, font size, background color, height, width, and overflow behavior.
  • An animation is applied to the child div, causing it to move vertically from the top to the bottom.
  • The animation is set to run infinitely (infinite) for 5 seconds and repeat twice (animation-iteration-count: 2;).

Live Demo:

See the Pen animation-iteration-count-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
Yes Yes Yes Yes No

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.