CSS
CSS Display

CSS Layout - The display Property

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


What is the display Property?

The display property specifies how an element is displayed on a web page.

Every HTML element has a default display value, which is typically either:

  • block
  • inline

You can use the display property to change this default behavior.


Block-level Elements

A block-level element always:

  • Starts on a new line
  • Takes up the full width available (stretching left to right)

Example Block Elements:

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

Inline Elements

An inline element:

  • Does not start on a new line
  • Takes up only as much width as necessary

Example Inline Elements:

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

CSS Display Property Values

The display property in CSS has a wide range of values that define how elements are rendered on the page.


Common Display Values

ValueDescription
inlineDisplays an element as an inline element
blockDisplays an element as a block element
contentsMakes the container disappear, promoting its children up in the DOM
flexDisplays the element as a block-level flex container
gridDisplays the element as a block-level grid container
inline-blockDisplays as inline-level but allows height and width
inline-flexDisplays as an inline-level flex container
inline-gridDisplays as an inline-level grid container
inline-tableBehaves like an inline <table>
list-itemBehaves like an <li> element
run-inDisplays as either block or inline, based on context

Table Display Values

ValueDescription
tableBehaves like a <table>
table-captionBehaves like a <caption>
table-column-groupBehaves like a <colgroup>
table-header-groupBehaves like a <thead>
table-footer-groupBehaves like a <tfoot>
table-row-groupBehaves like a <tbody>
table-cellBehaves like a <td>
table-columnBehaves like a <col>
table-rowBehaves like a <tr>

Utility Values

ValueDescription
noneThe element is completely removed (hidden)
initialResets the property to its default value
inheritInherits the property value from its parent

Use the correct display value to control layout behavior precisely for your elements.

Display Property - display: none; and Overriding Default Values

display: none;

The display: none; property is commonly used with JavaScript to hide elements without deleting or recreating them.

The <script> element uses display: none; as its default value, which hides it by default.

Example: Showing a Panel with display: none;

<button onclick="document.getElementById('panel').style.display = 'block'">Click to show panel</button>
 
<div id="panel" style="display: none;">
  This is the panel content!
</div>