Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS Display and Visibility

CSS Display and Visibility


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

Box 1

Box 2

Box 3


Hiding an Element - display:none or visibility:hidden

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

visibility:hidden hides an element, but the hidden element still occupies the same space as before. In other words, although the element is hidden, it still affects the layout.

Example

h1.hidden {visibility:hidden;}

Try it »

display:none hides an element, and the hidden element does not occupy any space. In other words, the element is not only hidden, but the space it originally occupied also disappears from the page layout.

Example

h1.hidden {display:none;}

Try it »


CSS Display - Block and Inline Elements

A block element is an element that takes up the full width and has a line break before and after it.

Examples of block elements:

An inline element only takes up as much width as necessary and does not force a line break.

Examples of inline elements:


How to Change an Element's Display

You can change inline elements to block elements and vice versa to make the page look composed in a particular way while still following web standards.

The following example displays list items as inline elements:

Example

li {display:inline;}

Try it »

The following example displays a span element as a block element:

Example

span {display:block;}

Try it »

**Note:** Changing an element's display type depends on how the element is displayed and what kind of element it is. For example, setting an inline element to display:block does not allow nested block elements inside it.


More examples

How to display an element as inline.

This example demonstrates how to display an element as inline.

How to display an element as a block element.

This example demonstrates how to display an element as a block element.

How to use the collapse property of a table.

This example demonstrates how to use the collapse property of a table.