If I have a floated <div id='1' style='float:right;'>controls and links</div>
followed by a <div id='2' style='position:relative;'>lorem ipsum text here...</div>
, <div> #2 lays on top of the <div> #1, blocking any mouse interaction. (If <div> #2 has a solid background color, it completely hides <div> #1, but still "wraps" around <div> #1.) Why is this so?
This remains true even if I set z-index
values on both, trying to force the floated <div> #1 to the top.
There are four scenarios one can consider. I've put them all into this JSFiddle. (The JSFiddle also uses opacity to illustrate the stacking.)
- Floated <div> #1 has no z-index or position; the following <div> #2 also has no z-index or position. RESULT: <div> #1 is stacked on top of <div> #2, and mouse interaction is fine.
- Floated <div> #1 has no z-index or position; <div> #2 also has no z-index but has
position:relative
. RESULT: <div> #2 is stacked on top of <div> #1. No mouse interaction is possible. - Floated <div> #1 has
z-index:1000
but no position; the following<div>
#2 hasz-index:0
andposition:relative
. RESULT: <div> #2 remains stacked on top of <div> #1. No mouse interaction is possible. - Floated <div> #1 has
z-index:1000
andposition:relative
; the following <div> #2 hasz-index:0
andposition:relative
. RESULT: <div> #1 is stacked on top of <div> #2, and mouse interaction is fine.
I have seen some similar SO questions, but nothing that exactly addresses this. I've also read a number of CSS float and positioning articles, but none seem to address this scenario.