When a button has visibility: hidden or display: none, accessibility behavior is different, and this is a very common interview question.
π 1οΈβ£ display: none
button {
display: none;
}
β Accessibility Behavior
- β Button is removed from the DOM layout
- β Screen readers cannot detect it
- β Keyboard users cannot tab to it
- β Treated as if it does not exist
π Use Case
- Completely removing UI elements
- Conditional rendering
π 2οΈβ£ visibility: hidden
button {
visibility: hidden;
}
β Accessibility Behavior
- β Button is not visible
- β Not announced by screen readers
- β Not focusable via keyboard
- β Space is still reserved in layout
π Use Case
- Temporarily hiding UI without shifting layout
π Key Difference (Accessibility View)
| Property | Visible | Screen Reader | Keyboard Focus | Layout Space |
|---|---|---|---|---|
| display: none | β | β | β | β |
| visibility: hidden | β | β | β | β |
β οΈ Important Accessibility Rule
If an element is not visible, it should usually not be accessible.
So both properties correctly remove the button from accessibility tools.
β How to Hide Visually but Keep Accessible
If you want the button to be available to screen readers but not visible, use visually hidden CSS:
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0 0 0 0);
white-space: nowrap;
}
<button class="visually-hidden">Submit</button>
β Screen readers can read it
β Keyboard users can reach it
β Not visible on screen
π― Short Interview Answer
Both
display: noneandvisibility: hiddenremove the element from screen readers and keyboard navigation.display: nonealso removes it from layout, whilevisibility: hiddenkeeps its space.
To hide visually but keep accessibility, use a visually-hidden technique instead.
β One-line summary
Hidden UI is also hidden from assistive technologies—unless you intentionally preserve accessibility.