一.定位的分类:
1.相对定位 position:relative;
2.绝对定位 position:absolute;
3.固定定位 position:fixed;
相对定位:
1.参考点:自己
2.不脱离标准文档流,老家留坑,形影分离
3.用途:微调元素,做绝对定位的参考(子绝父相)
注意:要学会计算盒子的实际高度和宽度
![](https://i-blog.csdnimg.cn/blog_migrate/19df87860758522346ca47b5f5299495.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f41f0dc7906c4dcab28d8451c60f38a8.png)
绝对定位:
- 绝对定位脱标
- 参考点 1.用top描述,参考点是页面的左上角,而不是浏览器的左上角; 2.用bottom描述,参考点是浏览器首屏窗口的尺寸,对应的页面的左下角;3.以盒子为参考点:一个绝对定位的元素,如果父亲元素中也出现了定位的 元素(可以是任何定位),那么将以父辈祖先这个元素作为参考点 。(如果是有定位的祖先元素有边框border,那么将以border内侧以参考点,另外绝对定位的儿子,无视参考的那个盒子的padding)
参考点:top
![](https://i-blog.csdnimg.cn/blog_migrate/e3d0204005489606dd6598ef2ca0771b.png)
参考点:bottom
![](https://i-blog.csdnimg.cn/blog_migrate/da937828441e14a00fdbf3dbc7b1bcd7.png)
参考点:盒子
![](https://i-blog.csdnimg.cn/blog_migrate/6ced6118fc77f79be05b534bb2759e01.png)
固定定位:
1.参考点:相对浏览器的窗口定位。页面如何滚动,这个盒子的位置显示 都不变。
2.脱离标准文档流
3.IE6不兼容
现在的位置
![](https://i-blog.csdnimg.cn/blog_migrate/c3a7edcfd8a0787e554a252e8cde6f21.png)
滚动后的位置
![](https://i-blog.csdnimg.cn/blog_migrate/ef7bba1ccfcc0f8024bf0e6078153718.png)
附上本人用定位做的布局图
![](https://i-blog.csdnimg.cn/blog_migrate/26477684f60d9efe112b3b285ab9f059.png)
附上HTML代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<style type="text/css">
body{
margin: 0;
padding: 0;
}
.father{
width: 970px;
height: 593px;
margin: 200px auto;
}
nav{
width: 970px;
height: 113px;
/*border: 1px solid red;*/
position: relative;
}
.box1{
width: 277px;
height: 103px;
background-color: #ff0000;
position: absolute;
top: 0;
left: 0;
}
.box2{
width: 137px;
height: 49px;
background-color: #00ff00;
position: absolute;
top: 0;
right:0;
}
.box3{
width: 679px;
height: 46px;
background-color: #00ff00;
position: absolute;
top: 57px;
right:0
}
section{
width: 970px;
height: 445px;
/*border: 1px solid red;*/
position: relative;
}
.box4{
width: 310px;
height: 435px;
background-color: #ffcc00;
position: absolute;
top: 0;
left: 0;
}
.box5{
width: 450px;
height: 240px;
background-color: #3399ff;
position: absolute;
top: 0;
left: 320px;
}
.box6{
width: 450px;
height: 110px;
background-color: #3399ff;
position: absolute;
top: 250px;
left: 320px;
}
.box7{
width: 450px;
height: 30px;
background-color: #3399ff;
position: absolute;
top: 370px;
left: 320px;
}
.box8{
width: 190px;
height: 400px;
background-color: #cc3399;
position: absolute;
top: 0;
right: 0;
}
.box9{
width: 650px;
height: 25px;
background-color: #339900;
position: absolute;
top: 410px;
right: 0;
}
footer{
width: 970px;
height: 35px;
/*border: 1px solid red;*/
position: relative;
}
.box10{
width: 970px;
height: 35px;
background-color: #000099;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div class="father">
<nav>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</nav>
<section>
<div class="box4"></div>
<div class="box5"></div>
<div class="box6"></div>
<div class="box7"></div>
<div class="box8"></div>
<div class="box9"></div>
</section>
<footer>
<div class="box10"></div>
</footer>
</div>
</body>
</html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<style type="text/css">
body{
margin: 0;
padding: 0;
}
.father{
width: 970px;
height: 593px;
margin: 200px auto;
}
nav{
width: 970px;
height: 113px;
/*border: 1px solid red;*/
position: relative;
}
.box1{
width: 277px;
height: 103px;
background-color: #ff0000;
position: absolute;
top: 0;
left: 0;
}
.box2{
width: 137px;
height: 49px;
background-color: #00ff00;
position: absolute;
top: 0;
right:0;
}
.box3{
width: 679px;
height: 46px;
background-color: #00ff00;
position: absolute;
top: 57px;
right:0
}
section{
width: 970px;
height: 445px;
/*border: 1px solid red;*/
position: relative;
}
.box4{
width: 310px;
height: 435px;
background-color: #ffcc00;
position: absolute;
top: 0;
left: 0;
}
.box5{
width: 450px;
height: 240px;
background-color: #3399ff;
position: absolute;
top: 0;
left: 320px;
}
.box6{
width: 450px;
height: 110px;
background-color: #3399ff;
position: absolute;
top: 250px;
left: 320px;
}
.box7{
width: 450px;
height: 30px;
background-color: #3399ff;
position: absolute;
top: 370px;
left: 320px;
}
.box8{
width: 190px;
height: 400px;
background-color: #cc3399;
position: absolute;
top: 0;
right: 0;
}
.box9{
width: 650px;
height: 25px;
background-color: #339900;
position: absolute;
top: 410px;
right: 0;
}
footer{
width: 970px;
height: 35px;
/*border: 1px solid red;*/
position: relative;
}
.box10{
width: 970px;
height: 35px;
background-color: #000099;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div class="father">
<nav>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</nav>
<section>
<div class="box4"></div>
<div class="box5"></div>
<div class="box6"></div>
<div class="box7"></div>
<div class="box8"></div>
<div class="box9"></div>
</section>
<footer>
<div class="box10"></div>
</footer>
</div>
</body>
</html>