There is an abundance of loading spinners out there for you to download or use, but how easy are they to implement, or even write yourself? This one is really easy, although you can build on it to make it more flashy.

This one has a two-tone colour, which spins one way, before slowing down and spinning the other, then repeat. See, nice and easy. All created with only HTML and CSS.

The loading spinner

First, you’ll need to actually create the spinner, which is easily done with the following code.

<div class="wrapper">
  <div id="outer"></div>
</div>

The wrapper is so that we can position it to fill the screen, and possibly put a tint over whatever is loading. Also, it makes it easier to centre align the spinner.

Setting the wrapper

The spinner that we are going to make is going to be 100px square. So for the wrapper to be centre aligned, we need it to be off by 50px.

.wrapper {
  margin: calc(50vh - 50px) auto 0;
}

Creating the spinner

Now we can concentrate on the spinner. First, we’ll create the circle.

#outer {
  width: 100px;
  height: 100px;
  border: 3px solid rgba(255,255,255, 0.4);
  border-left: 3px solid #fff;
}

Now we have the square, with three sides a dark grey colour, and one side white. Adding the border-radius will make this a circle, adding the margin will centre align the spinner.

#outer {
  margin: auto;
  width: 100px;
  height: 100px;
  box-sizing: border-box;
  border: 3px solid rgba(255,255,255, 0.4);
  border-left: 3px solid #fff;
  border-radius: 100%;
  animation: spin 4s infinite;
}

Creating the animation

We’ve referenced the animation keyframe in the previous section, so now we need to create the spin keyframe. One animation will spin it right 2 rotations clockwise, before spinning it back in the opposite direction. The call made in the #outer section above sets the speed and tells it to run infinitely.

@keyframes spin {
  0% {
    transform: rotatez(0deg);
  } 
  50% {
    transform: rotateZ(720deg);
  }
  100% {
    transform: rotatez(0deg);
  }
}

Demo

See the Pen Spinner by Dean Pugh (@wezlar) on CodePen.0

Conclusion

And there you have it! A simple spinner from which you can start to create anything. I am hopeful that in the future I can make it change shape and size. What can you come up with?

Tags: , , ,

Join the conversation...

Your email address will not be published. Required fields are marked *