How to create: CSS loader

Learn how to use CSS to create a preloader.

Try it yourself

How to create a loader

Step 1 - Add HTML:

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

Step 2 - Add CSS:

.loader {
  border: 16px solid #f3f3f3; /* Light gray */
  border-top: 16px solid #3498db; /* Blue */
  border-radius: 50%;
  width: 120px;
  height: 120px;
  animation: spin 2s linear infinite;
}
@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

Try it yourself

Example explanation:

border property specifies the border size and color of the loader.border-radius property to transform the loader into a circle.

The blue part rotating inside the border is specified by border-top properties specify. If you want more 'spinners', you can also include border-bottom,border-left and/or border-right(see the example below).

The size of the loader is specified by width and height property specification.

Finally, we added an animation that makes the blue part rotate forever at a speed of 2 seconds.

Note:for unsupported animation and transform browser for properties, you should also include a -webkit- prefix. Click the example to see how to operate.

Add more spinners

Example

.loader {
  border-top: 16px solid blue;
  border-bottom: 16px solid blue;
}

Try it yourself

Example

.loader {
  border-top: 16px solid blue;
  border-right: 16px solid green;
  border-bottom: 16px solid red;
}

Try it yourself

Example

.loader {
  border-top: 16px solid blue;
  border-right: 16px solid green;
  border-bottom: 16px solid red;
  border-left: 16px solid pink;
}

Try it yourself

Another example

How to place the loader in the center of the page and display 'Page content' when the loading is complete:

Example

Try it yourself