Kiểu vị trí Thuộc tính
Thí dụ
Thay đổi vị trí của phần tử <div> từ tương đối thành tuyệt đối:
document.getElementById("myDIV").style.position = "absolute";
Định nghĩa và Cách sử dụng
Thuộc tính vị trí đặt hoặc trả về kiểu phương pháp định vị được sử dụng cho một phần tử (tĩnh, tương đối, tuyệt đối hoặc cố định).
Hỗ trợ trình duyệt
Property | |||||
---|---|---|---|---|---|
position | Yes | Yes | Yes | Yes | Yes |
Cú pháp
Trả lại thuộc tính vị trí:
object.style.position
Đặt thuộc tính vị trí:
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
Giá trị tài sản
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 |
Chi tiết kỹ thuật
Giá trị mặc định: | tĩnh |
---|---|
Giá trị trả lại: | Một chuỗi, đại diện cho loại vị trí của một phần tử |
Phiên bản CSS | CSS2 |
Các ví dụ khác
Thí dụ
Sử dụng các loại vị trí khác nhau:
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
Thí dụ
Trả lại vị trí của phần tử <h2>:
alert(document.getElementById("myH2").style.position);
Các trang liên quan
Hướng dẫn CSS: Định vị CSS
Tham chiếu CSS: thuộc tính vị trí
❮ Đối tượng phong cách