The display
property is the most important CSS property for controlling layout.
The display
property is used to specify how an element is shown on a web page.
Every HTML element has a default display value, depending on what type of element it is. The default display value for most elements is block
or inline
.
The display
property is used to change the default display behavior of HTML elements.
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).
The <Div> element is a block-level element.
Examples of block-level elements:
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:
The display
property has many values:
Value | Description |
---|---|
inline | Displays an element as an inline element |
block | Displays an element as a block element |
contents | Makes the container disappear, making the child elements children of the element the next level up in the DOM |
flex | Displays an element as a block-level flex container |
grid | Displays an element as a block-level grid container |
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 |
inline-flex | Displays an element as an inline-level flex container |
inline-grid | Displays an element as an inline-level grid container |
inline-table | The element is displayed as an inline-level table |
list-item | Let the element behave like a <li> element |
run-in | Displays an element as either block or inline, depending on context |
table | Let the element behave like a <table> element |
table-caption | Let the element behave like a <caption> element |
table-column-group | Let the element behave like a <colgroup> element |
table-header-group | Let the element behave like a <thead> element |
table-footer-group | Let the element behave like a <tfoot> element |
table-row-group | Let the element behave like a <tbody> element |
table-cell | Let the element behave like a <td> element |
table-column | Let the element behave like a <col> element |
table-row | Let the element behave like a <tr> element |
none | The element is completely removed |
initial | Sets this property to its default value |
inherit | Inherits this property from its parent element |
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:
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: