Advertisement
  1. Web Design
  2. HTML/CSS
  3. CSS

Creating a Collection of CSS3 Animated Pre-loaders

Scroll to top

Pre-loaders are a common sight in modern web design. As users we expect the web to be fast and fluid - we don’t like waiting for things. Pre-loaders offer visual feedback in the event of content being loaded, thereby managing expectations and reducing the chance of a user abandoning your website.

CSS3 Essentials for Creating Pre-loaders

Before we dive into building our collection of CSS3 pre-loaders, first I’ll be discussing some of the attributes of CSS3 which are essential for creating these type of pre-loaders.

Pseudo Elements :before :after

Pseudo elements are really useful in helping creating CSS3 pre-loaders. Pseudo elements essentially create a fake element before or after the HTML element in question.

“Pseudo is derived from the Greek word pseudēs which means false.” 

This is similar to creating an extra element that doesn't really exist however pseudo elements can be styled using CSS. These pseudo elements aren't a necessity for creating CSS3 pre-loaders but they do come in handy and allow us to use minimal markup. 

Pseudo elements can be styled exactly the same way as you would any other HTML element the only difference being that you must specify a content property. Without specifying this the pseudo element will not render. The content property can contain any text which may be useful if your pre-loader needs to contain words such as “Loading...” however if you don’t need any text content then you can just leave the content property blank.

CSS3 Animation

CSS pseudo elements are useful but not essential for creating CSS3 pre-loaders, but the animation property is. Without this the pre-loader would fail to animate and would just be a static visual - not very useful to indicate that the content is loading.

“The main component of CSS animations is @keyframes, the CSS rule where animation is created. Think of @keyframes as being stages along a timeline. Inside@keyframes, you can define these stages, each having a different style declaration.” - A beginners introduction to CSS animation

Note: before we jump into the demo's it might be worth noting that vendor prefixes are not included in the code snippets below. If you need the vendor prefixes then please see the codepen examples.

1. Audio Wave

The idea behind this pre-loader is to create an animation similar to an audio wave. 

HTML

This is achieved by creating five spans, each one representing an audio bar.

1
<div id="preloader_1">
2
      		<span></span>
3
  			<span></span>
4
  			<span></span>
5
  			<span></span>
6
  			<span></span>
7
  		</div>

CSS

The effect is achieved by animating the height of each span from 5px to 30px. The span also moves down on the Y axis by 15px to give the effect that it is growing from the centre.

1
#preloader_1{
2
    position:relative;
3
}
4
#preloader_1 span{
5
	display:block;
6
	bottom:0px;
7
	width: 9px;
8
	height: 5px;
9
	background:#9b59b6;
10
	position:absolute;
11
    animation: preloader_1 1.5s	 infinite ease-in-out;
