61,112
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
有三种布局:1. 行布局(默认布局方式);2. 列布局(使用float);3. 定位布局(使用position)
在定位布局中,需要搞清楚两个问题:1. 目标;2. 参照物。
当使用绝对定位时,必须指定参照物,然后进行定位;
而成为参照物的条件是:1. 是目标元素的父元素或祖先元素;2. position属性的值不能为static(默认值)。
成为参照物的元素,其position属性值可以是 absolute、relative、fixed;
其中relative是最常见的,
因为将元素的position属性设置为relative,
不会使该元素从标准的文档流脱离出来(也就是没什么影响)。
<style type="text/css">
.relObj { position: relative; }
.absObj { position: absolute; }
.container {width: 300px; height: 200px; border: solid 1px gray; }
.closeBtn {color: red; font-weight: bold; cursor: pointer;
top: 1px; right: 1px; }
</style>
<div class="container relObj">
<div class="closeBtn absObj">X</div>
</div>
</body>
</html>