絕對定位的元素是不佔據空間的,它會出如今正常文檔流的位置。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;
}