<!DOCTYPE html>
<html>
<head>
<style>
/* position 属性用于指定元素的定位方式 */
.relative {
position: relative;
/* 相对定位:相对于其正常位置进行定位 */
left: 30px;
top: 20px;
background-color: lightblue;
}
.absolute {
position: absolute;
/* 绝对定位:相对于最近的已定位祖先元素,如果不存在,则相对于初始包含块(通常是浏览器窗口) */
top: 50px;
right: 0;
background-color: lightcoral;
}
.fixed {
position: fixed;
/* 固定定位:相对于浏览器窗口进行定位,即使页面滚动也保持在固定位置 */
bottom: 10px;
right: 10px;
background-color: lightgreen;
}
.sticky {
position: sticky;
/* 粘性定位:根据用户的滚动位置来定位。当页面滚动超出目标区域时,会根据设置的 top, bottom, left, right 值相对最近的已定位祖先元素进行定位 */
top: 10px;
background-color: lightyellow;
}
</style>
</head>
<body>
<div class="relative">相对定位</div>
<div style="height: 200px;"></div> <!-- 占位符,以便观察绝对定位的效果 -->
<div class="absolute">绝对定位</div>
<div style="height: 600px; background: #f0f0f0;">滚动此区域以查看固定和粘性定位的效果</div>
<div class="fixed">固定定位</div>
<div class="sticky">粘性定位</div>
</body>
</html>
上一篇:html 横线
下一篇:html中背景图片怎么设置
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站