0
0
CssComparisonBeginner · 3 min read

Display none vs Visibility hidden: Key Differences and Usage

The 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.

Factordisplay: nonevisibility: hidden
Effect on layoutElement is removed, no space takenElement is hidden but space remains
InteractivityElement is not interactiveElement is not interactive
Animation supportCannot animate display property directlyCan animate visibility and opacity smoothly
Screen readersUsually ignored by screen readersMay still be read by screen readers
Use caseRemove element completelyHide 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

html
<!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>
Output
You will see two coral boxes labeled 'Box 1' and 'Box 3' stacked vertically with space between them. 'Box 2' is not visible and does not take any space.
↔️

Visibility Hidden Equivalent

html
<!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>
Output
You will see three coral boxes stacked vertically. The middle box is invisible but its space remains, so Box 1 and Box 3 are separated by the hidden box's space.
🎯

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.
Neither property allows user interaction with the hidden element.
visibility: hidden supports smoother animations than display: none.
Use display: none to remove elements fully, and visibility: hidden to keep layout stable.