In CSS, the display property specifies how an element is displayed, while the visibility property specifies if an element should be visible or hidden. The display property is very useful in CSS. However, it is also one of the least well-known, especially by those just learning CSS.
Visibility Property
The visibility property can be used to show or hide elements. You should note that if you set the visibility property to invisible, the element still occupies the space on the page. If you want to make an element invisible and not take up space on the page, use the display property and set the value to none
.
The visibility property can be assigned the following values: visible
(default), hidden
, collapse
(for tables only), and inherit
. Some browsers do not support collapse
or inherit
. You should only use this property if you are trying to hide an element and still allow it to occupy the space on the page.
.div1 {
visibility:hidden;
}
Display Property
The display property is very flexible and is a great tool in the CSS toolbox. There are many values that can be assigned to this property. However, the most common values are inline
and block
. This is because we can use these two values to change some of the element’s default display behavior.
For example, some HTML elements are of type: block
such as headings, paragraphs, and divs. Other HTML elements such as span
and anchor
are inline
. We can easily change the behavior of an inline
element to act as a block
element by using this property.
You should note that changing the display type of an element changes only how the element is displayed, not what kind of element it is. For example, an inline
element set to display:block
is not allowed to have block elements nested within it.
span {
display:block;
}
li {
display:inline;
}
Here is a listing of the possible values you can specify for the display property. Not all of the values are supported by all of the browsers.
Value | Description |
---|---|
none | The element will not be displayed. |
block | The element is displayed as a block element. |
inline | The element is displayed as an inline element. |
inline-block | Placed as an inline element with other adjacent elements but behaves like a block element. |
inline-table | Element is displayed as an inline table. |
inherit | Inherits the display property of the parent element. |
list-item | Displayed as a list item. |
run-in | Becomes inline or block element depending on child elements. |
table | Element is displayed as a table. |
table-caption | Element is displayed as a table caption. |
table-cell | Element is displayed as a table cell. |
table-column | Element is displayed as a table column. |
table-column-group | Element is displayed as a table column group. |
table-footer-group | Element is displayed as a table footer group. |
table-header-group | Element is displayed as a table header row. |
table-row | Element is displayed as a table row. |
table-row-group | Element is displayed as a table row group. |
The values inline-table
, table
, table-caption
, table-cell
, table-column
, table-column-group
, table-row
, table-row-group
, and inherit
are not supported in Internet Explorer 7 and earlier. Internet Explorer 8 requires a !DOCTYPE
specified in the HTML document. Internet Explorer 9 does support the values.