views:

318

answers:

3

Hi all, I'm trying to make two transparent images (having the same size/dimension ) overlapping into a div at their top left corner. I tried:

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<body>
<div style="margin:20px;">
<div id="main" style="overflow:hidden;background-color:red;width:400px;height:400px;border:3px solid blue;">
<img src="myimage1.png" style="position:relative;top:0px;left:0px;z-index:0;"/>
<img src="myimage2.png" style="position:relative;top:0px;left:0px;z-index:10;"/>
</div>
</div>
</body>
</html>

but it doesn't work, the two pictures are concatenated into the parent div.

Thanks for your help !

+3  A: 

Try to make #main have position:relative, then change the two <img>'s to use position:absolute.

KennyTM
beat me to it--here's a live example http://jsbin.com/unogu3/ (I offset the images to show they overlap--obviously yours would be a little different)
Michael Haren
+1  A: 

make the second image position:absolute and #main position:relative

carillonator
A: 

Just a note:

position:relative and position:absolute refer to 0;0 of closest parent, which has position:relative or position:absolute. If none of your divs has, then it refers to 0;0 (top;left) of document (body).

position:relative - top and left refer to difference between natural flow value of element - i.e. if "image" has natural flow x 1500 and y 1200, top: 150px; left: -50px; will move it to x: 1450; y: 1350;

position:absolute - top and left are aligned against 0;0 of closest p:r or p:a parent element, regardless of natural flow - ex. (in pseudo html/css):

<div style="relative/absolute">
  <img absolute="top:-20px; left: 150px;">
</div>

Image will be 20px higher and 150px to the right from left top corner of parent div.

That said, you want your container either relative and absolute and both of your stacked images should be position:relative with top: 0; left: 0;. Don't forget to set z-index, if you want to have better control over layering.

-A

Adam Kiss