Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent background disappearing

Tags:

css

So I've got a body with a CSS gradient background. Then I've got an absolute positioned div nested just within that with a background overlay. In turn, the content wrapper div is then nested within this. I want background div to be fixed and the web page to scroll over the top. The problem is, when the page scrolls the background overlay div kind of disappears like a roller blind...

Here's my fiddle to demonstrate the issue... http://jsfiddle.net/WPk6h/ (try scrolling the result pane to see the effect I mean).

HTML....

<body>
    <div id="bgwrapper">
        <div id="wrapper">
            Content...
        </div>
    </div>    
</body>

and CSS...

body {
    background-color:#fcf
}
#bgwrapper{
    position:absolute;
    top:0;bottom:0;left:0;right:0;width:100%;height:100%;
    background: transparent url(http://www.freesmileys.org/smileys/big/big-smiley-001.gif) no-repeat right bottom;
    background-size:cover;
    background-attachment:fixed;    
}
#wrapper {
    width:300px;
    margin: 0 auto;
}

Any ideas how to prevent this so that the background overlay remains visible at all times?

note... I've not tested it heavily in all browsers yet - the issue is in the first browser I've been using, Chrome so I haven't got round to testing in others yet.

EDIT...

People are wondering why I don't just apply the background image to the HTML or BODY tags - well, there is a clash between CSS gradients and background images - you cannot have them both in the same element, as can be seen with the two examples below. This is why I'm using an additional background wrapper div to create the effect of an 5% alpha image overlaying the gradient bg.

http://jsfiddle.net/tqbtm/ (attempting to add gradient and bg image to body tag)

http://jsfiddle.net/ca5wa/ (adding bg image to bg wrapper div over the body gradient)

like image 804
Doug Avatar asked Jan 23 '26 06:01

Doug


2 Answers

You need to remove position: absolute from #bgwrapper div:

#bgwrapper{
    width:100%;
    height:100%;
    background: transparent url(http://www.freesmileys.org/smileys/big/big-smiley-001.gif) no-repeat right bottom;
    background-size:cover;
    background-attachment:fixed;    
}

Update jsfiddle

like image 81
Morpheus Avatar answered Jan 25 '26 02:01

Morpheus


You could also have a look at the following link:

http://css-tricks.com/perfect-full-page-background-image/

which details several different methods of doing full-screen, fixed, backgrounds

the method I currently use is method 1 (CSS3) for this kind of technique

html { 
  background: url(images/bg.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
like image 44
kolin Avatar answered Jan 25 '26 02:01

kolin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!