12
}
13
14
#preloader_1 span:nth-child(2){
15
left:11px;
16
animation-delay: .2s;
17
18
}
19
#preloader_1 span:nth-child(3){
20
left:22px;
21
animation-delay: .4s;
22
}
23
#preloader_1 span:nth-child(4){
24
left:33px;
25
animation-delay: .6s;
26
}
27
#preloader_1 span:nth-child(5){
28
left:44px;
29
animation-delay: .8s;
30
}
31
@keyframes preloader_1 {
32
    0% {height:5px;transform:translateY(0px);background:#9b59b6;}
33
    25% {height:30px;transform:translateY(15px);background:#3498db;}
34
    50% {height:5px;transform:translateY(0px);background:#9b59b6;}
35
    100% {height:5px;transform:translateY(0px);background:#9b59b6;}
36
}

By default the animation on each span happens at the same time. The Mexican wave effect is created by adding animation-delay  to each span with an offset of 2 milliseconds. Each span is targeted using the nth-child() selector. 

2. Circular Square

This pre-loader uses four squares which shift, rotate, change color and become circles.

HTML

It's created by using four spans. Each one is a circle/square and has its own animation applied to it.

1
<div id="preloader_2">
2
    		<span></span>
3
			<span></span>
4
			<span></span>
5
			<span></span>
6
		</div>

CSS

All four of them transform from a square into a circle by adjusting the border-radius  from 0px (square) to 20px (circle). 

1
#preloader_2{
2
position: relative;
3
left: 50%;
4
width: 40px;
5
height: 40px;
6
}
7
#preloader_2 span{
8
    display:block;
9
	bottom:0px;
10
	width: 20px;
11
	height: 20px;
12
	background:#9b59b6;
13
	position:absolute;
14
}
15
#preloader_2 span:nth-child(1){
16
animation: preloader_2_1 1.5s infinite ease-in-out;
17
}
18
#preloader_2 span:nth-child(2){
19
left:20px;
20
animation: preloader_2_2 1.5s infinite ease-in-out;
21
22
}
23
#preloader_2 span:nth-child(3){
24
top:0px;
25
animation: preloader_2_3 1.5s infinite ease-in-out;
26
}
27
#preloader_2 span:nth-child(4){
28
top:0px;
29
left:20px;
30
animation: preloader_2_4 1.5s infinite ease-in-out;
31
}
32
33
@-keyframes preloader_2_1 {
34
    0% {-transform: translateX(0px) translateY(0px)  rotate(0deg); border-radius:0px;}
35
    50% {-transform: translateX(-20px) translateY(-10px) rotate(-180deg); border-radius:20px;background:#3498db;}
36
    80% {-transform: translateX(0px) translateY(0px) rotate(-360deg); border-radius:0px;}
37
     100% {-transform: translateX(0px) translateY(0px) rotate(-360deg); border-radius:0px;}
38
}
39
@-keyframes preloader_2_2 {
40
    0% {-transform: translateX(0px) translateY(0px)  rotate(0deg);border-radius:0px;}
41
    50% {-transform: translateX(20px) translateY(-10px) rotate(180deg);border-radius:20px;background:#f1c40f;}
42
    80% {-transform: translateX(0px) translateY(0px) rotate(360deg);border-radius:0px;}
43
    100% {-transform: translateX(0px) translateY(0px) rotate(360deg);border-radius:0px;}
44
}
45
@-keyframes preloader_2_3 {
46
    0% {-transform: translateX(0px) translateY(0px)  rotate(0deg);border-radius:0px;}
47
    50% {-transform: translateX(-20px) translateY(10px) rotate(-180deg); border-radius:20px;background:#2ecc71;}
48
    80% {-transform: translateX(0px) translateY(0px) rotate(-360deg);border-radius:0px;}
49
     100% {-transform: translateX(0px) translateY(0px) rotate(-360deg); border-radius:0px;}
50
}
51
52
53
@-keyframes preloader_2_4 {
54
    0% {-transform: translateX(0px) translateY(0px)  rotate(0deg); border-radius:0px;}
55
    50% {-transform: translateX(20px) translateY(10px) rotate(180deg); border-radius:20px;background:#e74c3c;}
56
    80% {-transform: translateX(0px) translateY(0px) rotate(360deg); border-radius:0px;}
57
     100% {-transform: translateX(0px) translateY(0px) rotate(360deg);border-radius:0px;}
58
}

Each one also rotates and moves along the X & Y axis in opposite direction to its current position. The color of each span is also animated from a uniform purple to its own independent color. This gives the impression of the shapes merging from several circles into one square.

3. Crossing Shapes

The Crossing Shapes pre-loader is a single div that utilises the :before  and :after  pseudo elements that we talked about previously. 

HTML

1
<div id="preloader_3"></div>

CSS

1
#preloader_3{
2
    position:relative;
3
}
4
#preloader_3:before{
5
	width:20px;
6
	height:20px;
7
	border-radius:20px;
8
	background:blue;
9
	content:'';
10
	position:absolute;
11
	background:#9b59b6;
12
	animation: preloader_3_before 1.5s infinite ease-in-out;
13
}
14
15
#preloader_3:after{
16
	width:20px;
17
	height:20px;
18
	border-radius:20px;
19
	background:blue;
20
	content:'';
21
	position:absolute;
22
	background:#2ecc71;
23
	left:22px;
24
	animation: preloader_3_after 1.5s infinite ease-in-out;
25
}
26
27
@keyframes preloader_3_before {
28
    0% {transform: translateX(0px) rotate(0deg)}
29
    50% {transform: translateX(50px) scale(1.2) rotate(260deg); background:#2ecc71;border-radius:0px;}
30
      100% {transform: translateX(0px) rotate(0deg)}
31
}
32
@keyframes preloader_3_after {
33
    0% {transform: translateX(0px)}
34
    50% {transform: translateX(-50px) scale(1.2) rotate(-260deg);background:#9b59b6;border-radius:0px;}
35
  	100% {transform: translateX(0px)}
36
}

One animation is placed on #preloader_3:before and another on #preloader_3:after. Each animation changes into a different color at the opposite time. Similarly to the previous pre-loader each pseudo element changes from a circle to a square by animating the border-radius property.

4. The Snake

The snake is made up of a collection of spans each one being styled to form a circle. 

HTML

Here's the markup, but you could always try creating the snake pre-loader with three circles and rather than having several spans just use #preloader_4  div with :before and :after

1
<div id="preloader_4">
2
    		<span></span>
3
			<span></span>
4
			<span></span>
5
			<span></span>
6
			<span></span>
7
		</div>

CSS

The animation is created by transforming the Y position of each animation by -10px and changing color from blue to yellow. To create the underlying shadow effect a drop shadow is added to each span which changes its vertical shadow size from 0px to 20px. 

1
#preloader_4{
2
    position:relative;
3
}
4
#preloader_4 span{
5
	position:absolute;
6
	width:20px;
7
	height:20px;
8
	background:#3498db;
9
	opacity:0.5;
10
border-radius:20px;
11
	-animation: preloader_4 1s infinite ease-in-out;
12
13
}
14
#preloader_4 span:nth-child(2){
15
	left:20px;
16
	animation-delay: .2s;
17
}
18
#preloader_4 span:nth-child(3){
19
	left:40px;
