Wyświetlanie stylu Właściwość
Przykład
Ustaw element <div> tak, aby nie był wyświetlany:
document.getElementById("myDIV").style.display = "none";
Definicja i użycie
Właściwość wyświetlania ustawia lub zwraca typ wyświetlania elementu.
Elementy w HTML to głównie elementy „wbudowane” lub „blokowe”: element wbudowany ma pływającą zawartość po lewej i prawej stronie. Element blokowy wypełnia całą linię i nic nie może być wyświetlane po jego lewej lub prawej stronie.
Właściwość display umożliwia również autorowi pokazanie lub ukrycie elementu. Jest podobny do właściwości widoczności . Jeśli jednak ustawisz display:none
, ukryje on cały element, a visibility:hidden
oznacza, że zawartość elementu będzie niewidoczna, ale element pozostanie w swojej pierwotnej pozycji i rozmiarze.
Wskazówka: Jeśli element jest elementem blokowym, jego typ wyświetlania można również zmienić za pomocą właściwości float.
Obsługa przeglądarki
Property | |||||
---|---|---|---|---|---|
display | Yes | Yes | Yes | Yes | Yes |
Składnia
Zwróć właściwość wyświetlania:
object.style.display
Ustaw właściwość wyświetlania:
object.style.display = value
Wartości nieruchomości
Value | Description |
---|---|
block | Element is rendered as a block-level element |
compact | Element is rendered as a block-level or inline element. Depends on context |
flex | Element is rendered as a block-level flex box. New in CSS3 |
inline | Element is rendered as an inline element. This is default |
inline-block | Element is rendered as a block box inside an inline box |
inline-flex | Element is rendered as a inline-level flex box. New in CSS3 |
inline-table | Element is rendered as an inline table (like <table>), with no line break before or after the table |
list-item | Element is rendered as a list |
marker | This value sets content before or after a box to be a marker (used with :before and :after pseudo-elements. Otherwise this value is identical to "inline") |
none | Element will not be displayed |
run-in | Element is rendered as block-level or inline element. Depends on context |
table | Element is rendered as a block table (like <table>), with a line break before and after the table |
table-caption | Element is rendered as a table caption (like <caption>) |
table-cell | Element is rendered as a table cell (like <td> and <th>) |
table-column | Element is rendered as a column of cells (like <col>) |
table-column-group | Element is rendered as a group of one or more columns (like <colgroup>) |
table-footer-group | Element is rendered as a table footer row (like <tfoot>) |
table-header-group | Element is rendered as a table header row (like <thead>) |
table-row | Element is rendered as a table row (like <tr>) |
table-row-group | Element is rendered as a group of one or more rows (like <tbody>) |
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ść: | w linii |
---|---|
Wartość zwrotu: | String, reprezentujący typ wyświetlania elementu |
Wersja CSS | CSS1 |
Więcej przykładów
Przykład
Różnica między właściwością wyświetlania a właściwością widoczności:
function demoDisplay() {
document.getElementById("myP1").style.display = "none";
}
function demoVisibility() {
document.getElementById("myP2").style.visibility = "hidden";
}
Przykład
Przełącz między ukrywaniem a pokazywaniem elementu:
function myFunction() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
Przykład
Różnica między „inline”, „block” i „none”:
function myFunction(x) {
var whichSelected = x.selectedIndex;
var sel = x.options[whichSelected].text;
var elem = document.getElementById("mySpan");
elem.style.display = sel;
}
Przykład
Zwróć typ wyświetlania elementu <p>:
alert(document.getElementById("myP").style.display);
Powiązane strony
Samouczek CSS: wyświetlanie i widoczność CSS
Odniesienie CSS: właściwość wyświetlania
❮ Obiekt stylu