CSS Visibility
The visibility property is essential for controlling the visual presence of elements without necessarily destroying their layout footprint. It is often confused with display: none, but the two serve very different purposes.
Visibility Values
- Visible: The default value. The element is shown.
- Hidden: The element is invisible, but it still occupies the same space in the document flow.
- Collapse: Used primarily for table rows/columns/groups. It removes the element (like display: none) but recalculates the table structure dynamically.
Visibility vs Display vs Opacity
Understanding the nuance is key to layout stability:
display: noneremoves the element from the DOM flow entirely. Layout shifts occur.visibility: hiddenmakes it invisible, but layout is preserved (empty space remains). It is not interactive.opacity: 0makes it transparent. It preserves layout space AND interactivity (buttons can still be clicked).
