1、何为定位
定位指的是指定一个元素在网页上的位置,网页中行内元素在一行从左往右依次排列,块级元素垂直摆放,而定位呢就可以将其位置发生变化,上下左右均可移动,还可以脱离文档流,固定某个位置。
2、定位有哪些
2.1、相对定位
特点
- 使用属性position:relative表示
- 参考元素:是自己,偏移量和最初的自己做对比
- 不会脱离文档流,最初的位置,不会被其他元素填充
- 尝尝和使用绝对定位的孩子子元素搭配使用
- 并且常常和属性z-index进行网页元素层级划分
案例演示1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
padding: 0;
margin: 0;
}
div {
background-color: antiquewhite;
width: 200px;
height: 200px;
position: relative;
left: 200px;
top: 200px;
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>
效果
案例演示2 子绝父相
1、源码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
padding: 0;
margin: 0;
}
#father {
background-color: antiquewhite;
width: 200px;
height: 200px;
position: relative;
/* left: 200px;
top: 200px; */
}
#son {
background-color: purple;
width: 60px;
height: 60px;
position: absolute;
left: 60px;
top: 60px;
}
</style>
</head>
<body>
<div id="father">
<div id="son"></div>
</div>
<span>可能会填充的元素</span>
</body>
</html>
2、效果图
案例演示3 搭配z-index使用
1、源码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
padding: 0;
margin: 0;
}
#father {
background-color: antiquewhite;
width: 200px;
height: 200px;
position: relative;
left: 200px;
top: 200px;
}
#son {
background-color: purple;
width: 60px;
height: 60px;
position: absolute;
left: 160px;
z-index: -2;
top: 160px;
}
#grandson {
z-index: -1;
background-color: red;
width: 20px;
height: 20px;
position: absolute;
left: 50px;
top: 50px;
}
</style>
</head>
<body>
<div id="father">
<div id="son">
<div id="grandson"></div>
</div>
</div>
<span>可能会填充的元素</span>
</body>
</html>
2、效果图
特性简要说明
第二点:如图所示
第三点:span标签并未占据定位之后div位置
第四点:如案例演示2
第五点:如案例演示3
2.2、绝对定位
特点
- 元素设置样式通过position:absolute
- 参考对象是离自己最近的父元素
- 脱离文档流,原来的位置被填充,好像不存在似的
- 若是父元素没有设置定位的话,以HTML为参考对象
- 常和z-index搭配使用,提升层级
案例演示1
1、源码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
padding: 0;
margin: 0;
}
div {
background-color: antiquewhite;
width: 200px;
height: 200px;
position: absolute;
left: 200px;
top: 200px;
}
</style>
</head>
<body>
<div> </div>
<span>可能会占据定位元素的位置</span>
</body>
</html>
2、效果图
案例演示2 搭配z-index使用
1、源码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
padding: 0;
margin: 0;
}
#father {
background-color: antiquewhite;
width: 200px;
height: 200px;
position: absolute;
left: 200px;
top: 200px;
}
#son {
background-color: purple;
width: 60px;
height: 60px;
position: absolute;
left: 160px;
z-index: -2;
top: 160px;
}
#grandson {
z-index: -1;
background-color: red;
width: 20px;
height: 20px;
position: absolute;
left: 50px;
top: 50px;
}
</style>
</head>
<body>
<div id="father">
<div id="son">
<div id="grandson"></div>
</div>
</div>
<span>可能会占据定位元素的位置</span>
</body>
</html>
2、效果图
2.3、固定定位
特点
- 元素设置样式通过position:fixed
- 参考对象是以浏览器左上角
- 脱离文档流,好似不存在
- 不会随滚动条滚动
- 常和z-index搭配使用,提升层级
- 和绝对定位区别:是否随着滚动条滚动而滚动
案例演示1
1、源码实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
padding: 0;
margin: 0;
}
#father {
background-color: antiquewhite;
width: 200px;
height: 200px;
position: fixed;
left: 200px;
top: 200px;
}
#son {
background-color: purple;
width: 60px;
height: 60px;
position: fixed;
left: 160px;
z-index: -2;
top: 160px;
}
#grandson {
z-index: -1;
background-color: red;
width: 20px;
height: 20px;
position: fixed;
left: 50px;
top: 50px;
}
</style>
</head>
<body>
<div id="father">
<div id="son">
<div id="grandson"></div>
</div>
</div>
<span>可能会占据定位元素的位置</span>
</body>
</html>
2、效果图
简要说明
第六点:如图所示
2.4、静态定位
- 元素默认不设置position属性就是 position:static
- 行内元素左右排序,块级元素垂直摆放