css - Why position: relative without z-index creates a new stacking context? -


from article:

two elements same stack level layered based on source order. successive elements stack on top of predecessors.

and this:

a stacking context formed, anywhere in document, element in following scenarios: ... 2. element position value "absolute" or "relative" and z-index value other "auto".

now please @ piece of code:

.c1 {    position: relative;    top: 50px;  }      .c2 {     background: green;     width: 200px;     height: 200px;   }
<div class="c1">    why i'm visible?  </div>     <div class="c2">&nbsp;</div>

as follows 2 previous quotes, div.c1 shouldn't create new stacking context, because doesn't have z-index. therefore, div.c1 , div.c2 in <html> stacking context, , should rendered in order. why div.c2 rendered under div.c1?

here c1 has got stacking context since has position:relative in it, if give position absolute c2 both divs @ same level. layered on source order.

.c1 {    position:relative;    background:blue;    top: 50px;  }      .c2 {    position:absolute;     background: green;     width: 100px;     height: 100px;   }
<div class="c1">    i'm visible?  </div>     <div class="c2">&nbsp;</div>


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -