- Tutorials / HTML & CSS
- Friday, 26th Aug, 2011
Home » Tutorials » HTML & CSS » CSS3: Spinner Animation
In this tutorial, we are going to create an animated spinner fully with CSS3. Yeah, no need to include JavaScript at all, what we need is just CSS3 already enough! Ok, let’s start our tutorial. First of all prepare a PNG image with transparent background, you can draw yourself or use the following image and save it as “spinner.png“:

Well, the HTML structure of the spinner is very simple and easy! What you need to do is just to write a <div> tag and assign an id called “spinner” to it as shown below:
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>CSS3: Spinner Animation | onlyWebPro.com</title> </head> <body> <div id="spinner"></div> </body> </html>
That’s all for the HTML structure. Let’s move on to the CSS section.
Let’s define the style of our spinner with the following css:
<style>
#spinner {
position: absolute;
top: 50%;
left: 50%;
height: 128px;
width: 128px;
-webkit-mask-image: url(spinner.png);
background-color: #000;
-webkit-animation-name: spinnerRotate;
-webkit-animation-duration: 2s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
}
</style>
As you can see above, we have define several properties that with prefix “-webkit“.
Cool, you have link the spinner PNG image to the div with id “#spinner“, but now we don’t see any animation on our page yet, let’s define the “spinnerRotate” that we have declares in “-webkit-animation-name”:
@-webkit-keyframes spinnerRotate {
from {
-webkit-transform:rotate(0deg);
}
to {
-webkit-transform:rotate(360deg);
}
}
As you can see from above, our spinner will start rotate at 0 degrees, and end at 360 degrees, over a time span of 2 seconds and the animation will keep repeating until user close the page.
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSS3: Spinner Animation | onlyWebPro.com</title>
<style>
#spinner {
position: absolute;
top: 50%;
left: 50%;
height: 128px;
width: 128px;
-webkit-mask-image: url(spinner.png);
background-color: #000;
-webkit-animation-name: spinnerRotate;
-webkit-animation-duration: 2s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
}
@-webkit-keyframes spinnerRotate {
from {
-webkit-transform:rotate(0deg);
}
to {
-webkit-transform:rotate(360deg);
}
}
</style>
</head>
<body>
<div id="spinner"></div>
</body>
</html>
That’s all, save your document and preview it on webkit browser (Safari 4.0, iOS Safari 2.0 or Chrome), you should see an animated spinner!
CSS3: Spinner Animation
Introduction To Device Orientation With HTML5
HTML5 Canvas For Absolute Beginners – Part 4