Display Property

Learn how the CSS display property controls layout behavior, including block, inline, flex, grid, and more.

Loading...
Display Property

What is the display property?

The display property defines how an HTML element should be displayed on the web page. Its major task is to control the element’s layout and positioning.

Basic Syntax:

element {
  display: value;
}

Let’s dive into the most common display values.

display: block;

If you set an element’s display property to block then it will start on a new line and take the entire width of its parent container.

You can set the width and height properties for such elements.

Default block-level elements examples include <div>, <p>, <h1> - <h6>, <header>,<footer>.

HTML:

<div>This is a block-level element.</div>
<span>This is span.</span>

CSS:

div {
  border: 2px solid red;
}
 
span {
  border: 2px solid green;
}

Output:

css-display-block

Here, <div> is a block-level element by default, occupying the full width of its container. On the other hand, <span> does not possess the default block-level behavior and only occupies the width required by its content.

You can convert the <span> element to a block-level element by setting its display property to block.

span {
  border: 2px solid green;
  display: block;
}

Output:

css-display-block

Now, <span> is a block-level element, occupying the full width of its container.


display: inline;

If you set an element’s display property to inline then it does not start on a new line and takes the width required by its content.

You can not set the width and height properties for such elements.

Default inline elements examples include <span>, <a>, and <img>.

HTML:

<span>This is a span inline element.</span>
<a>This is a hyperlink inline element.</a>
<section>This is not a default inline element.</section>

CSS:

span {
  border: 2px solid green;
}
 
a {
  border: 2px solid red;
}
 
section {
  border: 2px solid blue;
}

Output:

css display inline

Here, <span> and <a> are inline elements by default, meaning they do not initiate a new line and occupy the width necessary for their content. On the other hand, <section> is not an inline element by default, causing it to start on a new line.

You can convert the <section> element to an inline element by setting its display property to inline.

section {
  border: 2px solid blue;
  display: inline;
}

Output:

css display inline

Now, <section> is an inline element.


display: inline-block;

If you set an element’s display property to inline-block then it contains the features of both block and inline elements. It takes up only as much width as necessary, but you can set its height and width properties.

HTML:

<div>This is a default block-level element.</div>
<span>This is a default inline element.</span>

CSS:

div {
  border: 2px solid red;
}
 
span {
  border: 2px solid green;
  height: 200px;
}

Output:

css display inline block

Here, I’m attempting to assign a height property to a <span> element, but it appears to have no impact due to its inline nature.

By changing its display property to inline-block, I can successfully apply the height property to the <span> element.

span {
  border: 2px solid green;
  height: 200px;
  display: inline-block;
}

Output:

css display inline block


display: flex;

The display: flex; property creates a flex container that can be used for creating flexible layouts.

HTML:

<div class="container">
   <div class="item">Item 1</div>
   <div class="item">Item 2</div>
   <div class="item">Item 3</div>
</div>

CSS:

.container {
  display: flex;
  border: 2px solid black;
}
 
.item {
  height: 100px;
  width: 100px;
  border: 1px solid green;
  margin: 10px;
}

Output:

css display flex


display: inline-flex;

The display: inline-flex; property is similar to display: flex; but the container on which display: inline-flex; is applied, behaves like an inline element.

HTML:

<div class="container">
   <div class="item">Item 1</div>
   <div class="item">Item 2</div>
   <div class="item">Item 3</div>
</div>

CSS:

.container {
  display: inline-flex;
  border: 2px solid black;
}
 
.item {
  height: 100px;
  width: 100px;
  border: 1px solid green;
  margin: 10px;
}

Output:

css display inline flex


display: grid;

The display: grid; property transforms the container into a grid container and its children into grid items. By having grid layouts, you have precise control over both rows and columns.

HTML:

<div class="container">
   <div class="item">Item 1</div>
   <div class="item">Item 2</div>
   <div class="item">Item 3</div>
</div>

CSS:

.container {
  display: grid;
  border: 2px solid black;
}
 
.item {
  border: 1px solid green;
  margin: 10px;
  padding: 10px;
}

Output:

css display grid


display: inline-grid;

The display: inline-grid; property is similar to display: grid; but the container on which display: inline-grid; is applied, behaves like an inline element.

HTML:

<div class="container">
   <div class="item">Item 1</div>
   <div class="item">Item 2</div>
   <div class="item">Item 3</div>
</div>

CSS:

.container {
  display: inline-grid;
  border: 2px solid black;
}
 
.item {
  border: 1px solid green;
  margin: 10px;
  padding: 10px;
}

Output:

css display inline grid


display: none;

If you set an element’s display property to none, then it will be completely invisible and take up no space within the document.

HTML:

<p>This element is not hidden.</p>
<div>This element is hidden.</div>
<p>This element is not hidden.</p>

CSS:

div {
  display: none;
  height: 200px;
}
 
p {
  border: 2px solid green;
}

Output:

css display none

Another CSS property that can be used to hide elements is visibility: hidden;. It’s important not to mix up visibility: hidden; with display: none;, as visibility: hidden; retains the empty space occupied by the hidden element, whereas display: none; does not occupy any space within the document.

div {
  visibility: hidden;
  height: 200px;
}
 
p {
  border: 2px solid green;
}

Output:

css display none


display: inherit;

If you set an element’s display property to inherit, then it will inherit the display value from its parent element.

HTML:

<div>
  <span>This will inherit the display value from its parent.</span>
</div>

Here, <div> is a block-level element and <span> is an inline element.

CSS:

span {
  display: inherit;
  border: 2px solid green;
  padding: 10px;
}

The display property of <span> element is set to inherit, so it will inherit the display value from its parent element (<div>) and act as a block-level element.

Output:

css display inherit


display: initial;

The display: initial; property sets the display property of an element to its initial value, which is usually inline for most elements.

HTML:

<div>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Saepe, a?</div>

CSS:

div {
  display: initial;
  border: 1px solid green;
}

Output:

css display initial


Display Cheatsheet

You can refer to this cheatsheet for quick reference:

CSS Display Cheatsheet

Download link for the cheatsheet