So you’re designing a website, and you’ve reached a point where you need two background images behind the whole page, both repeating in at least one direction, but not all your pages will be full screen height.

Let’s say that the “farthest” background is an abstract tiled pattern of some kind, and the “nearest” background gives you your column background colours. Let’s also say that you can’t merge the two images into one for some reason (different repeat directions, alpha transparency effects, etc).

If you use CSS to attach the farthest background to the body tag, and attach the nearest background to a div that encloses the whole page, you’ll still have problems if the page content doesn’t reach all the way down the screen.

Sure, you could use a min-height hack on the div, but if you allow for high resolution monitors (1200 vertical pixels or more), people using smaller monitors will see scrollbars that suggest there is much more content, when there isn’t. You could also hack it with JavaScript, but that just isn’t elegant enough, right?

Don’t worry. Here’s a very simple CSS trick (not a hack!) that I learned years ago, promptly forgot about, then rediscovered today when hunting through some old code.

The Code

html { background:url(farthest.png) 0% 0% repeat-x; }
body { background:url(nearest.png) 0% 0% repeat-y; }

Change your position and repeat properties as needed.

This works because the html tag always encloses the body tag, so it will always be at least the full size of the window because the body always is.

Tested on Firefox 3.5, Chrome 3, Safari 4, Opera 10 and Internet Explorer 6, 7 and 8.

Comment if you find it useful, or if you experience a problem with it.

Share Button
Facebook Comments

More Stuff

How do I check if a port is in use on Linux? I am a new Linux system user. I need to find out which process is listening on a port on Linux using the command line. How do you find out which pro...
How to change the ssh port on Linux or Unix server nixCraft Update... I am a new Linux/Unix server admin. How do I change the SSH Port for my Linux or Unix server using command line option? You can easily change the...
How do I know if my Linux is 32 bit or 64 bit? nixCraft I need to find out if my Linux server is running on 32-bit or 64-bit system. How do I know if my Linux is 32 bit or 64 bit? To check if your Linu...
How to update LXD container Ubuntu image from 16.04 to 18.04 nixCr... I run my app inside Ubuntu Linux 16.04 LTS lxd Linux container. To upgrade Ubuntu 16.04 to 18.04 LTS container, I run do-release-upgrade command, but ...
Spread the love

Posted by News Monkey