position a div on top of an image

You want to position the second div with absolute:

http://jsfiddle.net/sbNZu/

Relevant code:

img {
    border: 2px solid black;   
}

#container {
    position: relative;    
}

#example {
   position: absolute;
   top: 10px;
   left: 10px; 
    
   padding: 5px;
   background-color: white;
   border: 2px solid red;
}
<div id="container">
    <img src="https://placekitten.com/g/193/129">
    <div id="example">This is my div</div>
</div>

You need to use position: absolute; and the parent must be position: relative. Without a proper position rule set z-index means squat.


Have the parent with position: relative, the children with position: absolute, and use negative values for margin on the children to move it on top. You may not even need to deal with z-index.

Tags:

Html

Css