-
固定定位(Fixed Positioning):
-
静态定位(Static Positioning):
-
粘性定位(Sticky Positioning):
示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{ margin: 0; /* 清楚页面默认的外边距 */ }
p{
width: 100px; /* 设置宽 */
height: 50px; /* 设置高 */
margin: 0; /* 清楚p元素默认的外边距 */
}
.block1{
background-color: red; /* 设置背景色 */
position: fixed; /* 固定定位 */
top: 0; /* 距离页面顶部距离 */
left: 150px; /* 距离页面左侧距离 */
}
.block2{
background-color: blue; /* 设置背景色 */
position: static; /* 固定定位 */
}
.block3{
background-color: yellow; /* 设置背景色 */
position: sticky; /* 固定定位 */
left: 0; /* 距离页面左侧距离 */
right: 0; /* 距离页面右侧距离 */
}
</style>
</head>
<body>
<p class="block1">1-固定定位</p>
<p class="block2">2-静态定位</p>
<p class="block3">3-粘性定位</p>
</body>
</html>
图例: