Archived Blog

Let’s Cook Pixel Pancakes! A CSS3 Animation Tutorial

It’s that time of week again! Cue the epic music; it’s Tutorial Tuesday!

As it’s Pancake Day, I thought it would be a good idea to use this for inspiration for today’s post. We will in fact be cooking some pancakes with our lovely pixelated friend, Bob.

Try hovering over him to take a look at the end result:

Doesn’t he look ecstatic? The animation above is achieved using a combination of HTML, CSS3 and images.

Before we proceed, I’d like to clarify that the purpose of this tutorial is to showcase what can be done via CSS3 animations. I won’t be creating JavaScript fallbacks; browsers that don’t support CSS3 animations will just see a static version of Bob (with the exception of the flame, which is animated by using a .gif file).

In addition, please note that I won’t be using vendor prefixes to keep things concise. Here’s an article on CSS Tricks in case you need help setting up vendor prefixes.

The HTML & CSS

Let’s start by preparing the images. Bob and his cooker are actually comprised of 6 layers stacked on top of each other as shown below:

The order in which they’re stacked on top of each other doesn’t generally matter, with the exception of the pancake and arm. These need to be placed behind the rest of the images by either defining z-index values, or by utilising the natural stacking order; you’ll see the reason for this later. Personally I opted to use the natural stacking order as it’ll help me cut out a few lines of CSS. It’s also worth noting that I used an animated .gif for the flame.

I set the container to position: relative; so that I could position the child images in relation to the container, i.e. div#animation.

I set the position of each image using position: absolute;.

I explicitly set the width and height of the container. This is required as the container would otherwise have no dimensions due to the fact that all it’s child elements are positioned absolutely within it.

I added a little padding to the top of the container <div>, to give the head room to bob (no pun intended) up and down. In retrospect, I could have added some margin to the top instead, which would have slightly simplified the calculations of the image positioning. If you use the margin approach, make sure overflow is set to visible (which is default), else you’ll see some cropping.

We now have a static version of Bob to which we can add some CSS3 animation.

The Basics of CSS3 Animations

When it comes to creating CSS3 animations, we need to make use of the @keyframes rule. As the name suggests, @keyframes allow us to specify the key frames within the animation. The browser will then “tween” between the frames, i.e. it’ll automatically add the transition.

A simple example would be changing the background colour and font colour of a <div> when a user mouses over it.

Hover over me to see a CSS3 animation!

The above animation has 3 key frames:

Initial frame – Dark blue background with white text.

Intermediate frame – Light blue background with white text.

Final frame – Light blue background with yellow text.

All we need to do is specify the above CSS styles within the @keyframes rule, and use percentages to define when those key frames should occur within the duration of the animation. For example 0% would be the start of the animation, and 100% would be the end.

You’ll notice that I’ve put example-animation after @keyframes; this is the identifier for the animation and must be specified (obviously, you can name it what you want). It’s also worth noting that you must specify 0% (or the keyword from) and 100% (or to) as a bare minimum, else the keyframes declaration is invalid.

I’m sure some of you may have already noticed, but there’s actually nothing in the code above to bind the animation to a specific element; all we’ve done is created a set of key frames. To attach it to an element we’ll need to use the animation property, which has the following syntax:

I think the code here is pretty self-explanatory, but a few things to note:

Translate moves the element along the x-axis (first argument) and y-axis (second argument). A positive value will move the element right/down, whereas a negative value will move the element left/up.

Rotate accepts one argument and will rotate in a clockwise fashion, unless a negative value is specified.

When specifying multiple transform functions, they should be separated with a space as shown above.

We can then attach it to an element using animation as previously mentioned. In this case we’re animating Bob when someone hovers over the canvas, so the following CSS will suffice:

#animation:hover #head {
animation: headBob 0.3s infinite;
}

You’ll notice that I set the iteration-count to infinite, as I want Bob’s head to be bobbing the whole time the user’s mousing over the canvas.

Animating Bob’s Arm & Pancake

This is a little more tricky as we have to animate 2 image elements separately, while keeping them in sync. The timelines below should help clarify what we’re looking for. Please note that I’ve had to change the axis somewhat, so that the images fit!

10% – At this point, Bob’s arm has been rotated by 15° anti-clockwise. The pancake is still hidden behind the pan. We can achieve this by translating it along the y-axis.

20% – Bob’s arm is back to it’s starting position (and will remain there until the end of this iteration). The pancake on the other hand has been moved up further, as well as rotated 360° anti-clockwise.

50% – The pancake is moved back down behind the pancake, having undergone a 720° anti-clockwise rotation.

100% – No change, to mimic a delay between the flicks.

Now that we’ve outlined the exact steps in the animation, the CSS is relatively easy to code up:

So now we’ve got the head bobbing, the arm flicking and the pancake flying, surely we’re done?

Try hovering over Bob below to see what we’ve done so far:

Oh dear! Bob’s got himself an extendable arm!

The problem we’re seeing above is due to the transform-origin property, which as it’s name suggests, defines the origin of the transform. This property defaults to the centre of the element in question and therefore Bob’s arm is rotating around the centre of the image of the arm.

What we actually need is for the transform-origin to be his elbow, as that’s the pivot. Let’s go ahead and change that property:

And there we have it! A fully-functional animation of Bob making pancakes!

Here’s a download link in case you’d like to download all the assets (HTML, CSS and images) used in the tutorial. I’ve also included the .less file with custom mixins, that I used to create the CSS. If you’d like to learn more about Less, my colleague Andy wrote an awesome Introduction to Less, which should help you get on your way!

Thanks for sticking around until the end of the tutorial. Hopefully you’ve learnt a thing or two about CSS3 animations. As always, if you have any questions or constructive criticisms, please don’t hesitate to leave a comment below!

Want to hire us? We'd love to hear from you

Pelling have over 50 years combined experience designing and coding websites. We work with companies of all sizes, from one-man bands to multinational blue chip companies. We endeavour to provide high quality service, while maintaining a competitive price.

We pride ourselves in our level of customer service, which we believe is the main reason behind our high customer retention rates.