<!DOCTYPE html>
<html>
<head>
<title>HTML 定位示例</title>
<style>
/* 使用 position 属性进行定位 */
.box {
width: 200px;
height: 200px;
background-color: lightblue;
position: relative; /* 相对定位 */
top: 30px;
left: 30px;
}
.absolute-box {
width: 100px;
height: 100px;
background-color: lightcoral;
position: absolute; /* 绝对定位 */
top: 50px;
left: 50px;
}
.fixed-box {
width: 150px;
height: 150px;
background-color: lightgreen;
position: fixed; /* 固定定位 */
bottom: 20px;
right: 20px;
}
</style>
</head>
<body>
<div class="box">
这是一个相对定位的盒子。
<div class="absolute-box">
这是一个绝对定位的盒子。
</div>
</div>
<div class="fixed-box">
这是一个固定定位的盒子。
</div>
</body>
</html>
相对定位 (position: relative;
):
top: 30px;
和 left: 30px;
表示盒子从它的原始位置向右和向下各移动了 30 像素。绝对定位 (position: absolute;
):
static
)祖先元素进行偏移。如果没有这样的祖先元素,则相对于初始包含块(通常是浏览器窗口)。top: 50px;
和 left: 50px;
表示盒子从它的最近已定位祖先元素的左上角向右和向下各移动了 50 像素。固定定位 (position: fixed;
):
bottom: 20px;
和 right: 20px;
表示盒子从浏览器窗口的右下角向左和向上各移动了 20 像素。上一篇:padding是什么意思html
下一篇:html图片标签
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站