Overview
The display property and visibility property are used to control the layout of the page. They dictate how, and if, an element is shown on the page.
The display property is often used to change the natural display manner of HTML tags. For instance, making an inline tag display as block, or vice versa.
Note
The following are basic examples. Towards the bottom of this page you’ll find a complete list of all applicable properties, where you can find more information on the properties discussed, and sometimes find more involved properties not discussed on this page.
Display
The following are some examples of using the display property.
/* displays an element as a block element which starts on a new
line, and takes up the whole width of the parent element */
div {
display: block;
}
/* displays an element as an inline element which has no set
width and height and is the size of the content*/
div {
display: inline;
}
/* hides the element and collapses the space to be used by
other elements; it will not appear and leaves no whitespace */
div {
display: none;
}
Visibility
The following is the most common example of using the visibility property.
/* hides the element but DOES NOT collapse the space, leaving
an area of whitespace the size of the hidden element */
div {
visibility: hidden;
}
Display vs. Visibility
This is a brief comparison of how each removes an element from the page.
display: none; | visibility: hidden; |
---|---|
Hides the element and collapses the space to be used by other elements; it will not appear and leaves no whitespace | Hides the element but DOES NOT collapse the space, leaving an area of whitespace the size of the hidden element |
Good for completely hiding the element leaving no trace that it existed in that spot | Good for temporarily hiding an element until the page loads and then using JavaScript to show the element (will not cause the content around it to shift as it appears) |
CSS Notes:
- The “inherit”, “initial” and “unset” keywords can be used with any CSS property to set its value
- In CSS there are many ways to express a color value in a property
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.