ตำแหน่งสไตล์คุณสมบัติ
ตัวอย่าง
เปลี่ยนตำแหน่งของ <div> องค์ประกอบจากญาติเป็นสัมบูรณ์:
document.getElementById("myDIV").style.position = "absolute";
ความหมายและการใช้งาน
คุณสมบัติตำแหน่งตั้งค่าหรือส่งคืนประเภทของวิธีการกำหนดตำแหน่งที่ใช้สำหรับองค์ประกอบ (คงที่, สัมพันธ์, สัมบูรณ์หรือคงที่)
รองรับเบราว์เซอร์
Property | |||||
---|---|---|---|---|---|
position | Yes | Yes | Yes | Yes | Yes |
ไวยากรณ์
ส่งคืนคุณสมบัติตำแหน่ง:
object.style.position
กำหนดคุณสมบัติตำแหน่ง:
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
มูลค่าทรัพย์สิน
Value | Description |
---|---|
static | Elements renders in order, as they appear in the document flow. This is default. |
absolute | The element is positioned relative to its first positioned (not static) ancestor element |
fixed | The element is positioned relative to the browser window |
relative | The element is positioned relative to its normal position, so "left:20" adds 20 pixels to the element's LEFT position |
sticky | The element is positioned based on the user's scroll position
A sticky element toggles between |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
รายละเอียดทางเทคนิค
ค่าเริ่มต้น: | คงที่ |
---|---|
คืนมูลค่า: | สตริงที่แสดงถึงประเภทตำแหน่งขององค์ประกอบ |
เวอร์ชัน CSS | CSS2 |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
การใช้ตำแหน่งประเภทต่างๆ:
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
ตัวอย่าง
กลับตำแหน่งของ <h2> องค์ประกอบ:
alert(document.getElementById("myH2").style.position);
หน้าที่เกี่ยวข้อง
กวดวิชา CSS: การจัดตำแหน่ง CSS
การอ้างอิง CSS: คุณสมบัติตำแหน่ง
❮ สไตล์วัตถุ