How to make div go behind another div?
The reason you're getting so many different answers is because you've not explained what you want to do exactly. All the answers you get with code will be programmatically correct, but it's all down to what you want to achieve
.box-left-mini{
float:left;
background-image:url(website-content/hotcampaign.png);
width:292px;
height:141px;
}
.box-left-mini .front {
display: block;
z-index: 5;
position: relative;
}
.box-left-mini .front span {
background: #fff
}
.box-left-mini .behind_container {
background-color: #ff0;
position: relative;
top: -18px;
}
.box-left-mini .behind {
display: block;
z-index: 3;
}
<div class="box-left-mini">
<div class="front"><span>this is in front</span></div>
<div class="behind_container">
<div class="behind">behind</div>
</div>
</div>
You need to add z-index
to the divs, with a positive number for the top div and negative for the div below
.one {
position:absolute;
z-index:999;
}
.one {
position:absolute;
z-index:-999;
}
<div class="one">
this div is infront
</div>
<div class="two">
this div is behind
</div>
To answer the question in a general manner:
Using z-index
will allow you to control this. see z-index at csstricks.
The element of higher z-index
will be displayed on top of elements of lower z-index
.
For instance, take the following HTML:
<div id="first">first</div>
<div id="second">second</div>
If I have the following CSS:
#first {
position: fixed;
z-index: 2;
}
#second {
position: fixed;
z-index: 1;
}
#first
wil be on top of #second
.
But specifically in your case:
The div
element is a child of the div
that you wish to put in front. This is not logically possible.
One possible could be like this,
HTML
<div class="box-left-mini">
<div class="front">this div is infront</div>
<div class="behind">
this div is behind
</div>
</div>
CSS
.box-left-mini{
float:left;
background-image:url(website-content/hotcampaign.png);
width:292px;
height:141px;
}
.front{
background-color:lightgreen;
}
.behind{
background-color:grey;
position:absolute;
width:100%;
height:100%;
top:0;
z-index:-1;
}
http://jsfiddle.net/MgtWS/
But it really depends on the layout of your div elements i.e. if they are floating, or absolute positioned etc.