Display none vs Visibility hidden: Key Differences and Usage
display: none property hides an element and removes it from the page layout, so it takes no space. The visibility: hidden property hides the element but keeps its space reserved in the layout, making it invisible but still affecting page flow.Quick Comparison
Here is a quick side-by-side comparison of display: none and visibility: hidden properties.
| Factor | display: none | visibility: hidden |
|---|---|---|
| Effect on layout | Element is removed, no space taken | Element is hidden but space remains |
| Interactivity | Element is not interactive | Element is not interactive |
| Animation support | Cannot animate display property directly | Can animate visibility and opacity smoothly |
| Screen readers | Usually ignored by screen readers | May still be read by screen readers |
| Use case | Remove element completely | Hide element but keep layout space |
Key Differences
display: none completely removes the element from the page layout. This means the browser acts as if the element does not exist, so it takes no space and does not affect other elements. Because of this, the element is also not interactive and cannot be focused or clicked.
On the other hand, visibility: hidden hides the element visually but keeps its allocated space in the layout. The element is invisible but still affects the position of other elements around it. It is also not interactive, so users cannot click or focus it.
Another important difference is in animations: visibility: hidden can be smoothly animated with opacity changes, while display: none cannot be transitioned directly because the element is removed from the layout immediately.
Code Comparison
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Display None Example</title> <style> .box { width: 100px; height: 100px; background-color: coral; margin-bottom: 10px; } .hidden { display: none; } </style> </head> <body> <div class="box">Box 1</div> <div class="box hidden">Box 2 (hidden)</div> <div class="box">Box 3</div> </body> </html>
Visibility Hidden Equivalent
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Visibility Hidden Example</title> <style> .box { width: 100px; height: 100px; background-color: coral; margin-bottom: 10px; } .hidden { visibility: hidden; } </style> </head> <body> <div class="box">Box 1</div> <div class="box hidden">Box 2 (hidden)</div> <div class="box">Box 3</div> </body> </html>
When to Use Which
Choose display: none when you want to completely remove an element from the page and free up its space, such as hiding a menu or modal that should not affect layout.
Choose visibility: hidden when you want to hide an element but keep its space reserved, for example to maintain consistent layout or animate its appearance smoothly.
Remember that both make the element non-interactive, so neither can be clicked or focused while hidden.
Key Takeaways
display: none removes the element and frees its space in layout.visibility: hidden hides the element but keeps its space reserved.visibility: hidden supports smoother animations than display: none.display: none to remove elements fully, and visibility: hidden to keep layout stable.