CSS visibility
Created By: chatGPT
CSS visibility is a property that controls the visibility of an element in the document. It differs from the
display
property, as it does not remove the element from the document flow. Instead, it can either show or hide an element while maintaining its physical space on the page.The visibility property has three possible values:- visible: This is the default value. The element is visible.
- hidden: The element is not visible, but it still takes up space in the layout.
- collapse: This value only applies to table rows, columns, and groups, making them invisible and collapsing their space.
Here's how to use the visibility property in CSS:
.hidden {
visibility: hidden;
}
.visible {
visibility: visible;
}
.table-row {
visibility: collapse;
}
Example: If you want to hide a paragraph without affecting the layout of surrounding elements, you can apply the
hidden
class to it:<p class="hidden">This paragraph is hidden but still occupies space.</p>
<p>This paragraph is visible.</p>
JavaScript can also be used to dynamically change the visibility of elements. With the
This will hide the element with the ID myElement without removing it from the flow of the document.style
property, you can modify the visibility
CSS property directly.document.getElementById('myElement').style.visibility = 'hidden';