前言
定位(position)
-定位是一种更高级的布局手段
-通过定位可以将元素摆放到页面的任意位置
-使用position属性来设置定位
可选值:
static 默认值,元素是静止的没有开启定位
relative 开启元素的相对定位
absolute 开启元素的绝对定位
fixed 开启元素的固定定位
sticky 开启元素的粘滞定位
一、绝对定位
绝对定位
-当元素的position属性值设置为absolute时,则开启了元素的绝对定位
-绝对定位的特点:
1.开启绝对定位后,如果不设置偏移量,元素的位置不会发生变化
2.开启绝对定位后,元素会从文档中脱离
3.绝对定位会改变元素的性质,行内变成块,块的宽高被内容撑开
4.绝对定位会使元素提升一个层级
5.绝对定位元素是相对于其包含块进行定位的
包含块(containning block)
-正常情况下:
包含块就是离当前元素最近的祖先块元素
<div> <div></div> </div> (此时里面的div的包含块是外面的div)
<div> <span><em>hello</em></span> </div> (此时span的包含块是div em的包含块是div,因为span是行内元素不是块元素)
-绝对定位的包含块:
包含块就是离他最近的开启了定位的祖先元素
如果所有的祖先元素都没有开启定位则根元素就是它的包含块
-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>绝对定位</title>
<style>
body{
height: 6000px;
font-size: 50px;
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
}
.box2{
/* 绝对定位会改变元素的性质,块的宽高被内容撑开这里可以看到不设置宽度,.box2的宽度被文字2的大小撑开 */
width: 200px;
height: 200px;
background-color: orange;
/* 开启了绝对定位 */
position: absolute;
top: 0;
left: 0;
}
.box3{
width: 200px;
height: 200px;
background-color: palevioletred;
/* 开启绝对定位后,box3元素会从文档流中脱离,我们可以看到box6移动到box3底下了 */
/* position: absolute; */
}
.box4{
width: 400px;
height:400px;
background-color: tomato;
/* position: relative; */
}
.box5{
width: 300px;
height: 300px;
background-color: teal;
/* position: relative; */
}
.box6{
width: 200px;
height: 200px;
background-color:turquoise;
}
</style>
</head>
<body>
<div class="box1">1</div>
<div class="box4">4
<div class="box5">5
<div class="box2">2</div>
</div>
</div>
<div class="box3">3</div>
<div class="box6">6</div>
</body>
</html>
二、相对定位
-相对定位
-当元素的position属性值设置为relative时则开启了元素的相对定位
-相对定位的特点:
1.元素开启相对定位以后,如果不设置偏移量,元素不会发生任何的变化(和绝对定位区分一下,绝对定位定位开启,不设置偏移量,此时只是位置没发生变化,别的很多都发生了变化)
2.相对定位是参照于元素在文档流中的位置进行定位的
3.相对定位会提高元素的层级
4.相对定位不会使元素脱离文档流
5.相对定位不会改变元素的性质,块元素还是块元素,行内元素还是行内元素
-偏移量(offset)
-当元素开启了定位以后,可以通过偏移量来设置元素的位置
top
-定位元素和定位位置上边的距离
bottom
-定位元素和定位位置下边的距离
-定位元素垂直方向的位置由top和bottom两个属性来控制
通常情况下我们只会使用其中一个
top值越大,定位元素越往下移动(top是负值时候,方向相反)
bottom值越大,定位元素越向上移动(bottom是负值时候,方向相反)
left
-定位元素和定位位置左侧的距离
right
-定位元素和定位位置右侧的距离
-定位元素水平方向的位置由left和right两个属性控制
通常情况下只会使用一个
-left越大元素越往右靠
-right越大元素越往左靠
<!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>相对定位</title>
<style>
body{
font-size: 40px;
}
.box1{
width: 200px;
height: 200px;
background-color: #bfa;
}
.box2{
width: 200px;
height: 200px;
background-color: orange;
position: relative;
/* 元素top和left设置之后我们看到它是相对于box2在文档流中的位置进行移动
同时看到box2移动到box1右边,但是box3并没有上移,说明原来box2的位置还被box2占着
可以看出相对定位不会使元素脱离文档流
*/
top: -200px;
left: 200px;
/* 此时看出box2覆盖box3,所以可以看出开启定位会提高元素的层级 */
/* top: 100px; */
/* margin-left: 200px;
margin-top: -200px; */
}
.box3{
width: 200px;
height: 200px;
background-color: tomato;
/* margin-top: 200px; */
}
</style>
</head>
<body>
<div class="box1">1</div>
<div class="box2">2</div>
<div class="box3">3</div>
</body>
</html>
此笔记来自于跟尚硅谷老师学习自己所写,用于自我复习