Differences between CSS display: none; and visibility: hidden;


There are moments when you wish to visually hide elements in an application (that is, remove them from the DOM but leave them on the screen). There are several methods by which you can accomplish this. Using the visibility property with a hidden value(visibility:hidden;) or the display property with a none value (display:none;) are two common approaches.

Both approaches make the element hide, but they have different effects on how it behaves. In this articles we are going to learn about the difference between display:none; and visibility:hidden;

CSS visibility:hidden

This CSS property will cause an element to fill the screen's space even though it will be hidden from view. This means that the contents that are present before and after the element with this property will be rendered, but the hidden element will still have exactly the same amount of space to occupy. Even though it doesn't display on the screen and takes up space, the HTML element is still there in the DOM.

Syntax

Following is the syntax for CSS visibility property −

visibility: visible| hidden | collapse | initial | inherit;

Example

Let's look at the following example, where we are going to use the CSS visibility:hidden property.

<!DOCTYPE html>
<html>
<head>
   <style>
      .tp {
         display: flex;
         gap: 20px;
      }
      .x {
         height: 100px;
         width: 150px;
         font-size: 30px;
         text-align: center;
         font-family: verdana;
         color: #DE3163;
      }
      .x1 {
         background-color: #E8DAEF;
      }
      .x2 {
         background-color: #DFFF00;
         visibility: hidden;
      }
      .x3 {
         background-color: #CCCCFF;
      }
   </style>
</head>
<body style="background-color:#EAFAF1">
   <div class="tp">
      <div class="x1 x">A</div>
      <div class="x2 x">B</div>
      <div class="x3 x">C</div>
   </div>
</body>
</html>

When we run the above code, it will generate an output consisting of the div box out of which one is applied with CSS visibility property displayed on the webpage.

CSS display:none

However, display: none accomplishes a similar task by hiding the element from screen. It won't leave any space in this instance, and the behavior gives the impression that the element with this property doesn't exist. The element is still there in the DOM, despite this property having no effect on it. The element is just hidden within the DOM. This CSS property cannot be used to remove an element from the DOM.

Syntax

Following is the syntax for CSS display property −

display: none | inline | block | inline-block;

Example

In the following example, we are going to use the CSS display:none property.

<!DOCTYPE html>
<html>
<head>
   <style>
      .tp {
         padding: 50px;
         width: max-content;
         display: flex;
         border: 1px solid #DE3163;
      }
      .x {
         height: 50px;
         width: 130px;
      }
      .x1 {
         background-color: #DFFF00;
         margin-right: 30px;
      }
      .x2 {
         background-color: #229954;
         margin-right: 30px;
         display: none;
      }
      .x3 {
         background-color: #212F3D;
      }
   </style>
</head>
<body style="background-color:#F4ECF7">
   <div class="tp">
      <div class="x1 x"></div>
      <div class="x2 x"></div>
      <div class="x3 x"></div>
   </div>
</body>
</html>

On running the above code, the output window will pop up, displaying the div box out of which one is applied with display property on the webpage.

.

Updated on: 08-Jan-2024

577 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements