Forum Moderators: not2easy

Message Too Old, No Replies

Is it possible to a fixed background to the top AND bottom of a page?

         

smithaa02

5:50 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



CSS as most of you know as a neat trick where you can set background-attachment:fixed to the body and this allows the content to scroll over the background image.

What I would like to do is set both a top and bottom fix background image to my browser screen. So say...grass that stays at the bottom, but sky and a sun image that remains at the top. Is this possible?

BeeDeeDubbleU

6:01 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can't you create containers for the top and bottom (say masthead and footer) and put your background images in these?

swa66

6:47 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I didn't try it, but you have 2 elements already: html and body (I fear IE6 won't play along).
Make sure they both have 100% height. position one background on each and make sure the background colors of the outer one has the color you have that fits between the two images, and the upper then gets a transparent.

Again, I've not tried it, but that's how I'd start.

If it doesn't work with html and body, try adding a wrapper.

FWIW:
CSS3 will allow multiple background images per element
[w3.org...]
But browsers should not support that just yet.

smithaa02

7:06 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



BeeDeeDubbleU and swa66,

Thanks for the replies. If I were to use a nested div or wrapper, how would I ensure this would span to 100% of the screen height? Different resolutions would require heights, right?

swa66

7:39 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The root element of the document has the height of the viewport, and "
height: 100%;
" gives an element the same height as its direct parent, provided that direct parent has an explicitely set height (i.e. not "
height: auto
"). Just give all elements ( html, body, wrapper, ...) between it and the root element "
height: 100%
" in order to give them all the height of the viewport.

[edited by: swa66 at 11:30 pm (utc) on Dec. 22, 2008]

smithaa02

7:58 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



I tried your trick swa66 so that body element got the top end and the html element got the bottom end and this worked for firefox and ie7, but not ie6.

Any ideas?

height:100% was set to the body tag but that didn't work.

swa66

8:05 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I expected as much that IE6 wouldn't like <html> and <body> begin styled.

Do the same as you did, but set what you set on the html on the body and what you had on the body on a wrapper.

Keep the height:100% on the html.

It's not uncommon for IE6 to force you to add an otherwise unneeded wrapper.

smithaa02

8:10 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



Yeah I tried using a wrapper and moving the styles down to the body and wrapper and it still doesn't work... Both sections appear ok at first, but when I scroll the top section scrolls too which it obviously shouldn't. Height was set to 100% on html, body and wrapper.

swa66

8:18 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you post some code how far you got to play with ?

smithaa02

8:32 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



Hopefully, the my html survives the following code tag...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<style>
<!--
html body {
background: url(http://www.searchengineworld.com/gfx/logo.png) fixed;
background-position: bottom center;
background-repeat:repeat-x;

height:100%;
margin:0px;

}
html {
height:100%;
margin:0px;

}
#wrapper {
background-position: top center;
background: url(http://www.searchengineworld.com/gfx/logo.png) fixed;
background-repeat:repeat-x;
height:100%;
}
-->

</style>
</head>
<body ><div id='wrapper'><div style='background-color:tan;width:500px;height:2000px;background-position: top center;'>test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br />test<br /></div></div></body></html>

swa66

11:03 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Seems like the body and #wrapper are scrolling away from the backgrounds, also in FF etc.

My first idea was to position:fixed the body, but that didn't have any desired effect in FF3 - need to one day dig in the idea of position:fixed on a body, might be just too weird a concept to start with.

But that scroll away from the background is only happening due to our limited height of the body.

so if we make sure the body simply doiesn't have a limited height and use the top image on the body, it'll work:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>untitled</title>
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background: url(1.jpg) fixed repeat-x top center;
min-height: 100%;
}
html {
height:100%;
background: green url(2.jpg) fixed repeat-x bottom center;
}
</style>
</head>
<body >
replace with really long text,
at least more than 2 times the height of your viewport.
</body>
</html>

Works in FF3, safari and opera.

Works in IE7.

IE6 ...

Well there are two problems:

It doesn't honor the min-height, and that's a problem when there is to little content in the body to stretch it larger than the image you're using.

Easy fix: use height:100% in a conditional comment (IE6 treats height:100% mostly as min-height)

The second one is harder: there is a known bug where fixed background-attachment positioned backgrounds do scroll in IE6. There are a few tricks discussed a.o. here: [webmasterworld.com...] but I'd rather not use something that will limit the use of positioning inside the body (causing more trouble down the road).

So I take a look at one of my favorite scripts: IE7.js . It does have a fix for fixed background attachments, but they don't appear work on the body ... sigh.

Nothing lost, the fix does work on other elements, resulting in something like:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>untitled</title>
<style type="text/css">
* {
margin:0;
padding:0;
}
body {
background: url(1.jpg) fixed repeat-x top center;
min-height: 100%;
}
html {
height:100%;
background: green url(2.jpg) fixed repeat-x bottom center;
}
</style>
<!--[if lt IE 7]>
<script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE7.js"
type="text/javascript"></script>
<style type="text/css">
body {
height: 100%;
background:none;
}
#IE6 {
background: url(1.jpg) fixed repeat-x top center;
min-height: 100%;
}
</style>
<![endif]-->
</head>
<body>
<div id="IE6">
replace with really long text,
at least more than 2 times the height of your viewport.
</div>
</body>
</html>

Hope this inspires some to do even better.

Note that it you have 2 images, that in this case the top one will go over the bottom one if your images are taller than the available space.

smithaa02

2:25 pm on Dec 23, 2008 (gmt 0)

10+ Year Member



Thanks swa66 for all the information!