The animation of the solar system in pure CSS 3 by Julian Garnier is an amazing piece of work. In this article, we’ll take an in-depth look at how it’s done. We’ll analyze the CSS piece by piece and show you what makes the solar system tick, and you’ll learn all about base64 image encoding in CSS, keyframe animations and CSS 3D transformations.
In this CSS 3 tutorial, I’ll be building a series of pens on CodePen, constructing the solar system bit by bit.
Setting the stage: CSS Reset, Prefixtree and importing a font
Before we starting building any CSS for our animation, it’s always a good idea to reset the CSS styles that are provided by the browser, as they vary from browser to browser. Text fonts and sizes will be different, as will paddings and margins between elements. This is called a CSS reset and it gives you a clean slate to work with. Or, as Eric Meyer who coined the term CSS Reset puts it,
“The goal of a reset stylesheet is to reduce browser inconsistencies in things like default line heights, margins and font sizes of heading.”
The following code is not specific for the animation we’re about to build, and resets more elements than we’ll actually use, but it’s boilerplate code you should use in every CSS project.
Another useful thing to do is to include the Prefix Tree CSS. Prefix lets you use only unprefixed CSS properties everywhere. It works behind the scenes, adding the current browser’s prefix to any CSS code, only when it’s needed. This’ll free us from remembering to put in all browser-specific prefixes. Note that in modern browsers, browser prefixes are thankfully increasingly disappearing, but to make it easier to support not-so-modern browsers, prefix tree saves us a lot of work. Let’s start by building our base HTML:
We’ve included references to prefix tree and our CSS file.
Let’s also set the base font for the animation project:
@import statement, we can have our CSS code load an external CSS file. Here, we are using a reference to a font on Google Fonts, which we can then use in our
font-family CSS rule.
Adding a background image in pure CSS using Base64 encoding
The first thing we’ll want to do is add a starfield back ground image to the CSS solar system animation. There are three ways we can go about this:
- Add an
<img>element to the HTML and stretch it to cover the entire page
- Add a background-image CSS rule (referring to an external image file) to the
- Add a background-image CSS rule using a base64-encoded image in the actual CSS to the
<img> element to the HTML means an external file, which is another connection to the server that the browser will have to make in order to get the image data. Moreover, it’s an additional element that will clutter up the HTML, and we’ll try to keep the HTML as clean as possible. Since it’s a background image, there’s no need for it to have alt and title attributes anyway. Finally, while an
<img> element can be stretched, it can’t be set to repeat itself.
Adding a CSS background-image rule referring to an external image also means a separate download, while encoding an image straight into the CSS using base64 encoding does not, so that’s the way we’ll go. We’ll use the following image. It’s 500×500 and about 15 kB in size:
Encoding this image in Base64 encoding can be done using various online tools. Upload your image, and get a Base64 string in return. This code is then pasted into the CSS like this:
(The Base64 string is actually about 18,000 characters long, but has been shortened in this example.)
This is one of the nice features that CSS 3 has to offer. You can now provide a
data: instruction inside the
url() function of background-image, where you specify the image MIME type (
image/jpeg in this case, but you can use
image/png and others as well) and the base64-encoded image data.
Here’s what we have so far:
The starfield image is repeated nicely over the entire page background.
Adding sunlight using a transparent PNG and background-size
In Julian Garnier’s demo, there is an additional background image that simulates the sun (when we get around to building the sun, that is) illuminating the universe from the center outward. To obtain this effect, an image with alpha transparency is used, which looks like this:
Let’s add a layer to our HTML to accommodate this background, which will stack over the starfield we already have.
Next, we’ll add CSS rules for the
#universe <div>, making it stretch to fill the entire screen. The background image is once again added as a base64 encoded string, and it’s set to stretch to cover all available space. Note that we’re using another new CSS 3 feature here, namely
background-size. According to the specification on W3C, you can set
cover in order to scale the background image proportionally to be as large as possible so that the background area is completely covered by the background image where some parts of the background image may not be in view within the background positioning area, which is what we want. Another option is
contain, to scale the image to the largest size such that both its width and its height can fit inside the available area. Finally, you can specify width and height percentages.
Now we get this:
Not bad for an HTML page containing only one element!
In the next part of this tutorial, we’ll have a look at applying keyframe animations and box shadows.