What is the difference between visibility: hidden and display: none?

LowIntermediateCss
Preparing for interviews?

Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.

Quick Answer

Both visibility and display control element rendering in CSS, but they behave differently. visibility: hidden hides the element visually while preserving its layout space, whereas display: none removes the element completely from the document flow.

Answer

Overview

The visibility and display properties determine whether elements appear and how they affect layout. They differ in whether the element continues to occupy space or not.

Property

Effect

Layout Impact

Can Be Animated?

visibility: hidden

Element is invisible but still occupies space

✅ Yes, layout remains intact

✅ Yes (opacity transitions possible)

display: none

Element is completely removed from layout flow

❌ No space reserved

❌ No (toggle only on re-render)

Comparison of visibility: hidden vs display: none

Example Comparison

CSS
.hidden-element {
  visibility: hidden; /* Space remains occupied */
}

.none-element {
  display: none; /* Removed from layout entirely */
}
                  

In this scenario, the .hidden-element will not be visible but its space will remain. The .none-element will collapse and other elements will move up to fill its place.

Accessibility Implications

      • visibility: hidden elements are usually not focusable and hidden from assistive technologies.
      • display: none elements are completely removed from the accessibility tree.
      • Use aria-hidden='true' explicitly when managing visibility dynamically for accessibility compliance.

Performance & Animations

      • Use visibility when you need to hide/show elements smoothly with transitions (opacity fade).
      • Use display when you need to completely remove elements from the document flow (e.g., dropdown menus).
      • Combining both (opacity + pointer-events: none) can create interactive fade effects without layout jumps.

Still so complicated?

visibility: hidden is like making a ghost — you can’t see it, but it still exists in the room. display: none is like removing the object from the room entirely.

Summary
      • visibility: hidden hides elements but keeps layout space reserved.
      • display: none removes elements from both view and flow.
      • Use visibility for smooth transitions, display for structural layout changes.
      • Always handle visibility with accessibility in mind when toggling UI elements.
Similar questions
Guides
26 / 30