How to Create Animation on Page Load
There are many ways to add smooth and attractive animations to your webpage. CSS3 allows us to write behaviors for animations and transitions. In our snippet, we are particularly interested in those cases when we need to add animation on page load.
To create animation on page load, we just need to use @keyframes and some animation properties.
Let's start with HTML.
Create HTML
<header>
<a href="#">Books</a>
<a href="#">Quizzes</a>
<a href="#">Snippets</a>
<a href="#">Tools</a>
</header>
Add CSS
- Add @keyframes with the name "slideInLeft" and use 0% and 100% as a starting point and ending point of the animation, respectively. Then, specify the transform property for each of them.
- Use the animation property on the <header> and specify its background and padding.
- Style the <body> element by setting the margin to 0 and specifying the font-family.
- Specify the text-decoration, display, margin-right, and color properties for the <a> element.
@keyframes slideInLeft {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}
header {
animation: 1s ease-out 0s 1 slideInLeft;
background: #666;
padding: 40px;
}
body {
margin: 0;
font-family: "Segoe UI", Arial, Helvetica, Sans Serif;
}
a {
text-decoration: none;
display: inline-block;
margin-right: 10px;
color: #fff;
}
Now we can bring together the parts of our code and see the result.
Example of creating animation on page load with the animation property:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
@keyframes slideInLeft {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}
header {
animation: 1s ease-out 0s 1 slideInLeft;
background: #666;
padding: 40px;
}
body {
margin: 0;
font-family: "Segoe UI", Arial, Helvetica, Sans Serif;
}
a {
text-decoration: none;
display: inline-block;
margin-right: 10px;
color: #fff;
}
</style>
</head>
<body>
<header>
<a href="#">Books</a>
<a href="#">Quizzes</a>
<a href="#">Snippets</a>
<a href="#">Tools</a>
</header>
</body>
</html>
Next, we’ll demonstrate another version of this example where we use different animation-related properties instead of the animation shorthand property, as in the example above.
Example of creating animation on page load with some animation-related properties:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
@keyframes slideInLeft {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}
header {
animation-duration: 2s;
animation-timing-function: ease-in-out;
animation-delay: 0s;
animation-iteration-count: 2;
animation-name: slideInLeft;
background: #666;
padding: 40px;
}
body {
margin: 0;
font-family: "Segoe UI", Arial, Helvetica, Sans Serif;
}
a {
text-decoration: none;
display: inline-block;
margin-right: 10px;
color: #fff;
}
</style>
</head>
<body>
<header>
<a href="#">Books</a>
<a href="#">Quizzes</a>
<a href="#">Snippets</a>
<a href="#">Tools</a>
</header>
</body>
</html>
Let’s see the list of animation-related properties used in this example and explain their usage:
- animation-duration specifies the length of time of the animation.
- animation-timing-function specifies the behavior of the animation.
- animation-delay specifies the delay of the animation.
- animation-iteration-count specifies how many times the animation must be played.
- animation-name specifies the name of the animation.