I have a div with default positioning (i.e. position:static) and a div with a fixed position.
If I set the z-index
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 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
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.
element is the root element and is the first stacking contextThe Stacking order and stacking context rules below are from this link
element)The order of elements:
element is the only stacking context by default, but any element can be a root element for a stacking context, see rules above)