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.
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) |
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.
Why 1SMARTchicken?
See More →
Thanks for your support!
CSS Books
All are affiliate links. Thank you for your support!
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
Feedback
If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.