# Enhance the Way a Web Page Loads With CSS Animations

In a previous tutorial we created a full-screen responsive page with flexbox. Today, we’ll make this page more attractive by adding a few CSS animations to it.

Here's what we're going to build:

## 1. The HTML

In order to add the desired animations, we’ll slightly update the initial page markup. In the previous tutorial, the markup of our main element looked like this:

 1 
 2 
 3 

...

 4 

...

 5 

...

 6 
 7 


For the purposes of this tutorial, we’ll add some extra wrappers, changing it to this:

 1 
 2 
 3 
 4 

...

 5 
 6 
 7 

...

 8 
 9 
 10 

...

 11 
 12 
 13 


Below is the overall page markup after this modification:

 1 
 2   9 
 10 
 11 
 12 

...

 13 
 14 
 15 

...

 16 
 17 
 18 

...

 19 
 20 
 21 
 22 
 23  ...  24 
...
 25 
 26 


## 2. Adding Initial CSS Styles

With the HTML in place, let’s focus our attention on the important stuff: the CSS.

To begin with we’ll do four things:

1. Hide all the page elements.
2. Set styles for the header and footer elements and their sub-elements.
3. Define styles for the ::after pseudo-element of the header
4. Specify styles for the text wrappers and their sub-elements inside the main.

Here are the associated styles:

 1 /* variables */  2 :root {  3  --main-purple-color: #9e89b8;  4 }  5 6 .page-header nav > *,  7 .page-main,  8 .page-footer > * {  9  opacity: 0;  10 }  11 12 .page-header,  13 .page-footer,  14 .page-main .text-wrapper {  15  overflow: hidden;  16 }  17 18 .page-header nav > * {  19  transform: translateY(-30px);  20 }  21 22 .page-footer > * {  23  transform: translateY(30px);  24 }  25 26 .page-header {  27  position: relative;  28 }  29 30 .page-header::after {  31  content: '';  32  position: absolute;  33  bottom: 0;  34  left: 0;  35  width: 100%;  36  height: 2px;  37  background: var(--main-purple-color);  38  z-index: 99999;  39  transform: scaleX(0);  40 }  41 42 .page-main .text-wrapper + .text-wrapper {  43  margin-top: 10px;  44 }  45 46 .page-main .text-wrapper * {  47  transform: translateY(120%);  48 }  49 50 .page-main .p-wrapper * {  51  transform: translateY(-120%);  52 } 

## 3. Firing CSS Animations

With the HTML and basic CSS ready, we can now concentrate on the page animations.

However, before doing that, let’s make sure we understand when they should run: ideally, as soon as the page is fully loaded. This ensures that all page assets will be ready, sitting in the correct position, and we won’t lose any of the animation effects.

With that in mind, we first wait for the page to load, and when that happens we use JavaScript to add a loaded class to the body.

Here’s the related JavaScript code:

 1 window.addEventListener("load", () => {  2  document.querySelector("body").classList.add("loaded");  3 }); 

Any animations we apply will be done to descendants of the loaded class.

So, our first scale-in animation targets the ::after pseudo-element of the header:

 1 .loaded .page-header::after {  2  animation: scaleIn 1.3s ease-in 0.2s forwards;  3 }  4 5 @keyframes scaleIn {  6  100% {  7  transform: scaleX(1);  8  }  9 } 

Tip: By default the transform origin of an element is its center, which is why the animation of the pseudo-element starts from its center. If you want the animation to start from another position, just change the transform-origin property value of the target element.

Below you can see how our animation changes where we apply a different transform-origin:

Next, we use a fade-in effect to display the page elements. In addition to this animation, we also use a slide-in animation to show the header and footer contents:

 1 .loaded .page-main {  2  animation: fadeIn 1s ease-in 0.7s forwards;  3 }  4 5 .loaded .page-header nav > *,  6 .loaded .page-footer > * {  7  animation: fadeIn 1s ease-in 0.7s forwards, slideIn 0.8s ease-in 0.9s forwards;  8 }  9 10 @keyframes fadeIn {  11  60% {  12  opacity: 0;  13  }  14   15  100% {  16  opacity: 1;  17  }  18 }  19 20 @keyframes slideIn {  21  100% {  22  transform: translateY(0);  23  }  24 } 

Finally, the elements of the main become visible through a slide-in effect:

 1 .loaded .page-main .text-wrapper * {  2  animation: slideIn 0.6s cubic-bezier(0.54, 0.46, 0.54, 0.94) 2s forwards;  3 }  4 5 @keyframes slideIn {  6  100% {  7  transform: translateY(0);  8  }  9 } 

Now let’s look again at what we’ve just built:

## Conclusion

In this quick tutorial, we managed to animate an HTML flexbox page by taking advantage of CSS animations.

A few notes before closing:

• Instead of CSS animations, we could equally have used CSS transitions (almost all our animations go from a beginning state to an end state). The fade-in animation is the most complicated one because it includes three animation steps (0%, 60%, 100%).
• Instead of 2d transforms, we could equally have used 3d transforms.
• Play with the animation-duration and animation-delay property values to modify the animations according to your needs.
• On mobile screens you might want to limit the number of animations that occur (e.g. remove footer animations). The demo animations work well especially on screens where all page contents are visible.