文档流的元素有被浮动元素和定位元素覆盖的可能
浮动元素:意味着脱离文档流,但是经过下面的测试,发下它的层级比开启了定位的元素的层级要低(不管是开启的哪种定位,下面一相对定位和绝对定位做测试)
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width:600px;
height:600px;
background-color: #bfa;
}
.box2{
width:300px;
height:300px;
background-color: pink;
float:left;
}
.box3{
width:200px;
height:200px;
background-color: red;
position:relative;
top:100px;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
<div class="box3"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width:600px;
height:600px;
background-color: #bfa;
}
.box2{
width:300px;
height:300px;
background-color: pink;
float:left;
}
.box3{
width:200px;
height:200px;
background-color: red;
position:relative;
top:100px;
}
</style>
</head>
<body>
<div class="box1">
<div class="box3"></div>
<div class="box2"></div>
</div>
</body>
</html>
要测试绝对定位和float的层级关系的话,可以直接将上面代码中的position中的relative修改为absolute就可以
由于不管是哪种定位,只要开启了定位,它们的层级就是一样的,于是,我们可以通过z-index来调节开启了定位元素的层级关系
需要注意的事
第一:父元素设置了浮动,其子元素不一定是浮动了(除非,该子元素设置了浮动)
第二:父元素设置了定位,其子元素不一定就是设置了定位(除非,该子元素设置了定位)
总结:父元素是否设置了浮动或者是定位,它不会传递给其子元素或者是后代元素