定位(position)
- 边偏移
- top 顶端偏移量 定义元素相对于其父元素上边线的距离
- left 定义元素相对于其父元素左边线的距离
- right 定义元素相对于其父元素右边线的距离
- bottom 定义元素相对于其父元素下边线的距离
- 静态定位 (浏览器默认为静态定位)
- position: static
- 元素默认的定位方式,假如元素position设置为static,则元素定位在静态位置(即文档流中默认的位置)
- 相对定位
- position: relative
- 相对自己做偏移,相对定位作用 1 做一些位置微调 2 配合绝对定位使用
- 注意 相对定位的元素 不脱标,之前的位置仍然占有
- 绝对定位
- position: absolute
- 绝对定位的盒子脱标
- 工程上 常常使用子绝父相,父亲相对定位 占有位置不脱标,儿子采用绝对定位 不占有位置 完全脱标
- 固定定位(position: fixed)
- 参考点 浏览器左上角
- 固定定位的元素脱标不占有位置
- 兼容性 ie6低版本不支持固定定位
静态定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
相对定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div {
width: 100px;
height: 100px;
background-color: red;
margin-bottom: 20px;
}
div:nth-child(2) {
position: relative;
left: 150px;
top: 150px;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
</body>
</html>
绝对定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
div {
width: 100px;
height: 100px;
background-color: red;
margin-bottom: 20px;
}
div:nth-child(1) {
position: absolute;
left: 150px;
top: 150px;
background-color: yellow;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
</body>
</html>
绝对定位的参考点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.fa {
width: 200px;
height: 200px;
background-color: red;
margin: 200px;
}
.fa .son {
width: 100px;
height: 100px;
background-color: green;
position: absolute;
left: 20px;
top: 20px;
}
</style>
</head>
<body>
<div class="fa">
<div class="son"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.fa {
width: 200px;
height: 200px;
background-color: red;
margin: 200px;
position: relative;
top: 100px;
left: 100px;
}
.fa .son {
width: 100px;
height: 100px;
background-color: green;
position: absolute;
left: 20px;
top: 20px;
}
</style>
</head>
<body>
<div class="fa">
<div class="son"></div>
</div>
</body>
</html>
绝对定位的应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.fa {
width: 600px;
height: 300px;
margin: 60px auto;
background-color: skyblue;
position: relative;
}
.son {
width: 200px;
height: 100px;
background-color: pink;
position: absolute;
left: 50%;
margin-left: -100px;
top: 50%;
margin-top: -50px;
}
</style>
</head>
<body>
<div class="fa">
<div class="son"></div>
</div>
</body>
</html>
固定定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
body {
height: 2000px;
}
.fa {
width: 600px;
height: 300px;
margin: 60px auto;
background-color: skyblue;
position: relative;
}
span {
width: 300px;
height: 100px;
background-color: green;
position: fixed;
top: 60px;
left: 50px;
}
</style>
</head>
<body>
<div class="fa">
<span class="son">我是一段文字</span>
</div>
</body>
</html>