position属性:
absolute | 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。 |
---|---|
fixed | 生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。 |
relative | 生成相对定位的元素,相对于其正常位置进行定位。 |
static | 默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。 |
inherit | 规定应该从父元素继承 position 属性的值。 |
需求:父元素居中,子元素覆盖body的蒙版
原代码:
<div calss='fater'>
<div class='loading'></div>
</div>
.fater{
width:1024px;
height:100vh;
position:absolute;
transform:translate(-50%,-50%)
}
.loading{
postion:fixed;
width:100vw;
height:100vh;
top:0;
background:rgba(255,255,255,.84)
}
loading并没有覆盖body而是覆盖了father
原因:父级元素使用tranform,子元素使用fixed,子元素会变成相对于父级元素定位
修改后代码
<div calss='fater'>
<div class='loading'></div>
</div>
.fater{
width:1024px;
height:100vh;
margin:0 auto;
}
.loading{
postion:fixed;
width:100vw;
height:100vh;
top:0;
background:rgba(255,255,255,.84)
}