Creating a Simple Loading Animation with CSS

Creating a Simple Loading Animation with CSS
Play this article

  1. First, you'll need to create a container element for the loading animation. This could be a div, a span, or any other HTML element. Inside this container element, you'll add a loading animation using the ::before or ::after pseudo-elements. For example:
.loading {
  position: relative;
  width: 100px;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;

.loading::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 3px solid #000;
  border-radius: 50%;
  border-top-color: transparent;
  animation: spin 1s linear infinite;
  1. Next, you'll need to create the animation itself using the @keyframes rule. This rule specifies the animation that will be applied to the pseudo-element. In this example, we're using the spin animation, which rotates the pseudo-element around its center point:
@keyframes spin {
  from {
    transform: rotate(0deg);
  to {
    transform: rotate(360deg);
  1. Now you can use the loading animation by adding the loading class to any element:
<div class="loading"></div>

And that's it! You now have a simple loading animation using CSS. You can customize the animation by changing the animation property, as well as the appearance of the pseudo-element using CSS properties like border, border-radius, and background-color.