a79ce734e1
a79ce734e1

Reputation: 875

Element inside DIV positioned beneath it

I accidentally deleted my question about this.

I have the following HTML:

<div id="frame">
    <img src="sample.png" alt="" />
</div>

CSS:

#frame { position: relative; z-index: 2; }
#frame img { position: relative; z-index: 1; }

What I'm trying to do, is get the image's parent DIV to be positioned on top (it's a frame). It seems the CSS doesn't do it, but why? How can I make this work?

Upvotes: 2

Views: 294

Answers (4)

meder omuraliev
meder omuraliev

Reputation: 186662

Children of elements will have a higher stacking order than their parents, naturally ( children of the html element must be visible on TOP of html, and so forth ).

You may be able to offset this natural behavior by setting a z-index of -1 on the img.

EDIT: Why can't you just enclose the other "content" and/or use another wrapper? Would be the ideal solution. Otherwise you're hacking and trying to go around natural stacking order behaviour, which is meant to be that way

<div id="parent">
    <div id="frame"></div>
    <div id="child"></div>
</div>

Edit #2: Guess I was right all along, z-index of -1 works as well: http://jsfiddle.net/yBH2G/1/

Upvotes: 1

ScottS
ScottS

Reputation: 72261

You can achieve what you want (may not work in older browsers):

#frame { ... no special css (but background should be [semi-]transparent) ... }
#frame img { position: relative; z-index: -1; }

Note the negative z-index number. This is why some browsers have issues, but all modern browsers handle this just fine.

@Edit: Here's a test case that works in Firefox 3.6, IE8, Safari 4 (IE6 & 7 need to not have the containing element have position, that is, it must be position: static to get it to work);

<div style="border: 5px solid red; width: 190px; height: 190px; position: relative; zoom: 1; margin: 20px">Example Text
<div style="position: absolute; z-index: -1; width: 200px; height: 200px; background-color: blue; opacity: .7; top: -10px;"></div>
</div>

Upvotes: 0

dockeryZ
dockeryZ

Reputation: 3981

Why not put the "frame" and image inside a div itself.

#box img, #box #frame {
    position:relative;
}
#box #frame {
    z-index: 100;
}

<div id="box">
    <div id="frame"></div>
    <img src='image.jpg' />
</div>

Upvotes: 0

hookedonwinter
hookedonwinter

Reputation: 12666

Give the parent div a set height and see what happens.

Upvotes: 0

Related Questions