CSS Display Property


CSS display property defines how an HTML element is displayed on the webpage.

If it is displayed as a block then it takes the full width of the webpage. If it is displayed as inline then it takes space equal to the width of the content.

Look at the image below to understand.

Difference in block and inline display
Difference in block and inline display

The CSS display property is used to specify the type of box used for an element.

Defining a display type not only affects the element itself but also its child elements.

Apart from the block and inline, there are many other values of the display property. We will see them below.


CSS Display Property Values

There are mainly 6 values of CSS display property.

  1. block - The element is displayed as a block element. It starts on a new line and takes up the whole width.
  2. inline - The element is displayed as an inline element. It does not start on a new line. It only takes up as much width as necessary.
  3. inline-block - The element is displayed as an inline-level block container. The element itself is formatted as an inline element, but you can apply height and width values.
  4. none - The element is completely removed. It will not be displayed, and it will not take up any space.
  5. flex - The element is displayed as a block-level flex container.
  6. grid - The element is displayed as a block-level grid container.
CSS display property example
Change the display value and see how
testing text 12
behaves. (justify-content value is space-between for testing purposes)

Let's look at these values in detail.


1. Display block

The block value displays the elements as a block. It breaks the line at the start and end of the element and covers the whole width of the container.

The elements having block as a default value are:

Example

.block {
    display:block;
}
Try it

2. Display inline

The inline value displays the element within the line. It does not change lines or take any extra space.

When an element is displayed as inline, it takes only the width of the content. It does not support height and width properties.

Every HTML element has some default display value. Here are the elements that have inline as a default value:

Example

.inline {
    display:inline;
}
Try it

3. Display inline-block

The inline-block value displays the element as an inline element, but you can apply height and width values.

It does not break the line and takes only width of the content. But it supports height and width properties.

The elements having inline-block as a default value are :

Example

.inline-block {
    display: inline-block;
}
Try it

4. Display none

The none value removes the element from the document flow. It will not be displayed, and it will not take up any space.

Example

.hide {
    display: none;
}
Try it

5. Display flex

The flex value displays the element as a block-level flex container. It is used to create flexbox layouts.

Flexbox is a layout mode in CSS3. It is a one-dimensional layout mode. It can arrange the elements in a row or column, but not both.

Example

.flex {
    display: flex;
}
Try it

Conclusion

Understanding the display property is very important for a web developer. It helps us to create a better layout for our web pages.

Practice the examples given in this tutorial to understand the display property better.