绝对定位的元素是不占据空间的,它会出如今正常文档流的位置。spa
咱们通常绝对定位,是先让那个父容器相对定位,而后子元素绝对定位,经过top和left来调整位置可是这样定位,自适应很差。文档
若是绝对定位后不调整top和left,而经过margin来控制位置,那么这样自适应就很好,并且父容器也不用定位。it
绝对定位:若是同时设置了left和right,将实现水平拉伸;若是同时设置了top和botom,将实现垂直拉伸io
.chil{容器
position: absolute;自适应
top: 0;margin
left: 0;top
height: 30px;兼容
right: 0;co
bottom: 0;
background-color: pink;
}
满屏拉伸
但若是设置了width和height,则width和height覆盖拉伸。
若是同时出现width和拉伸,则能够使用margin来使元素水平居中 //只兼容IE8及其以上版本
.chil{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
width: 200px;
background-color: pink;
}