前端 | CSS
定位属性position
的用法

xml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS_POSITION</title>
<style type="text/css">
.div1 {
width: 360px;
height: 360px;
border: 1px solid black ;
}
.div1_1 {
width: 180px;
height: 180px;
background-color:burlywood ;
}
.div1_2 {
width: 180px;
height: 180px;
background-color:aquamarine;
}
</style>
</head>
<body>
<div class="div1">
<div class="div1_1">div11</div>
<div class="div1_2">div12</div>
</div>
</body>
</html>

static 静态定位
- 默认的
position
值是static
- 忽略
top
,right
,bottom
,left
,z-index
属性的设置
css
.div1 {
position: static;
width: 360px;
height: 360px;
border: 1px solid black ;
}
.div1_1 {
width: 180px;
height: 180px;
background-color:burlywood ;
}
.div1_2 {
width: 180px;
height: 180px;
background-color:aquamarine;
}

relative 相对定位
- 相对于元素正常位置(
static
)进行的定位
- 可以使用
top
、right
、bottom
、left
属性来调整元素的位置
1 relative
修改div1
div11
与div12
的高度和与div1
一致,宽度为div1
的一半
css
.div1 {
width: 360px;
height: 360px;
border: 1px solid black ;
position: relative;
top:100px;
}

div1
包含div11
与div12
,由于div1
整体高度偏移了,看上去整体偏移了
2 relative
修改div11
div1
还原,仅修改div11
css
.div1_1 {
width: 180px;
height: 180px;
background-color: cyan;
position: relative;
left: 180px;
}

- 修改
div12
与div11
的相对定位一致
3 relative
修改div1
和div11
css
.div1 {
width: 360px;
height: 360px;
border: 1px solid black ;
position: relative;
top:100px;
}
.div1_1 {
width: 180px;
height: 180px;
background-color: cyan;
position: relative;
left: 180px;
}

absolute 绝对定位
- 不存在父元素,元素将以浏览器窗口为基准进行定位
- 存在父元素,元素将以父元素为基准进行定位
- 可以使用
top
、right
、bottom
、left
属性来调整元素的位置
1 absolute
设置div1
css
.div1 {
width: 360px;
height: 360px;
border: 1px solid black;
position: absolute;
top: 100px;
}

- 与
relative
还是不相同的
2 absolute
设置div12
css
.div1_2 {
width: 180px;
height: 180px;
background-color: orange;
position: absolute;
/* left: 180px; */
}
- 先不设置
left
值时,与relative
是一致的,也就是按照布局展开的

- 由于没有删除
body
中margin
与padding
的宽度,固有以下影响

- 再次将
div12
的left
设置为180px
将出现空隙

- 受到
body { padding: '8px' }
与.div1 { border: 1px solid black; }
的影响,应该修改为180px + 8px + 1px
用以对齐

- 修改
div11
与div12
的绝对定位一致
3 absolute
设置div11
div11
初步设置absolute
,将使得div12
隐藏到div11
的下方,通过以下两图说明是隐藏的
css
.div1_1 {
width: 180px;
height: 180px;
background-color: cyan;
position: absolute;
}

css
.div1_2 {
width: 200px;
height: 200px;
background-color: orange;
}

- 重置
div12
,调整div11
css
.div1_1 {
width: 180px;
height: 180px;
background-color: cyan;
position: absolute;
left: 189px;
top: 189px;
}

fixed 固定定位
- 以浏览器窗口为基准的定位
- 可以使用
top
、right
、bottom
、left
属性来调整元素的位置
1 fixed
设置div1
css
.div1 {
width: 360px;
height: 360px;
border: 1px solid black;
position: fixed;
top: 100px;
}

div1
已经超出了body
的空间,以浏览器窗口 为基准,距离顶部100px
2 fixed
设置div11
css
.div1_1 {
width: 180px;
height: 180px;
background-color: cyan;
position: fixed;
left: 189px;
top: 189px;
}

- 与
absolution
不一致,absolution
则是相对父元素 进行的处理,而fixed
是相对浏览器窗口
css
.div1_1 {
width: 180px;
height: 180px;
background-color: cyan;
position: fixed;
left: 189px;
top: 289px;
}
- 将
top: 289px
,发现内容超过div1
高度,原因是fixed
布局不受body
影响,实际高度180px + 100px + 1px

css
.div1_1 {
width: 180px;
height: 180px;
background-color: cyan;
position: fixed;
left: 189px;
top: 281px;
}

sticky 粘性定位
- 元素在超过特定值前为相对定位(
relative
),之后为固定定位(fixed
)
- 可以使用
top
、right
、bottom
、left
属性来调整元素的位置
1 sticky
设置div1
css
.div1 {
width: 360px;
height: 360px;
border: 1px solid black;
position: sticky;
top: 100px;
left: 206px;
}
- 无论如何调整
top
大小没有作用,需要设置body
的高度

-
调整
left
大小,小于206px
是相对定位 ,大于206px
是固定定位。206px
是560px - 362px + 8px

left
等于206px

left
小于206px

left
大于206px

2 sticky
设置div1.top
css
body {
height: '400px';
}
.div1 {
width: 360px;
height: 360px;
border: 1px solid black;
position: sticky;
top: 100px;
left: 206px;
}

-
高度配置,需要掌握父元素 的高度,超过父元素 高度才有用,限制父元素高度为
400px
div1
元素高度为362px
,小于46px=400px - 362px + 8px
是相对定位,大于46px
是固定定位

top
小于46px
,是相对定位,

top
大于46px

- 由于
div1
的父元素是body,也就是浏览器窗体,是不容易判断是相对定位还是固定定位的
3 sticky
设置div11.top
div1
设置为相对定位,top: 100px
css
.div1 {
width: 360px;
height: 360px;
border: 1px solid black;
position: relative;
top: 100px;
}

-
按照上述减法计算临界
362px - 1px - 180px + 8px + 100px
div1
总高度362px
,上下边框总共占2px ,div11
在div1
内部,临界值为360px - 180px
div1
外层还有内容,所以要加上div1 上边框
,div1 相对高度
,body margin
,最终临界值为360px - 180px + 1px + 100px + 8px = 289px
css
.div1_1 {
width: 180px;
height: 180px px;
background-color: cyan;
position: sticky;
top: 289px;
}

-
top
小于289px
228px
是一个特殊位置,计算公式228px = 408px - 180px
css
.div1_1 {
width: 180px;
height: 180px;
background-color: cyan;
position: sticky;
top: 228px;
}

top
大于289px
