displayproperty is the most important CSS property for controlling layout.
display property specifies if or how an element is displayed.
Every HTML element has a default display value depending on what type of element it is. The default display value for most elements is
A block-level element always starts on a new line and takes up the full width available (stretches out to the left and right as far as it can).
Examples of block-level elements:
- <h1> – <h6>
An inline element does not start on a new line and only takes up as much width as necessary.
This is an inline <span> element inside a paragraph.
Examples of inline elements:
Override The Default Display Value
As mentioned, every element has a default display value. However, you can override this.
Changing an inline element to a block element, or vice versa, can be useful for making the page look a specific way, and still follow the web standards.
A common example is making inline
<li> elements for horizontal menus:
Note: Setting the display property of an element only changes how the element is displayed, NOT what kind of element it is. So, an inline element with
display: block; is not allowed to have other block elements inside it.
The following example displays <span> elements as block elements:
The following example displays <a> elements as block elements:
Hide an Element – display:none or visibility:hidden?
Hiding an element can be done by setting the
display property to
none. The element will be hidden, and the page will be displayed as if the element is not there:
visibility:hidden; also hides an element.
However, the element will still take up the same space as before. The element will be hidden, but still affect the layout:
|inline||Displays an element as an inline element (like <span>). Any height and width properties will have no effect||Try|
|block||Displays an element as a block element (like <p>). It starts on a new line, and takes up the whole width||Try|
|contents||Makes the container disappear, making the child elements children of the element the next level up in the DOM||Try|
|flex||Displays an element as a block-level flex container||Try|
|grid||Displays an element as a block-level grid container||Try|
|inline-block||Displays an element as an inline-level block container. The element itself is formatted as an inline element, but you can apply height and width values||Try|
|inline-flex||Displays an element as an inline-level flex container||Try|
|inline-grid||Displays an element as an inline-level grid container||Try|
|inline-table||The element is displayed as an inline-level table||Try|
|list-item||Let the element behave like a <li> element||Try|
|run-in||Displays an element as either block or inline, depending on context||Try|
|table||Let the element behave like a <table> element||Try|
|table-caption||Let the element behave like a <caption> element||Try|
|table-column-group||Let the element behave like a <colgroup> element||Try|
|table-header-group||Let the element behave like a <thead> element||Try|
|table-footer-group||Let the element behave like a <tfoot> element||Try|
|table-row-group||Let the element behave like a <tbody> element||Try|
|table-cell||Let the element behave like a <td> element||Try|
|table-column||Let the element behave like a <col> element||Try|
|table-row||Let the element behave like a <tr> element||Try|
|none||The element is completely removed||Try|
|initial||Sets this property to its default value. Read about initial||Try|
|inherit||Inherits this property from its parent element. Read about inherit|