Referring in this instance
I can not get the div with id="covered"
on top of the div with id="cover"
simply by using the z-index
.
Is there any other solution?
Code from http://codepen.io/anon/pen/jhgtf :
HTML:
<div id="cover"></div>
<div id="covered"></div>
CSS:
#cover{
position:fixed;
width:800px;
background:black;
height:350px;
z-index:10;
}
#covered{
width:80px;
background:yellow;
height:50px;
z-index:11;
}
By using a div with style z-index:1; and position: absolute; you can overlay your div on any other div . z-index determines the order in which divs 'stack'. A div with a higher z-index will appear in front of a div with a lower z-index . Note that this property only works with positioned elements.
Use the CSS z-index property. Elements with a greater z-index value are positioned in front of elements with smaller z-index values. Note that for this to work, you also need to set a position style ( position:absolute , position:relative , or position:fixed ) on both/all of the elements you want to order.
The root element forms the root stacking context. Other stacking contexts are generated by any positioned element (including relatively positioned elements) having a computed value of 'z-index' other than 'auto'. Stacking contexts are not necessarily related to containing blocks.
If you want the two div s to be displayed one above the other, the simplest answer is to remove the float: left; from the css declaration, as this causes them to collapse to the size of their contents (or the css defined size), and, well float up against each other.
You need to specify a position
on #covered
:
#cover{
position:fixed;
width:800px;
background:black;
height:350px;
z-index:10;
}
#covered{
position: fixed;
width:80px;
background:yellow;
height:50px;
z-index:11;
}
CodePen example
Elements with static
(default) positioning are not affected by z-index - only positioned elements with fixed
, relative
, or absolute
positioning are. Which of those you should use is dependent on context.
Official z-index specification
#covered{
position:relative;
width:80px;
background:yellow;
height:50px;
z-index:11;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With