Properti lebar CSS
Contoh
Atur lebar tiga elemen <div>:
div.a {
width: auto;
border: 1px
solid black;
}
div.b {
width: 150px;
border: 1px solid black;
}
div.c {
width:
50%;
border: 1px solid black;
}
Lebih banyak contoh "Coba Sendiri" di bawah ini.
Definisi dan Penggunaan
Properti width
mengatur lebar elemen.
Lebar elemen tidak termasuk padding, border, atau margin!
Catatan: Properti min-width dan
max-width menimpa width
properti.
Nilai bawaan: | mobil |
---|---|
Diwarisi: | tidak |
Animasi: | Ya. Baca tentang animasi |
Versi: kapan: | CSS1 |
Sintaks JavaScript: | objek .style.width="500px" |
Dukungan Peramban
Angka-angka dalam tabel menentukan versi browser pertama yang sepenuhnya mendukung properti.
Property | |||||
---|---|---|---|---|---|
width | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
Sintaks CSS
width: auto|value|initial|inherit;
Nilai properti
Value | Description | Play it |
---|---|---|
auto | Default value. The browser calculates the width | |
length | Defines the width in px, cm, etc. Read about length units | |
% | Defines the width in percent of the containing block | |
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
Atur lebar elemen <img> menggunakan nilai persen:
img {
width: 50%;
}
Contoh
Atur lebar elemen <input type="text"> menjadi 100px. Namun, saat mendapat fokus, buat lebar 250px:
input[type=text] {
width: 100px;
}
input[type=text]:focus {
width: 250px;
}
Halaman Terkait
Tutorial CSS: Tinggi dan Lebar CSS
Tutorial CSS: Model Kotak CSS
Referensi CSS: properti ketinggian
Referensi DOM HTML: properti lebar