Propiedad de visualización de estilo
Ejemplo
Configure un elemento <div> para que no se muestre:
document.getElementById("myDIV").style.display = "none";
Definición y uso
La propiedad de visualización establece o devuelve el tipo de visualización del elemento.
Los elementos en HTML son en su mayoría elementos "en línea" o "bloque": un elemento en línea tiene contenido flotante en su lado izquierdo y derecho. Un elemento de bloque llena toda la línea y no se puede mostrar nada en su lado izquierdo o derecho.
La propiedad de visualización también permite al autor mostrar u ocultar un elemento. Es similar a la propiedad de visibilidad . Sin embargo, si establece display:none
, oculta todo el elemento, mientras visibility:hidden
que significa que el contenido del elemento será invisible, pero el elemento permanecerá en su posición y tamaño originales.
Sugerencia: si un elemento es un elemento de bloque, su tipo de visualización también se puede cambiar con la propiedad flotante.
Compatibilidad con navegador
Property | |||||
---|---|---|---|---|---|
display | Yes | Yes | Yes | Yes | Yes |
Sintaxis
Devuelve la propiedad de visualización:
object.style.display
Establezca la propiedad de visualización:
object.style.display = value
Valores de propiedad
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 |
Detalles técnicos
Valor por defecto: | en línea |
---|---|
Valor devuelto: | Una cadena, que representa el tipo de visualización de un elemento. |
Versión CSS | CSS1 |
Más ejemplos
Ejemplo
Diferencia entre la propiedad de visualización y la propiedad de visibilidad:
function demoDisplay() {
document.getElementById("myP1").style.display = "none";
}
function demoVisibility() {
document.getElementById("myP2").style.visibility = "hidden";
}
Ejemplo
Alternar entre ocultar y mostrar un elemento:
function myFunction() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
Ejemplo
Diferencia entre "en línea", "bloque" y "ninguno":
function myFunction(x) {
var whichSelected = x.selectedIndex;
var sel = x.options[whichSelected].text;
var elem = document.getElementById("mySpan");
elem.style.display = sel;
}
Ejemplo
Devuelve el tipo de visualización de un elemento <p>:
alert(document.getElementById("myP").style.display);
páginas relacionadas
Tutorial de CSS: Visualización y visibilidad de CSS
Referencia CSS: propiedad de visualización
❮ Objeto de estilo