Style display egenskab
- Forrige side direction
- Næste side emptyCells
- Gå tilbage til forrige niveau HTML DOM Style Object
Definition og brug
display
Egenskaber indstiller eller returnerer elementets visningstype.
HTML-elementer er hovedsageligt "inline" eller "block" elementer: inline-elementer har både venstre og højre flydende indhold. Block-elementer fylder hele rækken, og der kan ikke vises noget indhold på deres venstre eller højre side.
display
属性还允许作者显示或隐藏元素。它类似于 visibility 属性。但是,如果设置 display:none
,它会隐藏整个元素,而 visibility:hidden
意味着元素的内容将不可见,但元素会保持其原始位置和大小。
提示:如果元素是块元素,也可以通过 float 属性更改其显示类型。
另请参阅:
CSS 教程:CSS Display 和 visibility
CSS 参考手册:display 属性
实例
Eksempel 1
设置不显示 <div> 元素:
document.getElementById("myDIV").style.display = "none";
Eksempel 2
display 属性和 visibility 属性的区别:
function demoDisplay() { document.getElementById("myP1").style.display = "none"; } function demoVisibility() { document.getElementById("myP2").style.visibility = "hidden"; }
Eksempel 3
在隐藏和显示元素之间切换:
function myFunction() { var x = document.getElementById('myDIV'); if (x.style.display === 'none') { x.style.display = 'block'; } else { x.style.display = 'none'; } }
Eksempel 4
"inline"、"block" 与 "none" 的区别: function myFunction(x) { var whichSelected = x.selectedIndex; var sel = x.options[whichSelected].text; var elem = document.getElementById("mySpan"); elem.style.display = sel; }
Eksempel 5
Return the display type of the <p> element:
alert(document.getElementById("myP").style.display);
Syntax
Return the display property:
object.style.display
Set the display property:
object.style.display = value
Attribute value
Value | Description |
---|---|
block | The element is rendered as a block-level element. |
compact | The element is presented as a block-level or inline element. It depends on the context. |
flex | The element is presented as a block-level flexible box. A new feature in CSS3. |
inline | The element is presented as an inline element. Default. |
inline-block | The element is presented as a block box within an inline box. |
inline-flex | The element is presented as an inline-level flexible box. A new feature in CSS3. |
inline-table | The element is presented as an inline table (such as <table>), without line breaks before and after the table. |
list-item | The element is presented as a list. |
marker |
This value sets the content before or after the box to be a marker (marker) Used with :before and :after pseudo-elements. Otherwise, this value is the same as "inline". |
none | The element is not displayed. |
run-in | The element is presented as a block-level or inline element. It depends on the context. |
table | The element is presented as a block table (block table) (such as <table>), with line breaks before and after the table. |
table-caption | The element is presented as a table caption (such as <caption>). |
table-cell | The element is presented as a table cell (such as <td> and <th>). |
table-column | The element is presented as a cell column (such as <col>). |
table-column-group | The element is presented as a group of one or more columns (such as <colgroup>). |
table-footer-group | The element is presented as a table footer row (such as <tfoot>). |
table-header-group | The element is presented as a table header row (such as <thead>). |
table-row | The element is presented as a table row (such as <tr>). |
table-row-group | The element is presented as a group of one or more lines (such as <tbody>). |
initial | Sets this property to its default value. See initial. |
inherit | Inherits this property from its parent element. See inherit. |
Technical details
Default value: | inline |
---|---|
Return value: | A string representing the display type of the element. |
CSS version: | CSS1 |
Browser support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |
- Forrige side direction
- Næste side emptyCells
- Gå tilbage til forrige niveau HTML DOM Style Object