Make Animated Content Placeholders with HTML and CSS
Tags: css, html, webdev, animation
Published: 2021-02-06 22:00:42

---
title: Make Animated Content Placeholders with HTML and CSS
published: true
description: A brief tutorial on how to create animated content placeholders with HTML and CSS.
tags: css, html, webdev, animation
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/5zwhc7u2t8busilzj73a.png
canonical_url: https://jamesinkala.com/blog/make-animated-content-placeholders-with-html-and-css
---

In the same spirit of my previous article on [creating loading animations with web technologies](https://dev.to/xinnks/create-loading-animations-with-svg-and-css-1d0p), on this post I'll be demonstrating how one can create content placeholders for their web projects using HTML and CSS.

It's a good practice to let the user know that something is going on in the background such as the retrieval of data from the database by displaying an animation that will later be replaced by the loaded data than having nothing displayed which might give the impression that either the page is broken or has issues especially for users that have a weak connection.

In the wide range of animations that one can choose from within the latest web trends content placeholders seem to be the best of the pile as they display animations that carry a look closest possible to the content to be displayed. They can be positioned to hold the place of different types of data from text paragraphs to content cards as evident on major websites such as youtube, instagram and facebook.

#### On to creating our animated content placeholder.
The anatomy of the placeholder animation consists of two div blocks, the parent block that acts as the animation's wrapper with a darker background and a child block relatively positioned to the parent spanning the entire height of the parent with a lighter colored gradient background which will be animated infinitely, moving it from out of view from the left side of the parent past it to out of view on the right side of the parent.
below is a demonstration of the animated placeholder.
![animated placeholder block](https://dev-to-uploads.s3.amazonaws.com/i/mrvsmk2pl3l8fwocbfhy.gif)

Here is a preview of the anatomy of the placeholder animation:
![Anatomy of the placeholder animation](https://dev-to-uploads.s3.amazonaws.com/i/y19gyj60khodkzf4nvqc.gif)

The template of the placeholder animation is as follows.
```html
<div class="load-wraper">
    <div class="activity"></div>
</div>
```
And the stylesheet to animate the placeholder animation.
```css
.load-wraper{
    position: relative;
    height: 100%;
    width: 100%;
    background-color: rgb(211,211,211);
    z-index: 44;
    overflow: hidden;
    border-radius: 5px;
}
.activity{
    position: absolute;
    left: -45%;
    height: 100%;
    width: 45%;
    background-image: linear-gradient(to left, rgba(251,251,251, .05), rgba(251,251,251, .3), rgba(251,251,251, .6), rgba(251,251,251, .3), rgba(251,251,251, .05));
    background-image: -moz-linear-gradient(to left, rgba(251,251,251, .05), rgba(251,251,251, .3), rgba(251,251,251, .6), rgba(251,251,251, .3), rgba(251,251,251, .05));
    background-image: -webkit-linear-gradient(to left, rgba(251,251,251, .05), rgba(251,251,251, .3), rgba(251,251,251, .6), rgba(251,251,251, .3), rgba(251,251,251, .05));
    animation: loading 1s infinite;
    z-index: 45;
}

@keyframes loading {
    0%{
        left: -45%;
    }
    100%{
        left: 100%;
    }
}
```
To make the animation smoother make sure the child block (_.activity_) is as wide as possible, here it's width is 45% of the parent's total width.

At this point the animated placeholder block is complete and that's all on implementing it.

What's left is placing the placeholder within the wireframes of any content card layout blocks to see the animated placeholder in action. 

Let's demonstrate an example of this:
- A card component's animated placeholder.
![A card component's animated placeholder](https://dev-to-uploads.s3.amazonaws.com/i/bj22jajoiosw1vgk92uk.gif)

The HTML for the the above cards is as follows:
```html
<div class="card">
    <div class="card-image">
        <div class="load-wraper">
            <div class="activity"></div>
        </div>
    </div>
    <div class="card-content">
        <div class="card-text">
            <div class="load-wraper">
                <div class="activity"></div>
            </div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-image">
        <div class="load-wraper">
            <div class="activity"></div>
        </div>
    </div>
    <div class="card-content">
        <div class="card-avatar">
            <div class="load-wraper circular">
                <div class="activity"></div>
            </div>
        </div>
        <div class="card-avatar-text">
            <div class="load-wraper">
                <div class="activity"></div>
            </div>
        </div>
    </div>
</div>
```

Adding the stylesheet to the cards:
```css
.card{
    margin: 20px;
    height: 260px;
    width: 400px;
    position: relative;
    float: left;
}
.card-image{
    height: 200px;
    width: 400px;
    position: relative;
}
.card-content{
    margin-top: 20px;
    min-height: 40px;
    display: flex;
}
.card-avatar{
    width: 40px;
    float: left;
    flex: none;
}
.card-avatar-text{
    margin-left: 20px;
    float: left;
}
.card-content, .card-text, .card-avatar, .card-avatar-text{
    height: 40px;
    position: relative;
}
.card-content, .card-text, .card-avatar-text{
    width: 100%;
}
.circular{
    border-radius: 50%;
}
```

Go ahead and get creative with animated placeholders in your web projects.

&nbsp;
&nbsp;

<center><a href="https://www.buymeacoffee.com/Xinnks" target="_blank"><img src="https://res.cloudinary.com/djx5h4cjt/image/upload/c_scale,h_50/james-sinkala/bmc-button.png" alt="Buy Me A Coffee" style="height: 50px !important;width: auto !important;" ></a></center>