Create Skeleton Loaders with HTML and CSS

How to Create Skeleton Loaders in CSS, display new and trending loaders in your web page.

🙋‍♂️ Shubham Verma    🗓 February 14, 2022


Create Skeleton Loaders with HTML and CSS


Create Skeleton Loaders with HTML and CSS

In now days, there are some trending loaders which is called skelton loaders are displaying on popular web pages like facebook, youtube, twitter etc. Displaying this skelton loaders can improve the load time scenarios and make UI more intuitive and also improve look and feel of your web pages. In this article we will learn about the skelton loaders and also implement some interesting skelton loaders.
In this article, we'll implement some basic skelton loaders first using onlt HTML and CSS.

Let's get started.

Create basic skelton loader

Let's create a very basic design and create it's skelton loader. You can see the below gif, let create this simple skelton loader using HTML and CSS only.

Create Skeleton Loaders with HTML and CSS
simple_skelton.html
                            <html lang="en">
                              <head>
                                  <title>Skelton IDKBLOGS</title>
                              </head>
                              <style>
                                  .card-template{
                                    width: 100%;
                                    margin: 100px 100px;
                              
                                  }
                                  .header-img{
                                    width: 200px;
                                    height: 200px;
                                    border: 1px solid lightgray;
                                  }
                              
                                  .skeleton {
                                    opacity: .9;
                                    animation: skeleton-loading .8s linear infinite alternate;
                                  }
                              
                                  @keyframes skeleton-loading {
                                  0% {
                                  background-color: hsl(200, 20%, 70%);
                                  }
                              
                                  100% {
                                  background-color: hsl(200, 20%, 95%);
                                  }
                                  }
                              </style>
                              <body>
                                  <div class="card-template">
                                      <img class="header-img skeleton" src="https://source.unsplash.com/200x200/?electronics" />
                                  </div>
                              </body>
                              </html>
                          

Let's have look on the animation

You can see, we have used css to make the skelton animation, it is very simple. here are the CSS that is responsible to make it successful skelton.
                                  .skeleton {
                                    opacity: .9;
                                    animation: skeleton-loading .8s linear infinite alternate;
                                  }
                              
                                  @keyframes skeleton-loading {
                                    0% {
                                        background-color: hsl(200, 20%, 70%);
                                    }
                              
                                    100% {
                                        background-color: hsl(200, 20%, 95%);
                                    }
                                  }
                          

Let's create another skelton with more blocks

Now this time we'll implement some more blocks with its skelton loader as you can see in the below gif: Create Skeleton Loaders with HTML and CSS

Create Skeleton Loaders with HTML and CSS

multiple_skelton.html
                          <html lang="en">
                          <head>
                              <title>Skelton IDKBLOGS</title>
                          </head>
                    
                          <style>
                              .card-template {
                                  width: 100%;
                                  margin: 100px 100px;
                              }
                    
                              .header-img {
                                  width: 200px;
                                  height: 200px;
                                  border: 1px solid lightgray;
                              }
                    
                              .skeleton {
                                  opacity: .9;
                                  animation: skeleton-loading .8s linear infinite alternate;
                              }
                    
                              @keyframes skeleton-loading {
                                  0% {
                                      background-color: hsl(200, 20%, 70%);
                                  }
                    
                                  100% {
                                      background-color: hsl(200, 20%, 95%);
                                  }
                              }
                          </style>
                          <body>
                              <div class="card-template">
                                  <img class="header-img skeleton" src="https://source.unsplash.com/200x200/?electronics" />
                                  <img class="header-img skeleton" src="https://source.unsplash.com/200x200/?book" />
                                  <img class="header-img skeleton" src="https://source.unsplash.com/200x200/?bike" />
                              </div>
                          </body>
                          </html>
                        

Do we need to create a copy of structure?

Now, there would be a question in your mind that do we need to write the codes to create a copy of original structure for the skelton.
The answer is NO. instead we will dynamically create the block by using below codes.
We'll use the below codes in the final skelton loading.
                            const grid = document.querySelector('.grid');
                            const cardTemplate = document.getElementById('card-template');
                            for (let i = 0; i < 6; i++) {
                              grid.append(cardTemplate.content.cloneNode(true));
                            }
                          

The above code will create multiple skelton blocks and will display the skelton loader for all the blocks.

Create final skelton loader

We have created multiple loader, now its time to create a little complex and complete skelton loader for our original blocks.
This is our original blocks, we will create skelton loader for below blocks. Create Skeleton Loaders with HTML and CSS

Create Skeleton Loaders with HTML and CSS

Write the code to create best skelton loader

original.html
                            <!DOCTYPE html>
                            <html lang="en">
                    
                            <head>
                              <title>Document</title>
                            </head>
                    
                            <style>
                              .card {
                                width: 500px;
                                margin: 0 auto;
                                margin-top: 50px;
                              }
                    
                              .title {
                                height: 20px;
                                width: 300px;
                              }
                    
                              .header-img {
                                width: 100px;
                                height: 100px;
                              }
                    
                    
                              .skeleton {
                                opacity: .7;
                                animation: skeleton-loading 1s linear infinite alternate;
                              }
                    
                              .skeleton-text {
                                width: 100%;
                                height: .5rem;
                                margin-bottom: .25rem;
                                border-radius: .125rem;
                                height: 15px;
                              }
                    
                              .skeleton-text:last-child {
                                margin-bottom: 0;
                                width: 80%;
                              }
                    
                              @keyframes skeleton-loading {
                                0% {
                                  background-color: hsl(200, 20%, 70%);
                                }
                    
                                100% {
                                  background-color: hsl(200, 20%, 95%);
                                }
                              }
                            </style>
                    
                            <body>
                              <div class="grid"></div>
                    
                              <template id="card-template">
                                <div class="card">
                                  <div class="header">
                                    <img class="header-img skeleton" data-img src="https://source.unsplash.com/100x100/?electronics" />
                                    <div class="title" data-title>
                                      <div class="skeleton skeleton-text"></div>
                                      <div class="skeleton skeleton-text"></div>
                                    </div>
                                  </div>
                                  <div data-body>
                                    <div class="skeleton skeleton-text"></div>
                                    <div class="skeleton skeleton-text"></div>
                                    <div class="skeleton skeleton-text"></div>
                                    <div class="skeleton skeleton-text"></div>
                                  </div>
                                </div>
                              </template>
                    
                            </body>
                            <script>
                              const grid = document.querySelector('.grid');
                              const cardTemplate = document.getElementById('card-template');
                              for (let i = 0; i < 6; i++) {
                                grid.append(cardTemplate.content.cloneNode(true));
                              }
                              setTimeout(() => {
                                fetch("https://fakestoreapi.com/products/category/electronics")
                                  .then(res => res.json())
                                  .then(posts => {
                                    grid.innerHTML = ''
                                    posts.forEach(post => {
                                      const div = cardTemplate.content.cloneNode(true)
                                      div.querySelector('[data-img]').src = post.image
                                      div.querySelector('[data-title]').textContent = "INR. ₹ " + post.price
                                      div.querySelector('[data-body]').textContent = post.title
                                      grid.append(div)
                                    })
                                  })
                              }, 3000)
                    
                            </script>
                            </html>
                      

Output: Create Skeleton Loaders with HTML and CSS

Create Skeleton Loaders with HTML and CSS


Conclusion:

In this article, we have learned about Skeleton Loaders also we have seen how we can create a simple Skeleton Loaders. Also we saw about the animation that is responsible to create skelton loaders, then we have created skelton loader for multiple blocks and finally we have created skelton loader with API call. Now we are able to create any skelton loader for any type of blocks using HTML and CSS.



Support our IDKBlogs team

Creating quality content takes time and resources, and we are committed to providing value to our readers. If you find my articles helpful or informative, please consider supporting us financially.

Any amount (10, 20, 50, 100, ....), no matter how small, will help us continue to produce high-quality content.

Thank you for your support!




Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.