Vị trí phong cách Thuộc tính
Ví 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";
Hãy tự mình thử »Sự miêu tả
Thuộc tính vị trí đặt hoặc trả về loại phương thức đị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ả về 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ả về: | Một Chuỗi, biểu thị loại vị trí của một phần tử |
Phiên bản CSS | CSS2 |
Thêm ví dụ
Ví 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;
}
Hãy tự mình thử »Ví dụ
Trả về vị trí của phần tử <h2>:
alert(document.getElementById("myH2").style.position);
Hãy tự mình thử »Trang liên quan
Hướng dẫn CSS: Định vị CSS
Tham chiếu CSS: thuộc tính vị trí