(注1:如果有問題歡迎留言探討,一起學習!轉載請注明出處,喜歡可以點個贊哦!)
(注2:更多內容請查看我的目錄。)
1. 簡介
我們提到過css的定位機制。正常不作處理的html元素遵循普通文檔流,但是有些定位方式會讓這些元素脫離文檔流。那么這些脫離的文檔流如果與其他元素形成重疊,誰能夠占據優勢呢?
2. 普通文檔流與脫離普通文檔流
可以分為三類:
- 元素若不指定,使用默認定位static,屬于普通文檔流。
- 指定使用realtive定位,可以相對在普通文檔流中的位置進行偏移,但仍然屬于普通文檔流。
- 指定使用absolute, fixed定位或者float浮動,會使元素脫離普通文檔流。
3. 定位之間的覆蓋
3.1 相同定位之間的覆蓋
3.1.1 static與static
這種情況,在普通文檔流中,無覆蓋。
3.1.2 relative與relative
<!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>
由圖看出,relative之間后者覆蓋前者
3.1.3 absolute之間(fixed與absolute只是定位的參考對象不一樣,但是覆蓋的優先級一樣,故在此將其合并為一個討論)
<!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>
從圖中可以看出,absolute,fixed同級且后者覆蓋前者。
3.1.4 float之間的覆蓋
<!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>
可以看出,利用float,元素排滿一行以后會自動換行,不會覆蓋。
3.2 不同定位之間的覆蓋
3.2.1 static與relative
<!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>
可以看出,relative覆蓋static
3.2.2 static與absolute
<!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>
可以看出,static被absolute覆蓋。
3.2.3 static與float
<span class="float-l">left</span>
由圖可以看出,static被float覆蓋,但是不會覆蓋其內容。
3.2.4 relative與absolute
<!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>
由圖看出,relative與absolute是后來者居上。
3.2.5 relative與float
<!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>
右圖可以看出,relative覆蓋float。
3.2.6 absolute與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>
由圖可以看出,absolute覆蓋float。
4. z-index
前面我們看到的是先后順序和定位類型對覆蓋的影響,那么有沒有一種更靈活的方式來決定元素的覆蓋關系呢。答案是有的,就是z-index。但是z-index只對relative,absolute和fixed生效,對static和float無效。
4.1 z-index對定位元素的作用
<!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>
可以看到,z-index對定位元素中的static無效,對于其余定為元素,z-index
默認值為0,可以設置為任意整數(注意,設置為小數不會生效),z-index越大,元素層級越高。
4.2 z-index對float的作用
在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>
可以對比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盒內最上面的一張紙更高。
5. 總結
- 相同定位(relative,absolute,fixed)之間按先后順序后者覆蓋前者。
- 不同定位之間static < float < relative,absolute,fixed
- z-index能影響relative,absolute,fixed的層級,越大越靠上,相同時按先后順序后者覆蓋前者。
- 比較z-index時,兄弟之間直接比較大小,非兄弟比較其祖先元素。
參考
07 CSS-相對定位、絕對定位、固定定位、z-index
CSS相對定位|絕對定位(五)之z-index篇
深刻解析position屬性以及與層(z-index)的關系
深入理解css中position屬性及z-index屬性