20
	animation-delay: .4s;
21
}
22
#preloader_4 span:nth-child(4){
23
	left:60px;
24
	animation-delay: .6s;
25
}
26
#preloader_4 span:nth-child(5){
27
	left:80px;
28
	animation-delay: .8s;
29
}
30
@keyframes preloader_4 {
31
    0% {opacity: 0.3; transform:translateY(0px);    box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.1);}
32
    50% {opacity: 1; transform: translateY(-10px); background:#f1c40f;	box-shadow: 0px 20px 3px rgba(0, 0, 0, 0.05);}
33
  	100%  {opacity: 0.3; transform:translateY(0px);	box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.1);}
34
}

Similarly to pre-loader 1, by adding animation-delay to each span and offsetting each span's delay by 2 milliseconds it creates the wave effect.

5. Spinning Disc

This is.. a spinny thing.

HTML

Here we simply use a single div for the central circle and  :after on that div to create the outer lines.

1
 <div id="preloader5"></div>

CSS

Adding a border to the top and bottom and giving it a border-radius of 50px creates the two outer lines. An animation is added to the main div, just to change the main div's color and create the rotation effect by adding transform: rotate() . The :after element animation is added to change the color of the outer border.

1
#preloader5{
2
    position:relative;
3
	width:30px;
4
	height:30px;
5
	background:#3498db;
6
	border-radius:50px;
7
	animation: preloader_5 1.5s infinite linear;
8
}
9
#preloader5:after{
10
	position:absolute;
11
	width:50px;
12
	height:50px;
13
	border-top:10px solid #9b59b6;
14
	border-bottom:10px solid #9b59b6;
15
	border-left:10px solid transparent;
16
	border-right:10px solid transparent;
17
	border-radius:50px;
18
	content:'';
19
	top:-20px;
20
	left:-20px;
21
	animation: preloader_5_after 1.5s infinite linear;
22
}
23
@keyframes preloader_5 {
24
    0% {transform: rotate(0deg);}
25
    50% {transform: rotate(180deg);background:#2ecc71;}
26
    100% {transform: rotate(360deg);}
27
}
28
@keyframes preloader_5_after {
29
    0% {border-top:10px solid #9b59b6;border-bottom:10px solid #9b59b6;}
30
    50% {border-top:10px solid #3498db;border-bottom:10px solid #3498db;}
31
    100% {border-top:10px solid #9b59b6;border-bottom:10px solid #9b59b6;}
32
}

6. Glistening Window

We've gone a bit Microsoft on this one..

HTML

This pre-loader is created using a div and four spans to create each square.

1
 <div id="preloader6">
2
    		<span></span>
3
			<span></span>
4
			<span></span>
5
			<span></span>
6
		</div>

CSS

These squares are then positioned in a grid-like order. Animation is added to the main div to rotate the whole pre-loader. Another animation is added to the spans which scales them from 100% to 50%. We then add animation-delay  to each span to create the pulsing effect.

1
#preloader6{
2
    position:relative;
3
	width: 42px;
4
	height: 42px;
5
	animation: preloader_6 5s infinite linear;
6
}
7
#preloader6 span{
8
	width:20px;
9
	height:20px;
10
	position:absolute;
11
	background:red;
12
	display:block;
13
	animation: preloader_6_span 1s infinite linear;
14
}
15
#preloader6 span:nth-child(1){
16
background:#2ecc71;
17
18
}
19
#preloader6 span:nth-child(2){
20
left:22px;
21
background:#9b59b6;
22
	animation-delay: .2s;
23
24
}
25
#preloader6 span:nth-child(3){
26
top:22px;
27
background:#3498db;
28
	animation-delay: .4s;
29
}
30
#preloader6 span:nth-child(4){
31
top:22px;
32
left:22px;
33
background:#f1c40f;
34
	animation-delay: .6s;
35
}
36
@keyframes preloader_6_span {
37
   0% { transform:scale(1); }
38
   50% { transform:scale(0.5); }
39
   100% { transform:scale(1); }
40
}

Conclusion

The great advantage about using CSS3 pre-loaders over image pre-loaders is that they are scalable and retina ready. That means that no matter what device they are displayed on they will always be crisp, clean and future proof (though bear in mind that not all legacy browsers support CSS3 animation). 

By understanding a few important CSS3 properties and techniques you should now be able to create your own CSS3 pre-loaders. They're fun to create and with a little bit of experimentation you can create some really cool animations to prevent users from leaving your website.

If you've created any cool pre-loaders recently then I'd love to see them! Feel free to leave your comments below.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.