Compare the three ways to hide an element by what actually changes: display: none removes layout, focus, and accessibility exposure; visibility: hidden keeps the space and can transition; opacity: 0 keeps everything, including focus and screen-reader exposure. Pick by layout, focus, and assistive technology, not by looks.
Frontend interview practice question
visibility: hidden vs display: none: layout, focus, and screen readers
Interview quick answer
Interview focus
This CSS interview question tests whether you can explain visibility hidden vs display none: Layout and A11y, connect it to production trade-offs, and handle common follow-up questions.
- visibility hidden vs display none: Layout and A11y explanation without falling back to memorized definitions
- Layout and Visibility reasoning, edge cases, and production failure modes
- How you would answer the most likely CSS interview follow-up
Use this CSS interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
Full interview answer
The decision to explain
Three ways to hide an element, three different results. display: none removes it from layout, the accessibility tree, and focus order. visibility: hidden keeps its space, removes it from the accessibility tree and focus, and still lets you transition it. opacity: 0 keeps space, focus, and screen-reader exposure, which is a bug for hidden menus and a feature for custom checkboxes. Pick by what should happen to layout, focus, and assistive technology, not by what looks right.
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.
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) |
Example Comparison
.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: hiddenelements are usually not focusable and hidden from assistive technologies.display: noneelements are completely removed from the accessibility tree.- Use
aria-hidden='true'explicitly when managing visibility dynamically for accessibility compliance.
Performance & Animations
- Use
visibilitywhen you need to hide/show elements smoothly with transitions (opacity fade). - Use
displaywhen 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: hiddenhides elements but keeps layout space reserved.display: noneremoves elements from both view and flow.- Use
visibilityfor smooth transitions,displayfor structural layout changes. - Always handle visibility with accessibility in mind when toggling UI elements.
Use this as one explanation rep, then continue with the CSS interview questions cluster or a guided prep path.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.