patternhtmlMinor
Full-page background in HTML
Viewed 0 times
htmlfullpagebackground
Problem
I am trying to make a page background for my website. Is this a good way to do it?
This is my HTML:
And this is my CSS:
This is my HTML:
And this is my CSS:
#page-background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
}Solution
The first small comment is that there's no need to put this extra
The other two issues are image ratio (do you want it to be preserved?) and browser support (willing to support IE6 and old Android and iPhones?)
div tag. Simply apply the page-background id to your img tag. It's still nice that you put it in the HTML code.The other two issues are image ratio (do you want it to be preserved?) and browser support (willing to support IE6 and old Android and iPhones?)
- position: fixed is not supported before iOS 5 and Android 4.0. Doesn't work on IE6 either. You could use a method that has been tested for every desktop browser since IE5 and will also work on any phone (tested on my old Android).
- If you want to preserve image ratio and don't care about old desktop browsers, you could use the CSS3
background-size:coverproperty supported since IE9, Android 2.2 and iOS4.
- If you want to preserve image ratio and support every browser, there's the usual JavaScript fallback. Use Modernizr to detect the CSS3 property and this jQuery script to simulate background-size: cover.
Context
StackExchange Code Review Q#9449, answer score: 5
Revisions (0)
No revisions yet.