views:

161

answers:

2

I am trying to set a background image to be outside the actual containing div.

<div class="expandable">Show Details</div>

.expandable
{
    background: transparent url('./images/expand.gif') no-repeat -20px 0px;
}

so the "expand" image should basically appear just to the left of the div.

I can't get this working, the image doesn't show when it's positioned outside the borders of the container. I'm not sure if there's a CSS trick I am missing, or if it's something to do with my page layout (the "expandable" div is nested inside several other divs).

Is it possible to do this? Any hints?

Edit: here is a jsFiddle showing the problem: link

+3  A: 

You're going to have to put the background image inside a separate element. Background image positions cannot place the image outside the element they're applied to.

edit your question jogged my memory and I went and checked the CSS specs. There is in fact a "background-attachment" CSS attribute you can set, which anchors the background to the viewport instead of the element. However, it's buggy or broken in IE, which is why I've got it sitting on the "do not use" shelf in my head :-)

Pointy
that sucks!! oh well, looks like jQuery will have to rescue me again :)
fearofawhackplanet
The "background-attachment" thing is pretty weird anyway, and it might be more trouble to use it than to use a companion element. I doubt you could make "background-attachment" work purely from CSS, because when you use it the positioning values reference the window and have nothing to do with the element. Thus you'd have to compute what those coordinates should be, and that seems like a big pain.
Pointy
+1  A: 

You can't do this how you want to exactly, but there is a pretty straightforward solution. You can put another div inside of #expandable like:

<div class="expandable">Show Details<div class="expand-image"></div></div>

Then your CSS would look something like:

.expandable{ position:relative; }
.expandable-image{ 
    position:absolute; top:0px; left:-20px;
    width:<width>px; height:<height>px;
    background: url('./images/expand.gif') no-repeat;
}
Russell Leggett