CSS Display Property Example

In this article we will show, what is and how to use the display property from CSS.

The css specification is maintained by the word wide web consortium (W3C). Here we will introduce the values  for display property defined in w3c specification.

1. Introduction

The display property lets you define how a certain html element should be displayed.

Each layout element has a default value to display property. The default value for most of elements is blocked or inlined.

For almost all examples introduced in this article, we will use the css class above to distinct one element from the other:


border: 3px solid #3fa4e2;
width: 200px;
height: 30px;
margin: 10px;

border:3px solid #d0d62f;
height: 30px;
margin: 10px;

border:3px solid #40b76e;
height: 30px;
margin: 10px;

border:3px solid #db5c1c;

2. Values to property Display

There are many values to display property. Bellow we will explain each one, with simple examples.

2.1. Commons display values

The tree most familiar display values are : block, inline and inline-block.

  • block : Causes an element to generate a block box.


 <div class="a" style="display:block "> div a</div>
 <div class="b" style="display:block" > div b</div>
 <div class="c" style="display:block" > div c</div>
 <div class="d" style="display:block "> div d</div>
Image 1 – Using block value for display property
  • inline: Causes an element to generate one or more inline boxes.


 <div class="a" style="display:inline;" > div a</div>
 <div class="b" style="display:inline" > div b</div>
 <div class="c" style="display:inline" > div c</div>
Image 2 – Using inline value for display property

You can see that inline value respects left & right margins and padding, but not top & bottom and cannot has a width and height set

  • inline-blockCauses an element to generate an inline-level block container. The inside of an inline-block is formatted as a block box, and the element itself is formatted as an atomic inline-level box.


 <div class="a" style="display:inline-block" > div a</div>
 <div class="b" style="display:inline-block" > div b</div>
 <div class="c" style="display:inline-block" > div c</div>
Image 3 – Using inline-block value for display property

2.2. Lists and Inherit

Unlike of inline property, inline-block property  respects top & bottom margins, padding and respects height and width.

  • list-item: Causes an element to generate a principal block box and a marker box.


 <div class="a" style="display:list-item" > div a</div>
 <div class="b" style="display:list-item" > div b</div>
 <div class="c" style="display:list-item" > div c</div>
Image 4 – Using list-item value for display property
  • none
    This value causes an element to not appear in the document. It has no effect on layout.
  • inherit
    Takes the same specified value as the property for the element’s parent.


<div class="d" style="display:block" > div d
      <div class="a" style="display:inherit" > div a</div>
 <div class="d" style="display:inline" > div d
     <div class="a" style="display:inherit" > div a</div>
Image 5 – Using inherit value for display property

For inherit property we have two examples . In first one, the parent element (div d) has display:block, than,  the child element that has display:inherit will behave like display block. In second example, the parent element (div d) has display:inline, than,  the child element that has display:inherit will behave like display inline.

2.3. Table display values

The display property has many properties that permits you to build a table like html:

  • table – Behave like a <table> element
  • inline-table – Specifies that an element defines an inline-level table.
  • table-row-group – Behavse like a <tbody> element.
  • table-header-group – Behaves like a <thead> element.
  • table-footer-group – Behaves like a <tfoot> element.
  • table-row – Behaves like a <tr> element.
  • table-column-group – Behaves like a <colgroup> element.
  • table-column – Behaves like a <col> element.
  • table-cell – Behaves like a <td> element.
  • table-caption – Behaves like a <caption> element.

If you want to create a table in HTML we usually do this:


<table border=1>

The result is

Image 6 – Simple html table.

Instead, you can use the display property to build a css table :


 .table { display: table }
 .tr { display: table-row }
 .thead { display: table-header-group }
 .tbody { display: table-row-group }
 .tfoot { display: table-footer-group }
 .col { display: table-column }
 .colgroup { display: table-column-group }
 .td { display: table-cell }
 .th { display: table-cell }
 .caption { display: table-caption }

We need to create a border to better see the table, thus, I create a simple border class bellow


.border {
 border:1px solid black;

The CSS code that replaces the HTML table could be:


 <div class="table" >
 <div class="thead">
 <div class="tr">
 <div class="td border">Day</div>
 <div class="td border">Sales</div>
 <div class="tfoot">
 <div class="tr">
 <div class="td border">Total</div>
 <div class="td border">175</div>
 <div class="tbody">
 <div class="tr">
 <div class="td border">Monday</div>
 <div class="td border ">100</div>
 <div class="tr ">
 <div class="td border">Tuesday</div>
 <div class="td border">75</div>

And, the result :

Image 7 – Using tables values for display property

3. Conclusion

We have many values to property display. You can do so much with display property than show or hide html elements. You can identify html elements and create beautiful table-less layout. Now, you can use more this property in yours css.

If you have any suggestion for this article, please tell-us.

Poliana Reis

Poliana graduated in Electric Engineering (E.E.) in 2006, when she started to work in the software development area. In 2012 she received the Masters of Computer Science. She is a multi-talent software developer with a leadership vein. Her greatest passion is the full stack of WEB development with major specialty in JEE platform.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button