Skeleton Screen Loading Animation using HTML & CSS
Tags: javascript, beginners, css, webdev
Published: 2021-10-05 16:36:09

In this article I have shown how Skeleton Screen Loading Animation is created using HTML CSS and JavaScript. I created this animation on a profile card. 

You will see various websites where you can see Skeleton Loading Animation before loading any element. Undoubtedly it enhances the quality and beauty of the website a lot. You need to have a basic idea about HTML and CSS to make it.

✅ **Watch Live Preview** 👉👉  [Skeleton Screen Loading Animation](https://www.foolishdeveloper.com/2021/09/skeleton-screen-loading-animation.html) 

I have shown this kind of effect before but it was not effective. In this case I have used JavaScript to make it work. 

This Skeleton Screen Loading Animation will continue for three seconds when you open the web page. After three seconds this effect is off and all the information on the profile card can be seen.
 
### Step 1: Design the web page

To create this animation, I first designed the webpage with CSS code. Here I have used light blue as the background color of the web page.
```css
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: #eff7f2;
  font-family: "Lato", sans-serif;
}
```
![Design the web page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0rb3fwz9lla2jnmczcx.jpg)

### Step 2: Basic structure of Skeleton Loading
 Now I have created the basic structure of this Skeleton Loading Animation or Profile Card. I have used the `width: 300px` and `height: 400px` of this profile card. A shadow has been used around the card to enhance the beauty.
```html
<div class="user-card skeleton">

</div>
```
```css
.user-card {
  width: 300px;
  height: 400px;
  box-shadow: -10px 5px 20px rgba(0, 0, 0, 0.3);
  border-radius: 15px;
  overflow: hidden;
  background: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  transition: box-shadow 0.2s ease-in;
  cursor: pointer;
}

.user-card:hover {
  box-shadow: -10px 10px 20px rgba(0, 0, 0, 0.4);
}
```
![Basic structure of Skeleton Loading](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiyr8facewnna20lubh9.jpg)

### Step 3: Add profile image and cover image
 Now I have added profile image and cover image in profile card. I have used the width and height of the profile image 100 px. I used `border-radius: 50%` to make it completely round. I have added a cover image with `height: 150px` and width: 100%.
```html
<div class="user-cover">
   <img class="user-avatar" src="img.jpg" alt="user profile image" />
</div>
```
```css
.user-card .user-cover {
  height: 150px;
  width: 100%;
  position: relative;
  background-image: url(img1.jpg);
  background-position: center;
  background-size: cover;
}

.user-card .user-cover .user-avatar {
  position: absolute;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  left: 0;
  right: 0;
  margin: auto;
  bottom: -35px;
  border: 2px solid #fff;
}
```
![Add profile image and cover image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dd44pjk2q0ato9o5je1.jpg)

### Step 4: Add basic information to the card
 Now I have added some basic information in this Skeleton Screen Loading Animation or Profile Card. I used margin-top: 35px to keep all the information from top to bottom.
```html
<div class="user-details">
   <div class="user-name hide-text">Anamika Roy</div>
   <div class="user-email hide-text">Web Designer</div>
   <div class="user-text hide-text">Lorem ipsum ... consectetur. </div>
</div>
```
```css
.user-card .user-details {
  text-align: center;
  margin-top: 35px;
  margin-bottom: 25px;
  width: 80%;
}

.user-card .user-details .user-name {
  margin-bottom: 10px;
  font-size: 24px;
  font-weight: 600;
}

.user-card .user-details .user-email {
  font-size: 14px;
  color: #0f5fc0;
  font-weight: 500;
}
.user-card .user-details .user-text{
  margin-top: 5px;
  font-size: 15px;
  color: #666;
}
```
![Add basic information to the card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7mjy7oln31ooup6ydxjm.jpg)

### Step 5: Create contact button
Now I have added a contact button in it. I have used the button `height: 35px`, `width 60%` and background color blue. I have used font-weight: bold to increase the size of the text.

 
```html
<button class="contact-user hide-text">CONTACT</button>
```
```css
.user-card .contact-user {
  margin-bottom: 15px;
  height: 35px;
  width: 80%;
  border: 0;
  color: white;
  font-weight: bold;
  background: #035f7d;
  letter-spacing: 0.5px;
  border-radius: 5px;
  cursor: pointer;
}
```
![Create contact button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0lant0cy7ebkgedh7pqz.jpg)

### Step 5: Add Loading Effect to images
 First I added animation between the profile image and the cover image. However, there is no animation in the image, only I have used gray color in the background. The way I designed the profile image and the cover image is the same way I designed it here.
```css
.user-card.skeleton .user-cover {
  background: #e2e2e2;
}

.user-card.skeleton .user-cover .user-avatar {
  display: none;
}

.user-card.skeleton .user-cover::after {
  content: "";
  position: absolute;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  left: 0;
  right: 0;
  margin: auto;
  bottom: -35px;
  border: 2px solid #fff;
  z-index: 10;
  background: #e2e2e2;
}
```
![Add Loading Effect to images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3ghj5th10vbuhd2ozo8.jpg)

### Step 6: Add Loading Effect to the information
Now I have added animation and color between the text and the button. First I added colors to the buttons and texts, then I added animations using `@keyframes`.

✅ **Related Post** 👉👉  [Transparent Login Form](https://www.foolishdeveloper.com/2021/09/transparent-login-form-with-html-and.html)

`animation-duration: 2s` used to help make this animation work every two seconds. Here animation means a black shadow will continue to travel from left to right every 2 seconds.
```css
.user-card.skeleton .hide-text {
  background: #e2e2e2;
  color: transparent;
  position: relative;
  overflow: hidden;
}

.user-card.skeleton .hide-text::before {
  content: "";
  position: absolute;
  left: 0%;
  top: 0;
  height: 100%;
  width: 50px;
  background: linear-gradient(to right, #e2e2e2 25%, #d5d5d5 50%, #e2e2e2 100%);
  animation-name: gradient-animation;
  animation-duration: 2s;
  animation-iteration-count: infinite;
  filter: blur(5px);
}
```


 Now I have implemented this animation using @keyframes.
```css
@keyframes gradient-animation {
  from {
    left: 0%;
  }
  to {
    left: 100%;
  }
}
```
![Add Loading Effect to the information](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjohmn92lreq0gw27iyb.jpg)

### Step 7: Turn off Skeleton Loading Effect using JavaScript
Now the most important thing is to turn off the animation using JavaScript. Above we have added Skeleton Screen Loading Animation using css. 

This animation will continue indefinitely. So now after some time using JavaScript you have to turn off that animation and instruct to view all the information in the profile card. I set the time to 3000 milliseconds using `setTimeout`. That means after `3000 milliseconds` that gray color animation will be hidden.

> If you want to use 4 seconds instead of these 3 seconds then you have to use 4000 instead of 3000 here.

```javascript
const $el = document.querySelector(".user-card");

// Loading finished
setTimeout(() => {
  $el.classList.remove("skeleton");
  $el
    .querySelectorAll(".hide-text")
    .forEach((el) => el.classList.remove("hide-text"));
}, 3000);
```
![Skeleton Screen Loading Animation using HTML CSS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7n9n2kkpkj9ublipoo5.jpg)

**Related Post:**
1. [Simple Footer HTML CSS](https://www.foolishdeveloper.com/2021/09/responsive-footer-design.html) 
2. [Make a Todo List using JavaScript](https://www.foolishdeveloper.com/2021/11/todo-list-javascript.html)
3. [Simple Stopwatch using JavaScript](https://www.foolishdeveloper.com/2021/10/simple-stopwatch-using-javascript.html)
4. [Javascript Age Calculator](https://www.foolishdeveloper.com/2021/07/javascript-age-calculator.html)
5. [Random Password Generator with JavaScript](https://www.foolishdeveloper.com/2021/07/random-password-generator-javascript.html)
6. [Automatic Image Slider in Html, CSS](https://www.foolishdeveloper.com/2021/07/automatic-image-slider.html) 
7. [Sidebar Menu Using HTML CSS](https://www.foolishdeveloper.com/2021/06/sidebar-menu-using-html-css-javascript.html) 

Hope you learned from this tutorial how to create **Skeleton Loading Animation using HTML CSS and JavaScript**. I have already shared many more such tutorials, you can see those designs.

You can visit my blog for more tutorials like this.
https://www.foolishdeveloper.com/