CSS中的绝对定位和相对定位
总结
相对定位
解析:
先写3个div,然后分别指定大小和颜色,分别命名为box1~3,中间那个box2指定相对定位(relative)
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>RelativePositon</title>
<style type="text/css">
.box1{
height: 100px;
width: 100px;
background-color: blue;
}
.box2{
height: 100px;
width: 100px;
background-color: yellow;
position: relative;
left: 100px;
top: 100px;
}
.box3{
height: 100px;
width: 100px;
background-color: green;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
运行截图:
绝对定位
解析:
先写4个div, 然后分别指定大小和颜色,分别命名为box1~4, box2放在box4中,box2开启绝对定位(absolute),box4开启相对定位(relative)
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AbsolutePostiion</title>
<style type="text/css">
.box1{
width: 200px;
height: 200px;
background-color: blue;
}
.box2{
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
left: 50px;
top: 50px;
}
.box3{
width:200px;
height: 200px;
background-color: green;
}
.box4{
width: 200px;
height: 200px;
background-color: red;
position: relative;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box4">
<div class="box2"></div>
</div>
<div class="box3"></div>
</body>
</html>