DEV Community

Cover image for Create A Loading Spinner with CSS Keyframes
Amy Oulton
Amy Oulton

Posted on

8 2

Create A Loading Spinner with CSS Keyframes

A great way to level up any application your building is to create a simple loading animation. Today, we're going to make use of free resources and pure CSS to create a loading animation of our own.

Of course, this is not integrated with anything. Integrating the spinner with actual loading time in the app is a whole other tutorial, but for now, we're going to worry about creating the actual spinner!

Spinner Gif

For this spinner I got this simple spinner of Pixabay (thanks gorkhs).

We start with just a very simple structure in our html.

<div class="spinner">
  <img src="./rainbow.png" class="img"/>
</div>
Enter fullscreen mode Exit fullscreen mode

Tip: Make sure you include the correct path and name to your image.

To create the animation, we will use keyframes and the transform property. Below, we'll create a keyframe animation called spin:

 @keyframes spin {
   0% {
     transform: rotate(0deg);
   }
   100% {
     transform: rotate(360deg);
   }
 }
Enter fullscreen mode Exit fullscreen mode

Now, let's style our actual spinner!

Stylish Alien

We created a div that contains the image in our HTML file, and assigned it the class of spinner. We will give it the following properties:

 .spinner {
   height: 100vh;
   display: flex;
   justify-content: center;
   align-items: center;
 }
Enter fullscreen mode Exit fullscreen mode

Now, for the actual image:

 .img {
   height: 150px;
   animation: spin 2.5s linear infinite;
 }
Enter fullscreen mode Exit fullscreen mode

Now we have a working spinner!

Want to see the full tutorial? Watch the YouTube video below or watch in on CodeCast to get access to all the code in the Player!

For more of my content, follow me on like Twitter & CodeCast!

You can also read my last blog here:

.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay