<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box{
width: 300px;
height: 300px;
background-color: #f00;
}
.inner{
width: 100px;
height: 100px;
background-color: yellowgreen;
}
span{
position: relative;
background-color: orange;
left: 100px;
z-index: 1;
}
strong{
position: relative;
background-color: green;
}
.box1{
display: inline-block;
width: 200px;
height: 200px;
background-color: blueviolet;
}
</style>
</head>
<body>
<!--
z-index属性用来设置定位元素的层叠顺序(仅对定位元素有效)
取值可以是正整数,负整数,0
z-index默认值为auto
比较原则:
如果是兄弟关系
z-index越大,层叠在越上面
z-index相等,写在后面的那个元素层叠在上面
如果不是兄弟关系
各自从元素自己以及祖先元素中,找出最邻的2个定位元素进行比较
而且这2个定位元素必须设置z-index的值
-->
<div class="box">
<div class="inner"></div>
</div>
<span>hahahha</span>
<strong>heheheh</strong>
<div class="box1">
</div>
</body>
</html>
css中z-index设置定位元素的层叠关系
最新推荐文章于 2022-04-09 17:02:28 发布