burger menu icon
WillMaster

WillMasterBlog > CSS

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

'Loading' Icon Animation With CSS

Here are steps to make an animation to indicate content is loading:

  1. Make an image to rotate.
  2. Rotate it with CSS.
  3. Smile.

The code in this article was made to rotate an icon image, although the code can be used to rotate any image. In other words, you might have other uses for it.

The image you specify rotates continuously at a speed you specify.

The following demonstration rotates an icon 360° every 2.5 seconds.

Before I present the code, I want to mention that there are pure CSS loading animations. A search for "CSS loading code" should reveal a bunch. In general, no image is required for those, but they are limited to what CSS can do by itself. I like a bit more flexibility. Which is why I rotate an image.

Here is the code for the above demonstration.

<!-- The CSS. -->
<style type="text/css">
@keyframes rotation {
   from { transform:rotate(0deg); }
   to   { transform:rotate(359deg); }
}
.loading-image { animation:rotation 2.5s infinite linear; }
</style>

<!-- Place the image where you want the icon positioned when it is animated. -->
<div style="text-align:center;">
<img class="loading-image" src="https://willmaster.com/images/loadingicon.png" style="width:50px; height:50px;">
</div>

Implementing Icon Animation With CSS

As you can see, there are two parts in the above source code. There is (i) the CSS and (ii) the image. Go ahead and place the image where you want it to rotate, then do the rest of the implementation.

  1. The CSS class is named loading-image and is used in both the CSS and in the img tag. If the CSS class name is changed, both places in the code need to be changed.

  2. The 2.5s is within the value of the CSS loading-image definition. The value specifies the amount of time to elapse for each complete rotation. 2.5s means 2.5 seconds. Change the number as appropriate for your implementation.

The icon animation has been implemented. Try it in your browser.

Any image can be continuously rotated at any speed. Your imagination is likely to come up with additional ways to use the functionality.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

JavaScript-Formatted Date and Time

A JavaScript function to publish date/time according to your custom format.

Friendly Anchor Link

How to insert a bit of space above a normal anchor link scroll position.

Form Field Prefill With a Tap

When a link is tapped, a form field is filled in.

Beacon

Send a beacon to your script. Optionally, also send information.

Digging for an IP Address

While an app's IP address can always be found in the REMOTE_ADDR value, the value might not be the actual destination of the content retrieved from your website.

Simple CSS Rotate

Rotate divs, tables, and even characters. Specify the number of degrees or fraction of a turn, and you're good to go.

Avoid Expired SSL Certificate

Check the expiration date of your domain's SSL certificate

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2026 Will Bontrager Software LLC