CSS 定位是用于控制元素在页面上的位置和布局的重要机制。以下我将列出position
属性的不同值,以及top
、left
、right
、bottom
和z-index
属性,并给出相应的示例代码。
1. position: static(默认)
static
是position
属性的默认值,元素按照正常的文档流进行布局,top
、left
、right
、bottom
和z-index
属性对其无效。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Static Positioning</title>
<style>
.static-box {
background-color: lightblue;
width: 100px;
height: 100px;
position: static;
top: 50px; /* 无效 */
left: 50px; /* 无效 */
}
</style>
</head>
<body>
<div class="static-box"></div>
</body>
</html>
2. position: relative(绝对定位)
relative
定位的元素会相对于其正常位置进行偏移。top
、left
、right
、bottom
属性可以用来指定偏移量,并且元素仍然会占据原来的空间。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Relative Positioning</title>
<style>
.relative-box {
background-color: lightgreen;
width: 100px;
height: 100px;
position: relative;
top: 20px;
left: 20px;
}
</style>
</head>
<body>
<div class="relative-box"></div>
</body>
</html>
3. position: absolute(相对定位)
absolute
定位的元素会相对于最近的已定位祖先元素(即position
值不为static
的祖先元素)进行定位。如果没有已定位的祖先元素,则相对于<html>
元素。元素脱离正常文档流,不再占据原来的空间。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Absolute Positioning</title>
<style>
.parent {
position: relative;
width: 200px;
height: 200px;
background-color: lightgray;
}
.absolute-box {
background-color: lightcoral;
width: 50px;
height: 50px;
position: absolute;
top: 20px;
right: 20px;
}
</style>
</head>
<body>
<div class="parent">
<div class="absolute-box"></div>
</div>
</body>
</html>
4. position: fixed(固定定位)
fixed
定位的元素会相对于浏览器窗口进行定位,无论页面如何滚动,元素的位置都不会改变。元素脱离正常文档流。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fixed Positioning</title>
<style>
.fixed-box {
background-color: lightyellow;
width: 100px;
height: 100px;
position: fixed;
bottom: 20px;
right: 20px;
}
p {
height: 2000px;
}
</style>
</head>
<body>
<div class="fixed-box"></div>
<p>滚动页面查看效果</p>
</body>
</html>
5. position: sticky(粘性定位)
sticky
定位的元素会在正常文档流中滚动,直到达到指定的阈值(由top
、left
、right
、bottom
属性指定),然后固定在该位置。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sticky Positioning</title>
<style>
.sticky-box {
background-color: lightpink;
width: 100%;
height: 50px;
position: sticky;
top: 0;
}
p {
height: 2000px;
}
</style>
</head>
<body>
<div class="sticky-box"></div>
<p>滚动页面查看效果</p>
</body>
</html>
6. z-index
属性
z-index
属性用于控制元素的堆叠顺序,值越大的元素会显示在值较小的元素之上。只有定位元素(position
值不为static
)的z-index
属性才有效。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Z-index Example</title>
<style>
.box1 {
background-color: lightblue;
width: 100px;
height: 100px;
position: relative;
top: 20px;
left: 20px;
z-index: 1;
}
.box2 {
background-color: lightgreen;
width: 100px;
height: 100px;
position: relative;
top: -20px;
left: 40px;
z-index: 2;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>