views:

61

answers:

5

Can background image extend beyond div's borders? Does overflow: visible apply to this?

A: 

After a little bit of research: No and No :)

Šime Vidas
Thanks- gave the answer to Guffa cuz he gave some more detail
Yarin
+4  A: 

No, a background can't go beyond the edge of an element.

The overflow style controls how the element reacts when the content is larger than the specified size of the element.

However, a floating element inside the div can extent outside the div, and that element could have a background. The usefulness of that is limited, though, as IE7 and earlier has a bug that causes the div to grow instead of letting the floating element show outside it.

Guffa
Interesting- thanks
Yarin
See my answer for a solution
Yarin
A: 

I tried using negative values for background-position but it didn't work (in firefox at least). There's not really any reason for it to. Just set the background image on one of the elements higher up in the hierarchy.

bemace
Negative background position values is used for sprite images, so that actually relies on the background *not* displaying outside the element. :)
Guffa
+1  A: 

No, the background won't extend beyond the borders. But you can stretch the border as far as you want using padding and some clever tweaking of negative margins & position.

kijin
Good point @kijin, I'll have to consider that
Yarin
@kijin, worked great- see my answer for more
Yarin
A: 

Following up on kijin's advice, I'd like to share my solution for image offsets:

/* 
Only effective cross-browser method to offset image out of bounds of container AFAIK, 
is to set as background image on div and apply matching margin/padding offsets:
*/
#logo {
    margin:-50px auto 0 auto;
    padding:50px 0 0 0;
    width:200px;
    height:200px;
    background:url(../images/logo.png) no-repeat;
}

I used this example on a simple div element <div id="logo"></div> to position my logo with a -50px vertical offset. (Note that the combined margin/padding settings ensure you don't run into collapsing margin issues.)

Yarin
The padding should be positive, not negative. A negative padding is invalid, so the style will be ignored.
Guffa