Style position 屬性
示例
將 <div> 元素的 position 從相對改為絕對
document.getElementById("myDIV").style.position = "absolute";
自己動手試一試 »
描述
position 屬性設定或返回元素使用的定位方法型別(static、relative、absolute 或 fixed)。
瀏覽器支援
屬性 | |||||
---|---|---|---|---|---|
position | 是 | 是 | 是 | 是 | 是 |
語法
返回 position 屬性
object.style.position
設定 position 屬性
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
屬性值
值 | 描述 |
---|---|
static | 元素按文件流中出現的順序渲染。這是預設值。 |
absolute | 元素相對於其第一個已定位(非靜態)的祖先元素定位 |
fixed | 元素相對於瀏覽器視窗定位 |
relative | 元素相對於其正常位置定位,因此 "left:20" 會在元素的 LEFT 位置增加 20 畫素 |
sticky | 元素根據使用者的滾動位置定位 粘性元素在 |
initial | 將此屬性設定為其預設值。閱讀關於 initial |
inherit | 從其父元素繼承此屬性。閱讀關於 inherit |
技術詳情
預設值 | static |
---|---|
返回值 | 一個字串,表示元素的 position 型別 |
CSS 版本 | CSS2 |
更多示例
示例
使用不同的 position 型別
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
自己動手試一試 »
相關頁面
CSS 教程:CSS 定位
CSS 參考:position 屬性