Who to Change the Default Display Value using CSS



Not Found
404
Not Found

We can change the default value of how will the HTML element be rendered using the CSS Display property.

NOTE − The <div> element has a default CSS display set to block and <span> element has a default CSS display set to inline.

Solution

Selector {
   display: /*value*/
}

Example

Let’s see an example for changed default CSS Display property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Change default CSS Display</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>Change default CSS-Display</legend>
<div id="container">
<div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div>
</div><br>
</body>
</html>

Output

This will produce the following output −

Example

Let’s see another example of changed default CSS Display property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Change default CSS Display</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>Change default CSS-Display</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 −

After clicking ‘Hide Primary Colors’ button −

raja
Updated on 07-Jan-2020 11:54:25

Advertisements