Posted by: Jade | September 29, 2006

CSS Stretch Background Image?

Q: Ever wonder that css can stretch a background image? So no matter what the monitor size the background image always stretches to meet the requirements?

A: There is no way to stretch or control the size of a background image using css. However, you could use the background, background-attachment and background-repeat porperties to achieve an alternative result…

Watch browser support for these though!

Well, CSS1 and CSS2 still can’t do the magic yet, but CSS3 can! Here’s how:-

The ‘background-size’ property

Is ‘background-stretch’ a better name?

Specifies the size of the background images. The section “Layering multiple background images” defines to which image of the ‘background-image’ property each of the comma-separated values applies.

If a comma-separated value has only one part (not counting the keyword ’round’), the second part is set to ‘auto’. Of the two parts, the first one refers to the width, the second to the height of the corresponding background image. The addition of the keyword ’round’ indicates that the width and height are approximate, as explained below. ’round’ on its own is equivalent to ‘auto auto round’.

The size of an image is established in two steps. The first step derives sizes as follows:

auto
The size that keeps the background image’s original aspect ratio. Additionally, if the other part of the value is also ‘auto’, the image has its intrinsic size.
<length>
A specific size.
<percentage>
The percentage is relative to the width or height of the area given by ‘background-origin’.

Negative values are not allowed. A size of zero is allowed, but causes the image not to be displayed. (The effect is the same as if it had been a transparent image.)

If the given size is accompanied by the keyword ’round’, and the computed value of ‘background-repeat’ is repeat for the horizontal and/or vertical direction, there is a second step: The UA must reduce the width, resp., height so that the image fits a whole number of times in the background area. In the case of the width:

If X ≠ 0 is the width of the image (i.e., the specified length or percentage, or the intrinsic width if the ‘background-size’ is ‘auto’) and W is the width of the background area, then the rounded width X’ = W / ceil(W / X)

The height is analogous. ceil() is a function that returns its argument if it is a whole number, otherwise the next bigger whole number.

If the width is reduced because of this formula, the aspect ratio is not retained, not even if the height was specifed as ‘auto’ and the vertical repeat as ‘no-repeat’. Ditto if the height is reduced.

Should ’round’ be specified for width and height separately? It then becoems possible to round in one direction and keep the aspect ratio in the other. E.g., instead of allowing the keyword in ‘background-size’, it could be one of the possible values in ‘background-repeat’: [ repeat | space | no-repeat | round ]{1,2}.

Is ’round’ the right word? How about ‘~’ in front of the number, or ‘approx’ or ‘about’?

Is it better allow the size of the image to be rounded up as well as down?

Here are some examples. The first example stretches the background image independently in both directions to completely cover the content area:

div {    background-image: url(plasma.png);background-size: 100%;background-origin: content}

The second example stretches the image so that exactly two copies fit horizontally. The aspect ratio is preserved:

p {    background-image: url(tubes.png);background-size: 50% auto;background-origin: border}

This example forces the background image to be 15 by 15 pixels:

para {    background-size: 15px;background-image: url(tile.png)}

This example uses the image’s intrinsic size. Note that this is the only possible behavior in CSS level 1 and 2.

body {    background-size: auto;background-image: url(flower.png)}

The following example rounds the height of the image to 25%, down from the specified value of 30%. At 30%, three images would fit entirely and a fourth only partially. After rounding, four images fit. The width of the image is 20% of the background area width and is not rounded.

p {    background-image: url(chain.png);background-repeat: repeat-y;background-size: 20% 30% round; }

Responses

This works in firefox, not sure about IE :P

This works in firefox, not sure about IE :P

One more try…

<body>
<div style=" position:absolute;z-index:0;top:0;left:0;width:100%;height:100%; " >
<img src="background.jpg" width="100%" height="100%">
</div>
<div style=" z-index:1;position:absolute; ">
<!-- Page Content -->
</div>
</body>

It does not working for Firefox (ver 2.0.0.4).

See the test here:
http://webdesign.about.com/od/examples/l/blbgsizeexample.htm

or here:
http://www.geocities.com/seanmhall2003/css3/bg.html

it’s easy to use css to stretch a background image to a specific size. just use the following :

html:

css:
#background {
position:absolute;
display:block;
z-index:0;
left:0px; right:0px;
top:0px; bottom:0px;
width:100%; height:100%;
}
#bg {
position:absolute;
z-index:0;
left:0px; top:0px;
width:100%;
height:100%;
}
.img1 { background-image:url(yourimage.jpg); }

and that’s all. whatever size you specify inside #bg for width and height (min/max works too!) will be the background image’s exact size, so it never actually repeats!. The only tricky part is the #background container div. The settings it must have depend on what it is a child of. But, for the #bg child element to size the background image correctly, #background must have display:block;. Padding limitations, and margin limitations, ALSO work on this background stretching method. Have fun!

WHOOPS, sorry about that response. the HTML got bleeped out. So if you don’t want to hit view page and dig for it, here it is again…

div id=”background”
div id=”bg” class=”img1″
/div
/div

This works with any kind of image of course, stretches the background-image to any size or shape, works in all major browsers, and is very AJAX friendly.

Tested the above method, but with no luck (in FF 2.0.0.9.)!
http://armands.freehostia.com/nami/temp2.html

I also don`t get why it should work.
Because of display:block?

Work in Firefox and IE!

HTML:

text

CSS:
body {
margin: 0px;
padding: 0px;
overflow: hidden;
}

#back {
position: absolute;
height: 100%;
width: 100%;
}

#content {
overflow: auto;
position: relative;
height: 100%;
width: 100%;
left: 0px;
top: 0px;
}

Problems with posting html code

HTML:

div id=”back”
img src=”back.jpg” width=”100%” height=”100%”
/div

div id=”content”
text here
/div

Add z-index into Dexter’s snippet:

#back {
position: absolute;
height: 100%;
width: 100%;
z-index:-1;
}

thanks a lot for this article. the solution provided by dubby was the one that helped me that i needed as i only wanted to apply the stretchy bg int a single div.

i just wanted to post my version of the code because some of the things dubby suggested to put on it were actually keeping his solution from working on my site.

here is what i ended up with

#apoccomment {
display:block;
z-index:0;
left:0px; right:0px;
top:0px; bottom:0px;
height:100%;
}

#bg {
z-index:0;
left:0px; top:0px;
height:100%;
background:url(”commentbg.jpg”);
background-repeat: no-repeat;
height:100%;}

i removed the absolute positioning and only kept height since height was the only thing i wanted to stretch for my site. i could probably even get rid of the z-position lines but they don’t bother me. as long as it works i’m happy.

my html was simply

div id=”apoccomment”
div id=”bg”
content and more nested but irrelevent divs
/div
/div

tested on FF 20014 and ie6.

Hi. Thanks for this information. I’ve managed to stretch my background image. Can someone tell me why the content is not displaying on top of the background image? Is my code wrong?

Untitled Document

jkhjk

Sorry here it is again without the html tags just the text….

head
style type “text/css”
body {
margin: 0px;
padding: 0px;
overflow: hidden;
}

#back {
width: 100%;
height: 100%;
left: 0px;
top: 0px;
position: absolute;
z-index: -1;
}

#content {
position:absolute;
z-index: 0;
}
/style
/head
body
div id=”back” img src=”back.jpg” width=”100%” height=”600″/div
div id=”content” table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″
tr
tdjkhjktd
tr
/table
/body
/html

Leave a response

Your response:

Categories