create a skeleton loading animation with CSS

How to create a skeleton loading animation with CSS

You can create a skeleton loading animation with CSS. Everyone knows that having a slow website is one of the worst things if you are a developer or browsing a lot. sometimes you just have a lot of data to load and it’s impossible to make your website any faster, which is why many large companies use loading animations.

It helps to make their site appear faster, and in this tutorial, I’ll show you exactly how to create a skeleton loading animation with CSS.

This will just give your site a lot more responsive experience to the users.
It will also appear to be quite professional, and best of all, it will be very simple to apply.

Create an HTML page, for example, the following code is a simple sample for you.

<!-- create a skeleton loading animation with CSS -->
  <div class="grid"></div>
  <template id="card-template">
    <div class="card">
      <div class="header">
        <img class="header-img skeleton" src="" />
        <div class="title" data-title>
          <div class="skeleton skeleton-text"></div>
          <div class="skeleton skeleton-text"></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>

We only have one card with a title, a description, and an actual image. This is something we can start with for our skeleton loading, and the first thing I want to do with skeleton loading is get our image skeleton loaded, which is really simple.
We’ll add a skeleton class to our image, and the nice thing is that almost always when working with images, you’ll probably set a specific width and height to that image somewhere in your CSS, which is perfect because it’s already defining the boundaries for where our skeleton loading animation will go.

Now we will add some JavaScript code here.

// create a skeleton loading animation with CSS
  const grid = document.querySelector('.grid')
  const cardTemplate = document.getElementById('card-template')
  for (let i = 0; i < 10; i++) {
    .then(res => res.json())
    .then(posts => {
      grid.innerHTML = ''
      posts.forEach(post => {
        const div = cardTemplate.content.cloneNode(true)
        div.querySelector('[data-title]').textContent = post.title
        div.querySelector('[data-body]').textContent = post.body

Finally, we will add some CSS code to it which will handle the styling of the page.

/*. create a skeleton loading animation with CSS */
.skeleton {
  opacity: .7;
  animation: skeleton-loading 1s linear infinite alternate;
.skeleton-text {
  width: 100%;
  height: .5rem;
  margin-bottom: .25rem;
  border-radius: .125rem;
.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%);
.grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  padding: 1rem;
.card {
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 6px -1px, rgba(0, 0, 0, 0.06) 0px 2px 4px -1px;
  padding: 16px;
  border-radius: 4px;
.header {
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
.header-img {
  width: 50px;
  height: 50px;
  object-fit: cover;
  border-radius: 100%;
  margin-right: 1rem;
  flex-shrink: 0;
.title {
  font-weight: bold;
  font-size: 1.25rem;
  text-transform: capitalize;
  word-wrap: none;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  flex-grow: 1;
/*. */

How does this skeleton loading in CSS work?

create a skeleton loading animation with CSS

If you look at the example, you’ll notice that this skeleton loading animation has the circle where our images are, and all we’re doing is taking a grey-colored background and pulling it in and out with a CSS animation.

It’s actually really simple to do, so I’m just going to give us some room at the top here where we can select that skeleton class, and this skeleton class will give us that grey background with the animation.

We’re going to call this animation skeleton loading, and it’ll be a one-second animation that follows a straight path. This means it’s only going to get worse.

This means it will just transition linearly between the two values we provide, looping indefinitely, thus we’ll use the term endless here.

which simply means that the animation’s transition will go from the beginning to the end, and then from the end all the way back to the beginning, so it won’t jump from the end to the beginning, and then we can define some keyframes for that animation, so we’ll just call it animation here, and at zero percent, we want to start with a greyish background color.

In our case, we’re going to use HSL with a value of 200 to give it a little bit of a blue hue, 20 for saturation to make it look greyish, and 70 for lightness because we want it to be a fairly light grey color. Then we’ll just copy that down for our 100 animations and change the lightness to 95 instead of 70 because that’s what we want.

How to create a beautiful scroll indicator design in CSS?

If we save that and come back over here, we’ll just make sure that we change this image here to a div so that we can see what it looks like without the image inside of it, so we’ll just give it a div with these exact same classes and save it, you can see that it doesn’t really look like anything’s going on right now.

AdBlocker Message

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.