Posisi gaya Properti
Contoh
Ubah posisi elemen <div> dari relatif menjadi absolut:
document.getElementById("myDIV").style.position = "absolute";
Definisi dan Penggunaan
Properti posisi menetapkan atau mengembalikan jenis metode penentuan posisi yang digunakan untuk suatu elemen (statis, relatif, absolut, atau tetap).
Dukungan Peramban
Property | |||||
---|---|---|---|---|---|
position | Yes | Yes | Yes | Yes | Yes |
Sintaksis
Kembalikan properti posisi:
object.style.position
Setel properti posisi:
object.style.position = "static|absolute|fixed|relative|sticky|initial|inherit"
Nilai properti
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 |
Detail Teknis
Nilai Bawaan: | statis |
---|---|
Nilai Kembali: | Sebuah String, mewakili tipe posisi elemen |
Versi CSS | CSS2 |
Lebih Banyak Contoh
Contoh
Menggunakan berbagai jenis posisi:
function myFunction(x) {
var whichSelected = x.selectedIndex;
var posVal = x.options[whichSelected].text;
var elem = document.getElementById("myDiv");
elem.style.position = posVal;
}
Contoh
Kembalikan posisi elemen <h2>:
alert(document.getElementById("myH2").style.position);
Halaman Terkait
Tutorial CSS: Pemosisian CSS
Referensi CSS: properti posisi
Objek Gaya