Propriedade de exibição de estilo
Exemplo
Defina um elemento <div> para não ser exibido:
document.getElementById("myDIV").style.display = "none";
Definição e uso
A propriedade display define ou retorna o tipo de exibição do elemento.
Elementos em HTML são principalmente elementos "inline" ou "block": Um elemento inline tem conteúdo flutuante em seu lado esquerdo e direito. Um elemento de bloco preenche toda a linha e nada pode ser exibido em seu lado esquerdo ou direito.
A propriedade display também permite que o autor mostre ou oculte um elemento. É semelhante à propriedade de visibilidade . No entanto, se você definir display:none
, ele ocultará todo o elemento, enquanto visibility:hidden
significa que o conteúdo do elemento ficará invisível, mas o elemento permanecerá em sua posição e tamanho originais.
Dica: Se um elemento é um elemento de bloco, seu tipo de exibição também pode ser alterado com a propriedade float.
Suporte ao navegador
Property | |||||
---|---|---|---|---|---|
display | Yes | Yes | Yes | Yes | Yes |
Sintaxe
Retorne a propriedade de exibição:
object.style.display
Defina a propriedade de exibição:
object.style.display = value
Valores de propriedade
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 |
Detalhes técnicos
Valor padrão: | em linha |
---|---|
Valor de retorno: | Uma String, representando o tipo de exibição de um elemento |
Versão CSS | CSS1 |
Mais exemplos
Exemplo
Diferença entre a propriedade de exibição e a propriedade de visibilidade:
function demoDisplay() {
document.getElementById("myP1").style.display = "none";
}
function demoVisibility() {
document.getElementById("myP2").style.visibility = "hidden";
}
Exemplo
Alterne entre ocultar e mostrar um elemento:
function myFunction() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
Exemplo
Diferença entre "inline", "block" e "none":
function myFunction(x) {
var whichSelected = x.selectedIndex;
var sel = x.options[whichSelected].text;
var elem = document.getElementById("mySpan");
elem.style.display = sel;
}
Exemplo
Retorna o tipo de exibição de um elemento <p>:
alert(document.getElementById("myP").style.display);
Páginas relacionadas
Tutorial CSS: Exibição e visibilidade CSS
Referência CSS: propriedade de exibição
❮ Objeto de estilo