What is the difference between visibility:hidden and display:none?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In web development, controlling the appearance and visibility of elements on a webpage is critical. CSS (Cascading Style Sheets) provides various ways to manage these aspects, two of which are using visibility: hidden; and display: none;. Both properties might seem to serve the same purpose at first glance—making elements invisible—but they operate in fundamentally different ways and have unique impacts on the layout and accessibility of a web page.
Understanding visibility: hidden;
The CSS property visibility: hidden; makes an element invisible to the user. However, it is crucial to note that even though the element is invisible, it still occupies its designated space in the layout. This is akin to making something transparent rather than removing it altogether.
Example of visibility: hidden;:
In this example, the text inside the <div> will not be visible, but the space that it occupies will still be part of the webpage layout, potentially affecting elements around it.
Understanding display: none;
On the other hand, display: none; does more than just make an element invisible—it completely removes the element from the document flow. This means the element does not occupy any space, and the layout behaves as if the element does not exist at all.
Example of display: none;:
When this code is rendered, the <div> and its content are not only invisible, but there is also no trace of the space that it would ordinarily occupy. The document's layout adjusts accordingly to fill in the space.
Comparative Table
Here's a brief table summarizing the differences between visibility: hidden; and display: none;:
| Property | Visibility | Occupies Space | Document Flow Impact | Accessibility Impact |
visibility: hidden; | Invisible, but exists | Yes | No change | Remains in the DOM |
display: none; | Completely gone | No | Removed from flow | Removed from the DOM |
Additional Differences: Interaction and Accessibility
Event Interaction
An element set to visibility: hidden; can still interact with JavaScript events because it remains in the document flow. For instance, if you have a JavaScript function that triggers on a click event, it would still work even if the element is invisible.
Contrastingly, display: none; removes the element from the document structure entirely, meaning it cannot interact with any events. Clicks or other interactions will act as if the element isn't there at all.
Accessibility Impact
Elements with visibility: hidden; are generally still accessible to screen readers unless explicitly instructed otherwise via ARIA (Accessible Rich Internet Applications) attributes. This is because the element is still in the DOM and considered part of the page.
However, elements hidden using display: none; are often ignored by screen readers as they are removed from the DOM. This makes display: none; a stronger choice for content that should not be accessible to screen readers.
Animation and Transitions
Using visibility with CSS transitions allows you to animate the process of an element becoming visible or hidden, because visibility can be delayed in CSS transitions. Display, however, cannot be animated or transitioned because it does not support interpolation between its values.
Conclusion
While visibility: hidden; and display: none; might seem interchangeable at first glance, they serve different purposes and have distinct effects on the layout and functionality of web pages. Choosing between them depends on whether you need to maintain the layout integrity (using visibility) or remove the element entirely from the document flow (using display). Understanding these subtleties allows developers to make more informed decisions while designing and building web interfaces.

