- Javascript DOM Style display Property
- Browser Compatibility
- Syntax
- Property Values
- Default Value
- Return Value
- More Examples
- Example
- Example
- Example
- Related
- Style display Property
- Browser Support
- Syntax
- Property Values
- Technical Details
- More Examples
- Example
- Example
- Example
- Example
- Все значения свойства display
- Значение none
- Значение block
- Значение inline
- Значение inline-block
- Значения table-*
- Вертикальное центрирование с table-cell
- Значения list-item, run-in и flex
Javascript DOM Style display Property
The display property gets and sets the element’s display type.
The HTML elements are mostly «inline» or «block» elements:
- An inline element has floating content on its left and right side.
- A block element fills the entire line.
In other words, an inline element can share the same line with other elements, while the block element always starts a new line.
The display property can show or hide an element.
It is similar to the visibility property.
If you set display:none, it hides the entire element and removes it from the layout, while visibility:hidden means that the element will be invisible, but the element occupy in its original position and size.
If an element is a block element, its display type can also be changed with the float property.
Browser Compatibility
Syntax
Return the display property:
let a = object.style.display;
object.style.display = value;
Property Values
Value | Element is rendered as |
---|---|
block | a block-level element |
compact |
Default Value
Return Value
A String, representing the display type of an element.
More Examples
Difference between the display property and the visibility property:
function demoDisplay() < document.getElementById("myP1").style.display = "none"; > function demoVisibility() < document.getElementById("myP2").style.visibility = "hidden"; >
!DOCTYPE html> html> body> p id="myP1">This is some text. p id="myP2">This is some text. input type="button" onclick="demoDisplay()" value="Hide text with display property"> input type="button" onclick="demoVisibility()" value="Hide text with visibility property"> script> function demoDisplay() !-- w w w . d e m o 2 s . c o m--> document.getElementById("myP1").style.display = "none"; > function demoVisibility() < document.getElementById("myP2").style.visibility = "hidden"; >
Example
Toggle between hiding and showing an element:
function myFunction() < let x = document.getElementById('myDIV'); if (x.style.display === 'none') < x.style.display = 'block'; > else < x.style.display = 'none'; > >
!DOCTYPE html> html> head> style> #myDIV !-- w w w . d e m o 2 s. c o m --> width: 500px; height: 500px; background-color: lightblue; > body> p>Click the "Test" button to toggle between hiding and showing the DIV element: button onclick="myFunction()">Test div id="myDIV"> This is my DIV element. p>b>Note: The element will not take up any space when the display property set to "none". script> function myFunction() < let x = document.getElementById('myDIV'); if (x.style.display === 'none') < x.style.display = 'block'; > else < x.style.display = 'none'; > >
Example
Difference between «inline», «block» and «none»:
let whichSelected = x.selectedIndex; let sel = x.options[whichSelected].text; let elem = document.getElementById("mySpan"); elem.style.display = sel;
!DOCTYPE html> html> body> p>Select a display type in the list below to change span id="mySpan" style="color:blue;">this span element's display type. select onchange="myFunction(this);" size="3"> option>block option>inline option>none !-- w w w . d e m o 2 s . c o m --> script> function myFunction(x) < let whichSelected = x.selectedIndex; let sel = x.options[whichSelected].text; let elem = document.getElementById("mySpan"); elem.style.display = sel; >
Example
Return the display type of a
element:
console.log(document.getElementById("myP").style.display);
!DOCTYPE html> html> body> p id="myP" style="display:none;">This is a p element. button type="button" onclick="myFunction()"> Return the display type of p p id='demo'> script> function myFunction() < document.getElementById('demo').innerHTML = document.getElementById("myP").style.display; >
Related
- Javascript DOM Style cssFloat Property
- Javascript DOM Style cursor Property
- Javascript DOM Style direction Property
- Javascript DOM Style display Property
- Javascript DOM Style emptyCells Property
- Javascript DOM Style filter Property
- Javascript DOM Style flex Property
demo2s.com | Email: | Demo Source and Support. All rights reserved.
Style display Property
The display property sets or returns the element’s display type.
Elements in HTML are mostly «inline» or «block» elements: An inline element has floating content on its left and right side. A block element fills the entire line, and nothing can be displayed on its left or right side.
The display property also allows the author to show or hide an element. It is similar to the visibility property. However, if you set display:none , it hides the entire element, while visibility:hidden means that the contents of the element will be invisible, but the element stays in its original position and size.
Tip: If an element is a block element, its display type can also be changed with the float property.
Browser Support
Syntax
Return the display property:
Property Values
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
|