div {
width: 100px;
height: 100px;
position: absolute;
background: rgba(0,0,0,0.5);
}
<div style="top:0; left:0; background: red;"></div> <div style="top:1em; left:1em; background: blue;"></div> <div style="top:2em; left:2em; background: green;"></div>
にじゅうきゅうおくきゅうせんきゅうじゅうきゅうまんきゅうせんきゅうひゃくきゅうじゅうきゅう
2147483647 = 2^31 – 1 = 0×7FFFFFFFで符号付き 32bit 整数の最大値
※ただし過去にはもっと低いこともありました。(Safari3)
div {
width: 100px;
height: 100px;
position: absolute;
background: rgba(0,0,0,0.5);
}
<div style="z-index: 2147483647; top:0; left:0; background: red;"></div> <div style="z-index: 2999999999; top:1em; left:1em; background: blue;"></div> <div style="z-index: 2147483647; top:2em; left:2em; background: green;"></div>
とりあえず999なら一番上だよねー
について知る必要がある。
イラレでいうグループみたいなもの
.z1 { z-index: 1; }
.z2 { z-index: 2; }
.z3 { z-index: 3; }
.z4 { z-index: 4; }
div {
position: absolute;
width: 100px;
height: 100px;
}
<div class="z2" style="top: 5em; left: 5em; background: green;">
<div class="z4" style="top: 1em; left: 1em; background: gold;"></div>
</div>
<div class="z1" style="top: 1em; left: 1em; background: red;">
<div class="z3" style="top: 1em; left: 1em; background: blue;"></div>
</div>
//イメージ ┣.z2 ┃┗.z4 ┗.z1 ┗.z3
.z1 { z-index: 1; }
.z2 { z-index: 2; }
.z3 { z-index: 3; }
.z4 { z-index: 4; }
div {
position: absolute;
width: 100px;
height: 100px;
}
<div class="z2" style="top: 5em; left: 5em; background: green;">
<div class="z4" style="top: 1em; left: 1em; background: gold;"></div>
</div>
<div style="top: 1em; left: 1em; background: red;">
<div class="z3" style="top: 1em; left: 1em; background: blue;"></div>
</div>
//イメージ ┣.z2 ┃┗.z4 ┗.z3
.box {
position: relative;
/*z-indexを指定しないが、z-index:auto;扱い*/
width: 100px; height: 100px;
background: tomato;
}
.box:before,
.box:after {
content: "";
position: absolute;
z-index: -1;
bottom: 4px;
width: 50px; height: 20px;
box-shadow: 0 2px 10px #000;
}
.box:before {
left: 10px;
-webkit-transform: rotate(-5deg);
}
.box:after {
right: 10px;
-webkit-transform: rotate(5deg);
}
.box {
position: relative;
z-index: 1; /*なんかの理由で指定した*/
width: 100px; height: 100px;
background: tomato;
}
.box:before,
.box:after {
content: "";
position: absolute;
z-index: -1;
bottom: 4px;
width: 50px; height: 20px;
box-shadow: 0 2px 10px #000;
}
.box:before {
left: 10px;
-webkit-transform: rotate(-5deg);
}
.box:after {
right: 10px;
-webkit-transform: rotate(5deg);
}
.parent { position: relative; }
.icon {
position: absolute;
top: -5px; left: -5px;
width: 50px; height: 50px;
background: blue;
}
.img {
width: 100px; height: 100px;
background: red;
}
.img:hover { opacity: 0.5; }
<div class="parent">
<div class="icon"></div>
<div class="img"></div>
</div>
適切なz-indexを指定する
.parent {
position: relative;
}
.icon {
position: absolute;
top: -5px; left: -5px;
width: 50px; height: 50px;
background: blue;
z-index: 1;
}
.img {
width: 100px; height: 100px;
background: red;
}
.img:hover {
opacity: 0.5;
/*z-index: 0;扱い*/
}
を理解すればz-indexに100000とか10000000とかを付ける理由がなくなる。