Properti posisi CSS
Contoh
Posisikan elemen <h2>:
h2
{
position: absolute;
left: 100px;
top: 150px;
}
Lebih banyak contoh "Coba Sendiri" di bawah ini.
Definisi dan Penggunaan
Properti position
menentukan jenis metode pemosisian yang digunakan untuk elemen (statis, relatif, absolut, tetap, atau lengket).
Nilai bawaan: | statis |
---|---|
Diwarisi: | tidak |
Animasi: | tidak. Baca tentang animasi |
Versi: kapan: | CSS2 |
Sintaks JavaScript: | objek .style.position="absolut" |
Dukungan Peramban
Angka-angka dalam tabel menentukan versi browser pertama yang sepenuhnya mendukung properti.
Property | |||||
---|---|---|---|---|---|
position | 1.0 | 7.0 | 1.0 | 1.0 | 4.0 |
Catatan: Nilai sticky
ini tidak didukung di Internet Explorer atau Edge 15 dan versi yang lebih lama.
Sintaks CSS
position: static|absolute|fixed|relative|sticky|initial|inherit;
Nilai properti
Value | Description | Play it |
---|---|---|
static | Default value. Elements render in order, as they appear in the document flow | |
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:20px" 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 |
Lebih Banyak Contoh
Contoh
Cara memposisikan elemen relatif terhadap posisi normalnya:
h2.pos_left {
position: relative;
left: -20px;
}
h2.pos_right {
position: relative;
left: 20px;
}
Contoh
Lebih banyak posisi:
#parent1 {
position: static;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child1 {
position:
absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
#parent2 {
position: relative;
border: 1px solid blue;
width: 300px;
height: 100px;
}
#child2 {
position: absolute;
border: 1px solid red;
top: 70px;
right: 15px;
}
Halaman Terkait
Tutorial CSS: Pemosisian CSS
Referensi DOM HTML: properti posisi