Computer >> Computer tutorials >  >> Programming >> CSS

Difference Between CSS Display and Visibility


We can hide or remove an element in a HTML document with CSS Visibility and CSS Display properties respectively. To the user, there might not seem any difference in using any of the two properties, but there is.

CSS Display − none does not render the element on the document and thus not allocating it any space.

CSS Visibility − hidden does renders the element on the document and even the space is allocated but it is not made visible to the user.

Example

Let’s see an example for CSS Display none −

<!DOCTYPE html>
<html>
<head>
<title>CSS Display None</title>
<style>
form {
   width:70%;
   margin: 0 auto;
   text-align: center;
}
* {
   padding: 2px;
   margin:5px;
   box-sizing: border-box;
}
input[type="button"] {
   border-radius: 10px;
}
.child{
   display: inline-block;
   height: 40px;
   width: 40px;
   color: white;
   border: 4px solid black;
}
.child:nth-of-type(1){
   background-color: #FF8A00;
}
.child:nth-of-type(2){
   background-color: #F44336;
}
.child:nth-of-type(3){
   background-color: #C303C3;
}
.child:nth-of-type(4){
   background-color: #4CAF50;
}
.child:nth-of-type(5){
   background-color: #03A9F4;
}
.child:nth-of-type(6){
   background-color: #FEDC11;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>CSS-Display-None</legend>
<div id="container">
<div class="child"></div><div class="child primary"></div><div class="child"></div><div class="child"></div><div class="child primary"></div><div class="child primary"></div>
</div><br>
<input type="button" value="Hide Primary Colors" onclick="displayNone()">
</fieldset>
</form>
<script>
var primaryColor = document.getElementsByClassName('primary');
function displayNone(){
   for(var i=0; i<3; i++)
   primaryColor[i].style.display = 'none';
}
</script>
</body>
</html>

Output

This will produce the following output −

Before clicking ‘Hide Primary Colors’ button −

Difference Between CSS Display and Visibility

After clicking ‘Hide Primary Colors’ button −

Difference Between CSS Display and Visibility

Example

Let’s see an example of CSS Visibility hidden −

<!DOCTYPE html>
<html>
<head>
<title>CSS Visibility hidden</title>
<style>
form {
   width:70%;
   margin: 0 auto;
   text-align: center;
}
* {
   padding: 2px;
   margin:5px;
   box-sizing: border-box;
}
input[type="button"] {
   border-radius: 10px;
}
.child{
   display: inline-block;
   height: 40px;
   width: 40px;
   color: white;
   border: 4px solid black;
}
.child:nth-of-type(1){
   background-color: #FF8A00;
}
.child:nth-of-type(2){
   background-color: #F44336;
}
.child:nth-of-type(3){
   background-color: #C303C3;
}
.child:nth-of-type(4){
   background-color: #4CAF50;
}
.child:nth-of-type(5){
   background-color: #03A9F4;
}
.child:nth-of-type(6){
   background-color: #FEDC11;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>CSS-Visibility-hidden</legend>
<div id="container">
<div class="child"></div><div class="child primary"></div><div class="child"></div><div class="child"></div><div class="child primary"></div><div class="child primary"></div>
</div><br>
<input type="button" value="Hide Primary Colors" onclick="visibilityHidden()">
</fieldset>
</form>
<script>
var primaryColor = document.getElementsByClassName('primary');
function visibilityHidden(){
   for(var i=0; i<3; i++)
   primaryColor[i].style.visibility = 'hidden';
}
</script>
</body>
</html>

Output

This will produce the following output −

Before clicking ‘Hide Primary Colors’ button −

Difference Between CSS Display and Visibility

After clicking ‘Hide Primary Colors’ button −

Difference Between CSS Display and Visibility