简体   繁体   中英

Stretching a background image vertically not in scale, not just cover - Can this be accomplished?

EDIT: The answer would allow the background image to change it's height depending on the size of the body. if the body is 500px high, it should be 100% width, 500px height. or 100% width 2500px height.

Maybe I'm missing the boat on this, but I'm trying to figure out how to have my background image scale with the page. The end user doesn't want for the background image to be static (COVER), but the image should scale with the bigger his content gets on his site.

I'm guessing this can't be done with CSS alone. When I say I guess I've been through a mess load of different ways of doing this.

Is this just a simple javascript/jquery where I get the height of the body tag, and then apply that to the background image height?

If you need an example:

<body>
<div class="first"><!--TEXT--></div>
<div class="second"><!--TEXT--></div>
</body>

CSS

body { background: url(http://flashfreezeicecream.com/bg.jpg) center no-repeat; }
div { width: 75%; margin: 0 auto; }
.first { height: 1000px; }
.second { height: 500px; }

http://jsfiddle.net/WEat7/

This would need to work on multiple pages with different body heights

EDIT: http://jsfiddle.net/WEat7/1/ Fixed widths on the divs to illustrate the concept. I apologize

body {
    background: url(http://flashfreezeicecream.com/bg.jpg) center no-repeat; 
    background-size:100% 100%;
}

http://jsfiddle.net/WEat7/

The following CSS should fix the background image and have it cover the entire body no matter what size the width or height - see demo

body {
    background: url(http://flashfreezeicecream.com/bg.jpg) no-repeat center center fixed;
    background-size:cover;
}

However, please note that IE8 does not support background-size .

Edit: updated demo using following CSS

body {
    background: url(http://flashfreezeicecream.com/bg.jpg) no-repeat center center;
    background-size:100% 100%;
}

添加到你的身体css:

background-size:100% 100%;

It seems that we need a wrap answer ))

It has been suggested above that background-size: 100% 100%; will stretch the background image to the full width and the full height. And so it does.

Say your content is small (400px) - the background image will cover only 400 - http://jsfiddle.net/skip405/WEat7/7/

Say your content is really huge (2500px) - the background image will still cover the full height - http://jsfiddle.net/skip405/WEat7/8/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM