Nice to see html background-image being used well. Because some developers depend on trailing edge css to maintain backwards compatibility, many quirks exist for otherwise css3
compatible browsers. Like background:cover
in older Safari versions (<4.05), which does not work. In this case we can use the older css3
vendor fallback as shown
html {
background: url(images/bg.jpg) no-repeat center center fixed;
background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
-webkit-backgound-size:100% 100%; /* early css3 implementation */
-webkit-background-size:cover;
}
And, if by chance you want different background images for different pages in pure css, lets go with
html#bg1 {backrgound-image:url(images/bg1.jpg);}
html#bg2 {background-image:url(images/bg2.jpg);}
But, if you cannot give your <html>
tag an id
or class
you may have to resort to some javascript
. You can then use a different background image for each page on the <html>
element, which is just what is needed. This seems to work fine on the html
tag. Some browsers ignore the css
for background-image
when used on html
. The body
tag is less temperamental. This may be a better fix than the php-based method. For example:
<script>
document.getElementsByTagName('html')[0].style.backgroundImage='url(images/bg3.jpg)';
</script>
The html css for background-size would continue to apply and would cover correctly across various browser versions. Note however, that older Safari will throw an error on javascript
which uses an empty url, say to clear the background image, e.g.
<script> /*Causes Error Safari 4*/
document.getElementsByTagName('html')[0].style.backgroundImage='url()';
</script>
The error will normally halt javascript
execution with no clue as to the cause. Instead we need to use style.backgroundImage='none';