»

CSS display

The display property is the most important CSS property for controlling layout.

The display Property

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.

Block-level 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:

  • <div>
  • <h1> - <h6>
  • <p>
  • <form>
  • <header>
  • <footer>
  • <section>

Inline 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:

  • <span>
  • <a>
  • <img>

The display Property Values

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

Display: none;

display: none; is commonly used with JavaScript to hide and show elements without deleting and recreating them. Take a look at our last example on this page if you want to know how this can be achieved.

The <script> element uses display: none; as default.

This panel contains a <div> element, which is hidden by default (display: none;). It is styled with CSS, and we use JavaScript to show it (change it to (display: block;).

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:

Example

css
li { display: inline; }

Try it Yourself »

== 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:

Example

css
span { display: block; }

Try it Yourself »

The following example displays <a> elements as block elements:

Example

css
a { display: block; }

Try it Yourself »

Hide an Element

display:none

display:none | Removes the element and the page will be displayed as if the element is not there

media-xuPbcC

visibility:hidden

visibility:hidden | Hides the element, but keeps the space provided for it

media-yCPKEl

More Examples

Differences between display: none; and visibility: hidden; This example demonstrates display: none; versus visibility: hidden;

Showing more display types This example demonstrates some more display types.

Using CSS together with JavaScript to show content This example demonstrates how to use CSS and JavaScript to show an element on click.

CSS Display/Visibility Properties

Property Description
display Specifies how an element should be displayed
visibility Specifies whether or not an element should be visible