๐Ÿ‘๏ธ CSS visibility โ€” Complete Tutorial

๐ŸŒ What Is visibility?

The visibility property controls whether an element isvisible or hidden โ€” but unlike display: none, the element still **occupies space in the layout** even when hidden.

>>โ€œUse visibility when you want to hide an element but keep its layout space.โ€

Note

โœ” Keeps layout space even when hidden
โœ” Child elements may still become visible (special case!)
โœ” Useful for toggling UI elements without shifting layout

๐Ÿ“ฆ Syntax

visibility syntax

visibility: visible | hidden | collapse;

๐Ÿ“Œ Values Explained

ValueDescription
visibleDefault. Element is shown normally.
hiddenElement is hidden, but still takes up space.
collapseSpecial: Used by table rows/columns โ†’ space collapses.

๐ŸŽฏ visibility vs display: none

PropertyEffect
visibility: hiddenHides but reserves layout space
display: noneRemoves element from layout completely

๐Ÿ“Œ 1. Basic Example

Hide an element

.box {
  visibility: hidden;
}

The box is invisible but still pushes surrounding elements.

๐Ÿ“Œ 2. Toggle Visibility Without Reflow

No layout shift

.popup {
  visibility: hidden;
}

.popup.show {
  visibility: visible;
}

Great for dropdowns, tooltips, hover effects, and transitions.

๐Ÿ“Œ 3. Visibility with Hover

Hover to show

.menu-item .submenu {
  visibility: hidden;
}

.menu-item:hover .submenu {
  visibility: visible;
}

๐Ÿ“Œ 4. Child Visibility Override

If a parent has visibility: hidden, setting visibility: visible on a child **makes it visible again** โ€” unlike display: none.

Child override

.parent {
  visibility: hidden;
}

.parent .child {
  visibility: visible;
}

Note

โš ๏ธ This can lead to unexpected behavior โ€” use carefully.

๐Ÿ“Œ 5. visibility: collapse for Tables

collapse is meaningful in table layouts. It hides rows/columns and collapses space.

Collapse table row

tr.hidden-row {
  visibility: collapse;
}

๐Ÿ“Œ 6. Animating Visibility (with opacity)

visibility itself cannot animate smoothly, but it's often paired with opacity.

Smooth fade

.fade {
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease;
}

.fade.show {
  opacity: 1;
  visibility: visible;
}

๐Ÿงช Real-World Examples

1๏ธโƒฃ Tooltip Show/Hide

Tooltip example

.tooltip {
  visibility: hidden;
  opacity: 0;
  transition: 0.2s;
}

.button:hover .tooltip {
  visibility: visible;
  opacity: 1;
}

2๏ธโƒฃ Dropdown Menu

Dropdown

.dropdown ul {
  visibility: hidden;
}

.dropdown:hover ul {
  visibility: visible;
}

3๏ธโƒฃ Hiding Items Without Breaking Layout

Reserved space

.label {
  visibility: hidden;
}

โš ๏ธ When NOT to Use visibility

  • โŒ For removing elements โ€” use display: none
  • โŒ For layouts (Grid/Flexbox already handle spacing)
  • โŒ For accessibility hiding โ€” use hidden attribute or aria-hidden

๐Ÿ”ฅ Summary

The visibility property controls whether an element is visually shown while keeping or removing its layout space. It's perfect for dropdowns, tooltips, transitions, and situations wheredisplay: none would cause layout shifts.

>>โ€œUse visibility to hide without breaking the layout.โ€

Learn more โ†’MDN Docs ๐Ÿ“˜