定位
2018-07-10 本文已影响0人
WANGLIN_HZ
固定定位
<!DOCTYPE html>
<html>
<head>
<title>固定定位</title>
<style type="text/css">
.box1{
width: 50px;
height: 50px;
background-color: red
}
.box2{
width: 80px;
height: 80px;
background-color: rgb(177,199,210);
position: fixed;
left: 0px;
top: 0px;
}
.box3{
width: 100px;
height: 100px;
background-color: rgb(210,219,222);
}
.box4{
width: 200px;
height: 200px;
background-color: yellow;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2"></div>
</div>
<div class="box3"></div>
<div class="box4"></div>
</body>
</html>
- 固定定位的元素会被锁定在屏幕的某个位置上,当 访问者滚动网页时,固定元素会在屏幕上保持不动。
- 当将position属性设置为fixed时,则开启了元素的 固定定位。
- 当开启了固定定位以后,可以使用top、right、
bottom、left四个属性对元素进行定位。 - 固定定位的其他特性和绝对定位类似。
相对定位
<!DOCTYPE html>
<html>
<head>
<title>相对定位</title>
<style type="text/css">
.box{
width: 200px;
height: 200px;
background-color: red;
}
.box2{
width: 200px;
height: 200px;
background-color: yellow;
/*margin-left: 200px;*/
/*margin-top: 200px;*/
position: relative;
left: 100px;
}
.box3{
width: 200px;
height: 200px;
background-color: rgb(122,133,144);
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
- 每个元素在页面的文档流中都有一个自然位置。相 对于这个位置对元素进行移动就称为相对定位。周 围的元素完全不受此影响。
- 当将position属性设置为relative时,则开启了元素 的相对定位。
- 当开启了相对定位以后,可以使用top、right、 bottom、left四个属性对元素进行定位。
绝对定位
- 绝对定位指使元素相对于html元素或离他最近 的祖先定位元素进行定位。
- 当将position属性设置为absolute时,则开启 了元素的绝对定位。
- 当开启了绝对定位以后,可以使用top、right、 bottom、left四个属性对元素进行定位。