Posts in current category

CSS Display (Display) and Visibility (Visibility)


May 03, 2021 22:00 CSS


Table of contents


CSS Display (Display) and Visibility (Visibility).

Both the CSS display property and the visibility property can be used to hide an element, but the two properties have different definitions, read the following in detail.


The display property sets how an element should be displayed, and the visibility property specifies whether an element should be visible or hidden.

Box 1
CSS Display (Display) and Visibility (Visibility)
Box 2
CSS Display (Display) and Visibility (Visibility)
Box 3
CSS Display (Display) and Visibility (Visibility)

Hidden elements - display:none or visibility:hidden

Hiding an element can be done by setting the display property to "none" or by setting the visibility property to "hidden." Note, however, that these two methods produce different results.

Visibility: Hidden can hide an element, but the hidden element still takes up the same amount of space as before it was hidden. That is, the element, while hidden, still affects the layout.

h1.hidden {visibility:hidden;}

Try it out . . .

Display:none can hide an element, and the hidden element does not take up any space. That is, not only is the element hidden, but the space that the element originally occupies disappears from the page layout.

h1.hidden {display:none;}

Try it out . . .

CSS Display - Blocks and inline elements

A block element is an element that takes up all widths and is a line break before and after.

Examples of block elements:

  • <h1>
  • <p>
  • <div>

Inline elements require only the necessary width and do not force line changes.

Examples of inline elements:

  • <span>
  • <a>

How to change an element display

You can change the inline element to a block element, and vice versa, to make the page appear to be grouped in a specific way and still follow web standards.

The following example shows the list item as an inline element:

li {display:inline;}

Try it out . . .

The following example uses the span element as a block element:

span {display:block;}

Try it out . . .

Note: Changing the display type of an element depends on how the element is displayed and what kind of element it is. F or example, an inline element is set to display:block is not allowed to have nested block elements inside it.


CSS Display (Display) and Visibility (Visibility)

More instances

How to display inline elements of an element.

This example shows how to display an inline element of an element.

How to display a block element of an element.

This example shows how to display a block element of an element.

How to use thecollapse property of a table.

This example shows how to use thecollapse property of a table.