目录
使用场景:想要覆盖盒子或移动盒子位置的时候使用。
绝对定位(position:absolute)
文档流
一个盒子给了绝对定位以后,该盒子是悬空不占位置的,并且不会进行移动。
代码:
<!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>
*{
padding: 0;
margin: 0;
}
div{
width: 200px;
height: 200px;
background-color: red;
/* 绝对定位 */
position: absolute;
}
h1{
width: 400px;
height: 300px;
background-color: blue;
}
</style>
</head>
<body>
<!-- 并列结构下通常为竖着排列 -->
<div></div>
<h1></h1>
</body>
</html>
运行结果:
层叠顺序:
默认情况下,当多个盒子同时给了定位,html在后的结构在上。
代码:
<!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>
div:nth-of-type(1){
width: 300px;
height: 300px;
background-color: red;
position: absolute;
}
div:nth-of-type(2){
width: 200px;
height: 200px;
background-color: blue;
position: absolute;
}
div:nth-of-type(3){
width: 100px;
height: 100px;
background-color: purple;
position: absolute;
}
</style>
</head>
<body>
<!-- 多个盒子绝对定位时,html结构在后面的最先显示 -->
<div></div>
<div></div>
<div></div>
</body>
</html>
运行结果:
应用
可以通过top,left,right,bottom移动位置。参照物可以是浏览器窗口的第一屏,或者有定位设置的父元素。
参照物为第一屏时:
代码:
<!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>
*{
margin: 0;
padding: 0;
}
div{
width: 100px;
height: 100px;
background-color: red;
/* 绝对定位 */
position: absolute;
/* 设置属性 */
left: 200px;
top: 500px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
运行结果:
参照物为有定位的父元素时:
代码:
<!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>
*{
margin: 0;
padding: 0;
}
div{
width: 500px;
height: 500px;
margin-left:100px;;
background-color: red;
/* 相对定位 */
position: relative;
}
p{
width: 50px;
height: 50px;
background-color: black;
/* 绝对定位 */
position: absolute;
left: 10px;
top: 200px;
}
</style>
</head>
<body>
<div>
<p></p>
</div>
</body>
</html>
运行结果:
相对定位( position:relative)
文档流
一个盒子给了相对对定位之后,该盒子是不悬空占位的。(也就是正常占位)
代码:
<!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>
*{
padding: 0;
margin: 0;
}
div{
width: 200px;
height: 200px;
background-color: red;
/* 相对定位 */
position: relative;
}
h1{
width: 400px;
height: 300px;
background-color: blue;
}
</style>
</head>
<body>
<!-- 并列结构下通常为竖着排列 -->
<div></div>
<h1></h1>
</body>
</html>
运行结果:
层叠顺序
默认情况下,当多个盒子同时给了相对定位,盒子没有层叠的视觉效果,但是移动位置之后,可以看到层叠效果,HTML结构在后的层在上。
代码:
<!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>
div:nth-of-type(1){
width: 300px;
height: 300px;
background-color: red;
position:relative;
}
div:nth-of-type(2){
width: 200px;
height: 200px;
background-color: blue;
position:relative;
/* 通过给负数达到盒子向上位移的效果 */
top:-100px;
}
div:nth-of-type(3){
width: 100px;
height: 100px;
background-color: purple;
position:relative;
/* 通过给负数达到盒子向上位移的效果 */
top:-150px;
}
</style>
</head>
<body>
<!-- 多个盒子相对定位时,html结构在后面的最先显示 -->
<div></div>
<div></div>
<div></div>
</body>
</html>
运行结果:
盒子没有层叠的视觉效果。
移动位置之后,可以看到层叠效果。
应用
可以通过top left right bottom移动位置,参照物: 自己的初始位置。
注意点:即使移动了位置,原有空间还在。
代码:
<!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>
*{
margin: 0;
padding: 0;
}
div:nth-of-type(1){
width: 100px;
height: 100px;
background-color: red;
/* 相对定位 */
position: relative;
/* 设置属性 */
top: -50px;
}
div:nth-of-type(2){
width: 100px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<div></div>
<div></div>
</body>
</html>
运行结果:
固定定位(position:fixed)
文档流
一个盒子给了固定定位之后,该盒子是悬空不占位的,只是原地悬空不占位,不会进行位置移动。
层叠顺序
默认情况下,当多个盒子同时给了定位,HTML结构在后的定位层在上
应用
可以通过top left right bottom移动位置,参照物: 浏览器的当前窗口(视口)。
代码:
<!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>
*{
margin: 0;
padding: 0;
}
body{
/* 给整个页面设置高度,出滚动条以便观察 */
height: 5000px;
}
div{
width: 100px;
height: 100px;
background-color: blue;
/* 固定定位 */
position: fixed;
right: 100px;
bottom: 100px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
运行结果:
移动前:
移动后:
层叠顺序的改变(z-index)
想要改变层叠顺序,可以通过属性z-index改变。
注意:
- 默认值是0
- 数值越大层越靠上
- 不带单位
- 没有最大值和最小值
- 可以给负数
代码:
<!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>
div:nth-of-type(1){
width: 300px;
height: 300px;
background-color: red;
position: absolute;
}
div:nth-of-type(2){
width: 200px;
height: 200px;
background-color: blue;
position: absolute;
}
div:nth-of-type(3){
width: 100px;
height: 100px;
background-color: purple;
position: absolute;
z-index: -1;
}
</style>
</head>
<body>
<!-- 多个盒子绝对定位时,html结构在后面的最先显示 ,但更改z-index属性值以后显示顺序就会改变-->
<div></div>
<div></div>
<div></div>
</body>
</html>
运行结果:
以上就是常用的三个定位的详细解释,码字不易点个赞再走吧~~~~