Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 100% CSS background-image with scrolling content?

I want to create a site with a background image that always fills the entire window, even if the content can scroll vertically.

I have created this JSFiddle using background-size:cover to scale the background-image to the window.

It works, as long as the divs inside are smaller than the window. If you scroll vertically, the background image does not fill the page anymore, and shows a white/grey area instead.

So my question is: how can I combine a 100% background image with scrolling content? This is my CSS:

html {
    height: 100%;
    margin:0px;
    background-color:#999999;
    background-image: url(http://s22.postimg.org/e03w9stjl/main_bg.png);
    background-position:center;
    background-repeat:no-repeat;
    background-size: cover;
}
body {
    min-height: 100%;
    margin:0px;
}
#appcontainer {
    position: absolute;
    background-color: rgba(255, 255, 255, 0.7);
    width:560px; height:2220px;
    left:20px; top:20px;
}

And HTML:

<!DOCTYPE html>
<html>
<head></head>
<body>

    <div id="appcontainer">
        This div causes the background image to stop scaling to the page.
    </div>  

</body>
</html>
like image 973
Kokodoko Avatar asked Oct 14 '13 15:10

Kokodoko


People also ask

How do I keep my background fixed while scrolling CSS?

To keep your background fixed, scroll, or local in CSS, we have to use the background-attachment property. Background-attachment: This property is used in CSS to set a background image as fixed or scroll. The default value of this property is scroll.

Which CSS property allows you to easily make an image cover the entire background of an element via a URL?

The background-image CSS property sets one or more background images on an element.

How do I stretch a background image to the whole page?

When you work with background images, you may want an image to stretch to fit the page despite the wide range of devices and screen sizes. The best way to stretch an image to fit the background of an element is to use the CSS3 property, for background-size, and set it equal to cover.


2 Answers

Use background-attachment: fixed;

Demo

html { 
    height: 100%;
    margin:0px;
    background: url(http://www.freegreatpicture.com/files/147/18380-hd-color-background-wallpaper.jpg) no-repeat center center;
    background-size: cover;
    background-attachment: fixed;
}

Also, I didn't got why you are using position: absolute; on the wrapper element, generally you should be using position: relative;

like image 101
Mr. Alien Avatar answered Oct 27 '22 00:10

Mr. Alien


Add to your CSS:

background-attachment: fixed;
like image 40
gvee Avatar answered Oct 26 '22 23:10

gvee