static
position的默認定位。relative
相對于原位置進行偏移,使用top
,right
,bottom
,left
進行設置,元素仍占原來位置。元素上下位置默認后一個元素在前一個元素之上,可通過設置z-index
屬性改變。
div{
width: 200px;
height: 150px;
background-color: #fff;
border: 1px solid black;
}
.move{
position: relative;
left: 20px;
top: -30px;
}
relative
- absolute
與原位置無關,根據(jù)最近的除static 定位的已定位祖先元素進行定位,若沒有以定位祖先元素,則以<body>
進行定位。使用top
,right
,bottom
,left
進行設置,元素不占原來位置。元素上下位置默認后一個元素在前一個元素之上,可通過設置z-index
屬性改變。
.move{
position: absolute;
left: 100px;
top: 100px;
}
absolute
fixed
相對于瀏覽器窗口進行定位,當移動滾動條時,元素相對于瀏覽器窗口不移動。使用top
,right
,bottom
,left
進行設置,元素不占據(jù)文檔流中的位置。inherit
元素的定位繼承父元素的定位。