I have some HTML with two divs:
<div>
<div id="backdrop"><img alt="" src='/backdrop.png' /></div>
<div id="curtain" style="background-image:url(/curtain.png);background-position:100px 200px; height:250px; width:500px;"> </div>
</div>
I want the second div #curtain
to appear on top of the div #backdrop
. The two divs are the same size, however I'm not sure how to position the second div on top of the other.
There are many ways to do it, but this is pretty simple and avoids issues with disrupting inline content positioning. You might need to adjust for margins/padding, too.
#backdrop, #curtain {
height: 100px;
width: 200px;
}
#curtain {
position: relative;
top: -100px;
}
Use CSS position: absolute;
followed by top: 0px; left 0px;
in the style
attribute of each DIV. Replace the pixel values with whatever you want.
You can use z-index: x;
to set the vertical "order" (which one is "on top"). Replace x
with a number, higher numbers are on top of lower numbers.
Here is how your new code would look:
<div>
<div id="backdrop" style="z-index: 1; position: absolute; top: 0px; left: 0px;"><img alt="" src='/backdrop.png' /></div>
<div id="curtain" style="z-index: 2; position: absolute; top: 0px; left: 0px; background-image:url(/curtain.png);background-position:100px 200px; height:250px; width:500px;"> </div>
</div>
Have the style for the parent div and the child divs' set as following. It worked for my case, hope it helps you as well..
<div id="parentDiv">
<div id="backdrop"><img alt="" src='/backdrop.png' /></div>
<div id="curtain" style="background-image:url(/curtain.png);background-position:100px 200px; height:250px; width:500px;"> </div>
</div>
in your JS:
document.getElementById('parentDiv').style.position = 'relative';
document.getElementById('backdrop').style.position = 'absolute';
document.getElementById('curtain').style.position = 'absolute';
document.getElementById('curtain').style.zIndex= '2';//this number has to be greater than the zIndex of 'backdrop' if you are setting any
or in your CSS as in this working example:::
#parentDiv{_x000D_
background: yellow;_x000D_
height: 500px;_x000D_
width: 500px;_x000D_
position: relative;_x000D_
}_x000D_
#backdrop{_x000D_
background: red;_x000D_
height: 300px;_x000D_
width: 300px;_x000D_
position: absolute;_x000D_
}_x000D_
#curtain{_x000D_
background: green;_x000D_
height: 100px;_x000D_
width: 100px;_x000D_
position: absolute;_x000D_
z-index: 2;_x000D_
margin: 100px 0px 150px 100px;_x000D_
}
_x000D_
<div id="parentDiv"><h2>_x000D_
THIS IS PARENT DIV_x000D_
</h2>_x000D_
<div id="backdrop"><h4>_x000D_
THIS IS BACKDROP DIV_x000D_
</h4></div>_x000D_
<div id="curtain"><h6>_x000D_
THIS IS CURTAIN DIV_x000D_
</h6></div>_x000D_
</div>
_x000D_
Here is the jsFiddle
#backdrop{
border: 2px solid red;
width: 400px;
height: 200px;
position: absolute;
}
#curtain {
border: 1px solid blue;
width: 400px;
height: 200px;
position: absolute;
}
Use Z-index to move the one you want on top.
To properly display one div on top of another, we need to use the property position
as follows:
position: relative
position: absolute
I found a good example here:
.dvContainer {_x000D_
position: relative;_x000D_
display: inline-block;_x000D_
width: 300px;_x000D_
height: 200px;_x000D_
background-color: #ccc;_x000D_
}_x000D_
_x000D_
.dvInsideTL {_x000D_
position: absolute;_x000D_
left: 0;_x000D_
top: 0;_x000D_
width: 150px;_x000D_
height: 100px;_x000D_
background-color: #ff751a;_x000D_
opacity: 0.5;_x000D_
}
_x000D_
<div class="dvContainer">_x000D_
<table style="width:100%;height:100%;">_x000D_
<tr>_x000D_
<td style="width:50%;text-align:center">Top Left</td>_x000D_
<td style="width:50%;text-align:center">Top Right</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td style="width:50%;text-align:center">Bottom Left</td>_x000D_
<td style="width:50%;text-align:center">Bottom Right</td>_x000D_
</tr>_x000D_
</table>_x000D_
<div class="dvInsideTL">_x000D_
</div>_x000D_
</div>
_x000D_
I hope this helps,
Zag.
Source: Stackoverflow.com