Fullsize Background Image with CSS3 background-size

The CSS3 background-size property is pretty useful to create a fullsize background image. Just use the value cover and the image will automatically cover the width and height of the selected element. The best thing is that the image keeps its aspect ratio and centered itself vertically and horizontally.

To save bandwidth you can use a small image. But remember the image will be streched at least to the width and height of the browser window. As result you get a loss of quality on high resolution screens.

To solve this problem you can use a pattern overlay. All you need is an image of a pixel with a transparent background that will be repeated over the whole area. With CSS3 multiple backgrounds you can apply the main background image and the pixel pattern overlay on the same element. That's very nice because you keep your markup clean and don't have to write extra CSS for pseudo elements.

Fallback

This should work in Chrome, FF 3.6+, Safari 4.1+, Opera 10.5+ and IE9. Strangely in Opera is a kind of parallax scrolling. If you know how to fix that please let me know. For other/older Browsers you can create a fallback function with JavaScript(jQuery). Just include the following JavaScript and CSS code.

First we check the background-size support (You can also use Modernizr). If there is no support we add an image that adapts at least the width and height of the page. The image still keeps its aspect ratio. Finally we add a div with the pattern overlay that covers the whole page. That's it.

IE7 performance problem

The fallback should work in IE7 and IE8. But there is a performance issue in IE7 when scrolling pages that have large regions with a transparent image background, where the image used is very small. You can work around this performance problem by using a larger (wider/taller) image.

Problem with mobile devices

Unfortunately there is a performance issue on mobile devices with fixed background images when scrolling pages. So if you have scrolling content you should use a non-fixed background image on mobile devices, maybe with Media Queries.