I have a div with default positioning (i.e. position:static) and a div with a fixed position.
If I set the z-index
Add position: relative; to #over
#over {
width: 600px;
z-index: 10;
position: relative;
}
#under {
position: fixed;
top: 5px;
width: 420px;
left: 20px;
border: 1px solid;
height: 10%;
background: #fff;
z-index: 1;
}
<!DOCTYPE html>
<html>
<body>
<div id="over">
Hello Hello HelloHelloHelloHelloHello Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
</div>
<div id="under"></div>
</body>
</html>
Fiddle
z-index only works within a particular context i.e. relative, fixed or absolute position.
z-index for a relative div has nothing to do with the z-index of an absolutely or fixed div.
EDIT This is an incomplete answer. This answer provides false information. Please review @Dansingerman's comment and example below.
This question can be solved in a number of ways, but really, knowing the stacking rules allows you to find the best answer that works for you.
The <html> element is your only stacking context, so just follow the stacking rules inside a stacking context and you will see that elements are stacked in this order
- The stacking context’s root element (the
<html>element in this case)- Positioned elements (and their children) with negative z-index values (higher values are stacked in front of lower values; elements with the same value are stacked according to appearance in the HTML)
- Non-positioned elements (ordered by appearance in the HTML)
- Positioned elements (and their children) with a z-index value of auto (ordered by appearance in the HTML)
- Positioned elements (and their children) with positive z-index values (higher values are stacked in front of lower values; elements with the same value are stacked according to appearance in the HTML)
So you can
#under positioned -ve z-index appear behind non-positioned #over element#over to relative so that rule 5 applies to it Developers should know the following before trying to change the stacking order of elements.
<html> element is the root element and is the first stacking contextThe Stacking order and stacking context rules below are from this link
<html> element)The order of elements:
<html> element is the only stacking context by default, but any element can be a root element for a stacking context, see rules above)
I was building a nav menu. I have overflow: hidden in my nav's css which hid everything. I thought it was a z-index problem, but really I was hiding everything outside my nav.
Give the #under a negative z-index, e.g. -1
This happens because the z-index property is ignored in position: static;, which happens to be the default value; so in the CSS code you wrote, z-index is 1 for both elements no matter how high you set it in #over.
By giving #under a negative value, it will be behind any z-index: 1; element, i.e. #over.
the behaviour of fixed elements (and absolute elements) as defined in CSS Spec:
They behave as they are detached from document, and placed in the nearest fixed/absolute positioned parent. (not a word by word quote)
This makes zindex calculation a bit complicated, I solved my problem (the same situation) by dynamically creating a container in body element and moving all such elements (which are class-ed as "my-fixed-ones" inside that body-level element)