Szerokość stylu Właściwość
Przykład
Ustaw szerokość elementu <button>:
document.getElementById("myBtn").style.width = "300px";
Definicja i użycie
Właściwość width ustawia lub zwraca szerokość elementu.
Właściwość width ma wpływ tylko na elementy blokowe lub elementy z bezwzględną lub stałą pozycją. Przepełnioną treścią można manipulować za pomocą właściwości overflow.
Wskazówka: Użyj właściwości height , aby ustawić lub zwrócić wysokość elementu.
Obsługa przeglądarki
Property | |||||
---|---|---|---|---|---|
width | Yes | Yes | Yes | Yes | Yes |
Składnia
Zwróć właściwość szerokości:
object.style.width
Ustaw właściwość szerokości:
object.style.width = "auto|length|%|initial|inherit"
Wartości nieruchomości
Value | Description |
---|---|
auto | The browser sets the width. This is default |
length | Defines the width in length units |
% | Defines the width in % of the parent element |
initial | Sets this property to its default value. Read about initial |
inherit | Inherits this property from its parent element. Read about inherit |
Szczegóły techniczne
Domyślna wartość: | automatyczny |
---|---|
Wartość zwrotu: | String, reprezentujący szerokość elementu |
Wersja CSS | CSS1 |
Więcej przykładów
Przykład
Zmień szerokość elementu <div>:
document.getElementById("myDIV").style.width = "500px";
Przykład
Zmień wysokość i szerokość elementu <img>:
document.getElementById("myImg").style.height = "300px";
document.getElementById("myImg").style.width = "300px";
Przykład
Zwróć szerokość elementu <img>:
alert(document.getElementById("myImg").style.width);
Powiązane strony
Samouczek CSS: wymiar CSS
Samouczek CSS: model Box CSS
Odniesienie CSS: właściwość szerokości
❮ Obiekt stylu