CSS (Cascading Style Sheets) is a powerful tool used to control the presentation and layout of web pages. One of its key properties is visibility, which allows you to control the visibility of elements on a webpage. In this guide, we will explore the CSS visibility property and provide examples to help you understand its usage.
The visibility property in CSS determines whether an element is visible or hidden. It affects the rendering of the element and its space in the layout. There are three possible values for the visibility property:
1. Visible (default): When the visibility property is set to “visible”, the element is displayed normally. It is visible to the user and takes up space in the layout.
Example:
“`html
.box {
visibility: visible;
}
“`
2. Hidden: When the visibility property is set to “hidden”, the element is not visible, but it still takes up space in the layout. It is as if the element is transparent or invisible, but its space is preserved.
Example:
“`html
.box {
visibility: hidden;
}
“`
3. Collapse: The “collapse” value is primarily used for table-related elements. When applied to table rows, columns, or groups, it hides the element and removes its space from the layout. This can be useful when you want to hide specific table elements without affecting the overall structure.
Example:
“`html
.table-row {
visibility: collapse;
}
Visible row |
Collapsed row |
Visible row |
“`
It’s important to note that when an element’s visibility is set to “hidden” or “collapse”, it is still present in the HTML structure and can be accessed by assistive technologies, search engines, and screen readers. It is simply not visible to the user.
Additionally, the visibility property does not affect the visibility of child elements. If a parent element is set to “hidden” or “collapse”, its child elements will still be visible if their visibility is set to “visible”.
In conclusion, the CSS visibility property allows you to control the visibility of elements on a webpage. Whether you want to hide an element while preserving its space or collapse table-related elements, the visibility property gives you the flexibility to achieve your desired layout. By understanding and utilizing this property effectively, you can enhance the overall user experience of your website.