CSS
z-index 层叠规则
如果对 z-index(层叠样式表)是什么还不太自信,可以看 MDN 使用 z-index 常见的问题 为什么我设置了 z-index:9999,或者 z-index:-9999,但是没生效 设置了定位属性,也设置 float 属性,怎么 z-index 就出问题了 z-index:0,z-index:1,z-index auto 有什么区别 定义一些概念 定位元素:设置了 position:re…
如果对 z-index(层叠样式表)是什么还不太自信,可以看 MDN 使用 z-index 常见的问题 为什么我设置了 z-index:9999,或者 z-index:-9999,但是没生效 设置了定位属性,也设置 float 属性,怎么 z-index 就出问题了 z-index:0,z-index:1,z-index auto 有什么区别 定义一些概念 定位元素:设置了 position:re…
如果对 z-index(层叠样式表)是什么还不太自信,可以看MDN
对于全都是非定位元素,文档流后面的 DOM 元素会覆盖前面的 DOM 元素
* {
margin: 0;
padding: 0;
}
.box {
width: 50px;
height: 50px;
border: solid;
display: inline-block;
}
.red {
background: red;
}
.green {
background: green;
}
.box1 {
margin-left: 0;
}
.box2 {
margin-left: -50px;
}
<div class="box red box1"></div>
<div class="box green box2"></div>
* {
margin: 0;
padding: 0;
}
.box {
width: 50px;
height: 50px;
border: solid;
display: inline-block;
}
.red {
background: red;
}
.green {
background: green;
}
.box1 {
position: relative;
left: 50px;
}
.box2 {
}
<div class="box red box1"></div>
<div class="box green box2"></div>
* {
margin: 0;
padding: 0;
}
.box {
width: 50px;
height: 50px;
border: solid;
display: inline-block;
}
.red {
background: red;
}
.green {
background: green;
}
.yellow {
background: yellow;
}
.blue {
background: blue;
}
.box1 {
position: absolute;
z-index: 1;
}
.box2 {
position: absolute;
z-index: 0;
}
.box1child {
z-index: -99999;
position: absolute;
width: 20px;
height: 20px;
}
<div class="box1 red box">
<div class="box1child yellow"></div>
</div>
<div class="box2 green box"></div>
定位元素,且 z-index 是整数,会被放置到与 dom 元素不一样的层级树中
* {
margin: 0;
padding: 0;
}
.box {
width: 50px;
height: 50px;
border: solid;
display: inline-block;
}
.red {
background: red;
}
.green {
background: green;
}
.yellow {
background: yellow;
}
.blue {
background: blue;
}
.box1 {
position: relative;
z-index: 2;
left: 50px;
}
.box2 {
position: relative;
}
.box1child {
z-index: 0;
position: relative;
width: 20px;
height: 20px;
}
.box2child {
z-index: 1;
position: relative;
right: 100px;
width: 200px;
height: 20px;
}
box1 和 box2child 会被放置与 dom 元素不一样的层级树中,且 box1 的层级大于 box2child
box1 和 box2child 在同一个层级树中,且 box1 大于 box2child,所以 box1 的父子元素都比 box2 的父子元素层级高
<div class="box1 red box">
<div class="box1child yellow"></div>
</div>
<div class="box2 green box">
<div class="box2child blue"></div>
</div>

* {
margin: 0;
padding: 0;
}
.box {
width: 50px;
height: 50px;
border: solid;
display: inline-block;
}
.red {
background: red;
}
.green {
background: green;
}
.yellow {
background: yellow;
}
.blue {
background: blue;
}
.box1 {
position: relative;
}
.box2 {
position: relative;
}
.box3 {
position: relative;
}
.box1child {
position: relative;
}
.box2child {
position: relative;
}
.box3child {
position: relative;
}
.box1childchild {
position: relative;
width: 170px;
height: 20px;
margin-top: 10px;
background: bisque;
z-index: 100;
}
.box2childchild {
position: relative;
width: 40px;
height: 40px;
margin-top: 5px;
background: greenyellow;
z-index: 10;
}
.box3childchild {
position: relative;
width: 40px;
height: 50px;
background: palevioletred;
z-index: 1;
}
body 下第一层含有定位元素整数 z-index,会被放置到与 dom 元素不一样的层级树中
box1childchild、box2childchild、box3childchild 三者在同一层级树,所以三者的层级关系是 box1childchild>box2childchild>box3childchild
<div class="box1 red box">
<div class="box1child">
<div class="box1childchild"></div>
</div>
</div>
<div class="box2 green box">
<div class="box2child">
<div class="box2childchild"></div>
</div>
</div>
<div class="box3 yellow box">
<div class="box3child">
<div class="box3childchild"></div>
</div>
</div>

定位>浮动
* {
margin: 0;
padding: 0;
}
.box {
width: 50px;
height: 50px;
border: solid;
display: inline-block;
}
.red {
background: red;
}
.green {
background: green;
}
.yellow {
background: yellow;
}
.blue {
background: blue;
}
.box1 {
position: relative;
right: 50px;
}
.box2 {
position: relative;
right: 80px;
}
.box3 {
float: left;
}
<div class="box1 red box"></div>
<div class="box2 green box"></div>
<div class="box3 yellow box"></div>
暂无评论
确认操作
请再次确认编辑图片
待上传图片 (剩余 0 张)
项目详情
确认评论邮箱
邀请制评论