19 Oct 2024

Using HTML and CSS to create a custom animated loader

When it comes to creating a website, loading times can make or break a user's experience. It's frustrating to wait for a page to load, and it can lead to visitors leaving your site altogether. That's why it's important to have a loading animation that engages users and keeps them entertained while your page loads. In this blog post, we'll show you how to use HTML and CSS to create a custom animated loader.

Step 1: Create the HTML Markup

First, let's create the HTML markup for our loader. We'll use a div element with the class name "loader" and add four child div elements inside it. Each child div will have the class name "bar", and we'll use CSS to animate these bars later on.

<div class="loader">
  <div class="bar"></div>
  <div class="bar"></div>
  <div class="bar"></div>
  <div class="bar"></div>
</div>

Step 2: Style the Loader

Next, we'll style the loader using CSS. We'll set the height and width of the loader to 50 pixels, and set its position to absolute so that it appears in the center of the screen. We'll also set the background color of the loader to a light grey color.

.loader {
  height: 50px;
  width: 50px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #f1f1f1;
}

Step 3: Animate the Bars

Now it's time to animate the bars. We'll use CSS animations to achieve this effect. First, we'll set the height of each bar to 10 pixels, and the width to 4 pixels. We'll then position each bar using absolute positioning, and set the left property of each bar to a percentage value based on its index. This will ensure that each bar is evenly spaced apart.

.bar {
  position: absolute;
  height: 10px;
  width: 4px;
  background-color: #333;
}

.bar:nth-child(1) {
  left: 25%;
}

.bar:nth-child(2) {
  left: 37.5%;
}

.bar:nth-child(3) {
  left: 50%;
}

.bar:nth-child(4) {
  left: 62.5%;
}

Step 4: Create the Animation

To create the animation, we'll use the CSS transform property to scale each bar vertically. We'll start with a scale value of 1, and then set it to 0.3 at the midpoint of the animation. This will create a "squish" effect as each bar compresses vertically. We'll then set the scale value back to 1 at the end of the animation.

@keyframes loader {
  0% {
    transform: scaleY(1);
  }
  50% {
    transform: scaleY(0.3);
  }
  100% {
    transform: scaleY(1);
  }
}

Step 5: Test the Loader

Now that we've created our custom animated loader, it's time to test it out. Simply add the HTML markup and CSS styles to your webpage, and the loader should appear when the page is loading. You can also customize the colors and dimensions of the loader to fit your website's design.

Here's the complete HTML and CSS code for our custom animated loader:

<div class="loader">
  <div class="bar"></div>
  <div class="bar"></div>
  <div class="bar"></div>
  <div class="bar"></div>
</div>

<style>
.loader {
  height: 50px;
  width: 50px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #f1f1f1;
}

.bar {
  position: absolute;
  height: 10px;
  width: 4px;
  background-color: #333;
  animation-name: loader;
  animation-duration: 1s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}

.bar:nth-child(1) {
  left: 25%;
  animation-delay: 0s;
}

.bar:nth-child(2) {
  left: 37.5%;
  animation-delay: 0.25s;
}

.bar:nth-child(3) {
  left: 50%;
  animation-delay: 0.5s;
}

.bar:nth-child(4) {
  left: 62.5%;
  animation-delay: 0.75s;
}

@keyframes loader {
  0% {
    transform: scaleY(1);
  }
  50% {
    transform: scaleY(0.3);
  }
  100% {
    transform: scaleY(1);
  }
}
</style>

In conclusion, creating a custom animated loader is a great way to enhance your website's user experience. By using HTML and CSS, you can create a loader that is unique and engaging. You can also customize the colors and dimensions of the loader to match your website's design. So, try out this tutorial and create your own custom loader today!

You may also like

Designing a custom animated banner with HTML, CSS, and JavaScript

Design a custom animated banner with HTML, CSS, and JavaScript: foll...

Continue reading

Using HTML and CSS to create a custom tooltip

This blog discusses how to create a custom tooltip using HTML and CS...

Continue reading

Using HTML and CSS to create a custom scrollbar

This blog post provides a step-by-step guide on how to create a cust...

Continue reading