CSS
🎯 CSS visibility Property
The visibility CSS property controls whether an HTML element is visible or hidden while preserving its layout space.
Unlike display: none, the element is hidden but still occupies space on the page, so the layout does not shift.
🔹 Why Use visibility?
The visibility property is useful when you want to hide elements without affecting the page structure or layout flow.
- Hides elements without changing layout
- Useful for UI transitions and toggles
- Prevents layout shifting
- Works well with dynamic interfaces
🧩 visibility Values
1️⃣ visible (Default)
The element is fully visible on the page. This is the default behavior.
div {
visibility: visible;
}
2️⃣ hidden
The element becomes invisible but still occupies space in the layout.
div {
visibility: hidden;
}
This is the key difference from display: none, which removes the element completely from the layout.
3️⃣ collapse
Used mainly for table elements. Rows or columns are hidden without taking up space.
tr {
visibility: collapse;
}
In non-table elements, it behaves similarly to hidden.
4️⃣ initial
Resets the property to its default value (visible).
5️⃣ inherit
Inherits the visibility value from its parent element.
📌 Quick Summary
-
🧠 Overview
- visible → element is shown normally
- hidden → element is invisible but keeps space
- collapse → removes table rows/columns space (tables only)
- initial → resets to default value
- display: none → hides and removes from layout (comparison)
