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:

.

Image of Stellar post

πŸš€ Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay