Starry Night: Incredible 3D Background Effect with Parallax

Avatar of Chris Coyier
Chris Coyier on (Updated on )

starrynight.png

If you haven’t seen the mysteriously cool Silverback app teaser page you should check it out. The Gorilla was designed by Jon Hicks (yep! the same site with the cool body-border technique). Make sure you resize your browser around when you visit the site to see the cool “parallax” effect of the hanging vines. The site itself was designed by Paul Annett. Paul wrote an article on Think Vitamin on how he created that parallax effect.

I thought I would take the idea an explain it in a bit more detail, which a slightly different application, and offer up a download for those interested.

What is Parallax?

You can think of motion parallax as what you see when you look out of a moving train. Bushes right alongside the train tracks will zip past your view, while cattle grazing in the field will appear to move more slowly across your view and the mountains in the background will hardly move at all. Parallax has particular relevance to astronomy. Not only because sci-fi movies use the effect all the time to create cool space effects, but because this effect is how we measure how far stars are away from our own solar system.

Multiple Backgrounds with Parallax

In honor of this amazing effect and it’s relevance to science, I created Starry Night. Starry night is a full-page background effect with three layers. Distant stars will only move a little bit as you resize the browser window, while closer stars will move faster. The effect is created with three page elements, all of which occupy the entire browser window. The body (“background”), a div (“midground”), and another div (“foreground”).

<body>
	<div id="midground"></div>
	<div id="foreground"></div>
</body>

The body element will fill the entire screen by default, but we’ll have to style the other two divs with absolute positioning to do the same.

body { 
	background: url(images/background.png) repeat 5% 5%;
}
#midground {
	background: url(images/midground.png) repeat 20% 20%;
	position: absolute;
	top: 0; left: 0; right: 0; bottom: 0;
	z-index: 997;
}
#foreground {
	background: url(images/foreground.png) repeat 90% 110%;
	position: absolute;
	top: 0; left: 0; right: 0; bottom: 0;
	z-index: 998;
}

Both the midground and the foreground PNG files are alpha-transparent, so they will lay on top of each other nicely. The important part here is the % values you see in the background attribute. This controls the horizontal and vertical positioning, but in the context of this example, you can read that as “how fast you want it to move”. Also notice you can go beyond 100%, this means it will move at a faster rate than the rate of resizing the browser window.

It is worth mentioning at this time that this effect just ain’t gonna happen in IE 6 or below. Between the bugs with PNG and the bugs with absolute positioning, it’s just not worth it. In my example, I use a conditional stylesheet to display a warning message.

Where does my content go?

That’ll be up to you, notice in my example page the small bit of content in the upper right. You could do something similar. Remember that any area you build to put content into will need to be absolutely positioned and have a higher z-index value. Otherwise, the absolutely positioned foreground and midground will cover up your content area and you won’t be able to select or click anything (if you can see it at all).

[VIEW EXAMPLE PAGE]

[DOWNLOAD EXAMPLE]
.ZIP file inclues Photoshop document for the stars.