(注1:如果有问题欢迎留言探讨,一起学习!转载请注明出处,喜欢可以点个赞哦!)
(注2:更多内容请查看我的目录。)
我们提到过css的定位机制。正常不作处理的html元素遵循普通文档流,但是有些定位方式会让这些元素脱离文档流。那么这些脱离的文档流如果与其他元素形成重叠,谁能够占据优势呢?
可以分为三类:
这种情况,在普通文档流中,无覆盖。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test3.1.2</title>
<style>
.relative1 {
background-color: blue;
position: relative;
top: 15px;
}
.relative2 {
background-color: green;
position: relative;
top: 0px;
}
.relative3 {
background-color: red;
position: relative;
top: 0px;
}
.relative4 {
background-color: yellow;
position: relative;
top: -15px;
}
</style>
</head>
<body>
<div class="relative1">relative1</div>
<div class="relative2">relative2</div>
<div class="relative3">relative3</div>
<div class="relative4">relative4</div>
</body>
</html>
3.1.2
由图看出,relative之间后者覆盖前者
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test3.1.3</title>
<style>
.relative {
position: relative;
}
.absolute1 {
background-color: blue;
position: absolute;
top: 20px;
}
.absolute2 {
background-color: green;
position: absolute;
top: 20px;
}
.absolute3 {
background-color: red;
position: absolute;
top: 40px;
}
.absolute4 {
background-color: yellow;
position: absolute;
top: 40px;
}
.fixed1 {
background-color: black;
position: fixed;
top: 20px;
}
.fixed2 {
background-color: purple;
position: fixed;
top: 20px;
}
.fixed3 {
background-color: black;
position: fixed;
top: 50px;
}
.fixed4 {
background-color: purple;
position: fixed;
top: 50px;
}
</style>
</head>
<body>
<div calss="relative">
<div class="fixed1">fixed1</div>
<div class="fixed2">fixed2</div>
<div class="absolute1">absolute1</div>
<div class="absolute2">absolute2</div>
</div>
<div class="relative">
<div class="absolute3">absolute3</div>
<div class="absolute4">absolute4</div>
<div class="fixed3">fixed3</div>
<div class="fixed4">fixed4</div>
</div>
</body>
</html>
3.1.3
从图中可以看出,absolute,fixed同级且后者覆盖前者。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test3.1.4</title>
<style>
.float-l {
float: left;
}
.float-r {
float: right;
}
</style>
</head>
<body>
<div class="float-l">left</div>
<div class="float-l">left</div>
<div class="float-l">left</div>
<div class="float-l">left</div>
<div class="float-l">left</div>
<div class="float-l">left</div>
<div class="float-r">right</div>
<div class="float-r">right</div>
<div class="float-r">right</div>
<div class="float-r">right</div>
<div class="float-r">right</div>
<div class="float-r">right</div>
</body>
</html>
3.1.4-1
3.1.4-2
3.1.4-3
可以看出,利用float,元素排满一行以后会自动换行,不会覆盖。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test3.2.1</title>
<style>
.static {
background-color: blue;
}
.relative1 {
background-color: green;
position: relative;
top: -20px;
}
.relative2 {
background-color: green;
position: relative;
top: 20px;
}
</style>
</head>
<body>
<div class="static">static1</div>
<div class="relative1">relative1</div>
<div class="relative2">relative2</div>
<div class="static">static2</div>
</body>
</html>
3.2.1
可以看出,relative覆盖static
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test3.2.2</title>
<style>
.static {
background-color: blue;
}
.absolute1 {
background-color: green;
position: absolute;
top: 20px;
}
.absolute2 {
background-color: red;
position: absolute;
top: 40px;
}
</style>
</head>
<body>
<div class="static">static1</div>
<div class="absolute1">absolute1</div>
<div class="absolute2">absolute2</div>
<div class="static">static2</div>
</body>
</html>
3.2.2
可以看出,static被absolute覆盖。
<span class="float-l">left</span>
3.2.3-1
3.2.3-2
由图可以看出,static被float覆盖,但是不会覆盖其内容。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test3.2.4</title>
<style>
.relative1 {
background-color: green;
position: relative;
top: 20px;
}
.relative2 {
background-color: green;
position: relative;
top: 20px;
}
.absolute1 {
background-color: blue;
position: absolute;
top: 20px;
}
.absolute2 {
background-color: blue;
position: absolute;
top: 60px;
}
</style>
</head>
<body>
<div class="absolute1">absolute1</div>
<div class="relative1">relative1</div>
<div class="relative2">relative2</div>
<div class="absolute2">absolute2</div>
</body>
</html>
3.2.4
由图看出,relative与absolute是后来者居上。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test3.2.5</title>
<style>
.relative1 {
background-color: blue;
position: relative;
left: -10px;
}
.relative2 {
background-color: blue;
position: relative;
left: 30px;
}
.float-l {
background-color: green;
float: left;
}
.float-r {
background-color: green;
float: right;
}
</style>
</head>
<body>
<span class="relative1">relative11111111111</span>
<span class="float-l">left</span>
<span class="float-r">right</span>
<span class="relative2">relative222222222</span>
</body>
</html>
3.2.5
右图可以看出,relative覆盖float。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test3.2.6</title>
<style>
.absolute1 {
background-color: blue;
position: absolute;
left: 30px;
}
.absolute2 {
background-color: blue;
position: absolute;
left: 200px;
}
.float-l {
background-color: green;
float: left;
}
.float-r {
background-color: green;
float: right;
}
</style>
</head>
<body>
<span class="absolute1">absolute11111111111</span>
<span class="float-l">left</span>
<span class="float-r">right</span>
<span class="absolute2">absolute222222222</span>
</body>
</html>
3.2.6
由图可以看出,absolute覆盖float。
前面我们看到的是先后顺序和定位类型对覆盖的影响,那么有没有一种更灵活的方式来决定元素的覆盖关系呢。答案是有的,就是z-index。但是z-index只对relative,absolute和fixed生效,对static和float无效。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test4.1</title>
<style>
.static1 {
position: static;
background-color: pink;
}
.relative1 {
background-color: blue;
position: relative;
top: -7px;
}
.absolute1 {
background-color: red;
position: absolute;
top: 20px;
}
float1 {
background-color: green;
float: left;
}
.static2 {
position: static;
background-color: pink;
z-index: 999;
}
.relative2 {
background-color: blue;
position: relative;
top: -7px;
z-index: 888;
}
.absolute2 {
background-color: red;
position: absolute;
top: 60px;
z-index: 777;
}
</style>
</head>
<body>
<div class="static1">static1</div>
<div class="relative1">relative1</div>
<div class="absolute1">absolute1</div>
<div class="static2">static2</div>
<div class="relative2">relative2</div>
<div class="absolute2">absolute2</div>
</body>
</html>
4.1
可以看到,z-index对定位元素中的static无效,对于其余定为元素,z-index
默认值为0,可以设置为任意整数(注意,设置为小数不会生效),z-index越大,元素层级越高。
在3.2.6的代码中加入z-index,看一下是否会有作用。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test4.2</title>
<style>
.absolute1 {
background-color: blue;
position: absolute;
left: 30px;
z-index: 666;
}
.absolute2 {
background-color: blue;
position: absolute;
left: 200px;
z-index:666;
}
.float-l {
background-color: green;
float: left;
z-index:999;
}
.float-r {
background-color: green;
float: right;
z-index:999;
}
</style>
</head>
<body>
<span class="absolute1">absolute11111111111</span>
<span class="float-l">left</span>
<span class="float-r">right</span>
<span class="absolute2">absolute222222222</span>
</body>
</html>
4.2
可以对比3.2.6看出z-index对于float无效。
另外,z-index对元素层级的影响是依赖于其祖先元素的,把页面看做一层层的盒子叠加而成的话,如果A盒的z-index比B盒大,那么A盒会在B盒之上,此时即使A盒内的元素a的z-index比B盒内的元素b的z-index小,a也会在b之上。这一点其实很容易理解,因为A盒内即使放在最下面的一张纸其实也是比B盒内最上面的一张纸更高。