版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
CSS中的相对定位与绝对定位
在CSS盒子模型学习的过程中会经常用到绝对定位与相对定位,我们用它来确定盒子的位置。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>相对定位与绝对定位</title>
<style type="text/css">
.f{
margin-left: 200px;
width: 150px;
height: 150px;
padding: 10px;
background: grey;
border: 1px solid #000;
}
.one{width: 50px;height: 20px;background: yellow;border: 1px solid #000;margin:10px 0;text-align: center;}
/*使相对定位与绝对定位的偏移量相等*/
.two{
width: 50px;height: 20px;border: 1px solid #000;margin:10px 0;text-align: center;
/*相对定位*/
position: relative;
left: 80px;
top: 60px;
background:yellowgreen;
}
.three{
width: 50px;height: 20px;border: 1px solid #000;margin:10px 0;text-align: center;
/*绝对定位*/
position: absolute;
left: 80px;
top: 60px;
background:pink;
}
</style>
</head>
<body>
<div class="f">
<div class="one">第一个</div>
<div class="two">第二个</div>
<div class="three">第三个</div>
</div>
</body>
</html>
可以看出相对定位的实现必须得有一个参照物,而绝对定位没有这个限制。