CSS position 屬性
更多“自己嘗試”的例子見下文。
定義和用法
The position
property specifies the type of positioning method used for an element (static, relative, absolute, fixed, or sticky).
瀏覽器支援
表中的數字指定了完全支援該屬性的第一個瀏覽器版本。
屬性 | |||||
---|---|---|---|---|---|
position | 1.0 | 7.0 | 1.0 | 1.0 | 4.0 |
注意: sticky
值在 Edge 15 及更早版本中不受支援。
CSS 語法
position: static|absolute|fixed|relative|sticky|initial|inherit;
屬性值
值 | 描述 | 演示 |
---|---|---|
static | 預設值。元素按照它們在文件流中的順序進行渲染 | 試一試 » |
absolute | 元素相對於其第一個定位(非 static)的祖先元素進行定位 | 試一試 » |
fixed | 元素相對於瀏覽器視窗進行定位 | 試一試 » |
relative | 元素相對於其正常位置進行定位,因此 "left:20px" 會在元素的左側位置增加 20 畫素 | 試一試 » |
sticky | 元素根據使用者的滾動位置進行定位 粘性元素在 |
試一試 » |
initial | 將此屬性設定為其預設值。閱讀關於initial | |
inherit | 從其父元素繼承此屬性。閱讀關於inherit |
更多示例
示例
如何相對於其正常位置定位元素
h2.pos_left {
position: relative;
left: -20px;
}
h2.pos_right {
position: relative;
left: 20px;
}
自己動手試一試 »
示例
更多定位
#parent1 {
position: static;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child1 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
#parent2 {
position: relative;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child2 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
自己動手試一試 »
相關頁面
CSS 教程:CSS 定位
HTML DOM 參考:position 屬性