Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the initial and unset values?

Tags:

css

A simple example:

HTML

<p style="color:red!important">      this text is red      <em>         this text is in the initial color (e.g. black)     </em>     this is red again </p> 

CSS

em {     color:initial;     color:unset; } 

What is the difference between initial and unset? Only supports browsers

CanIUse: CSS unset value

Developer Mozilla Web CSS initial

like image 515
zloctb Avatar asked Nov 20 '15 18:11

zloctb


People also ask

What is the difference between inherit and initial?

inherit : Get the property from the parent element. initial : The default value for the property (the browser default). unset : Acts as either inherit or initial. It'll act as inherit if the parent has a value that matches, or else it will act as initial.

What is unset value in CSS?

The unset CSS keyword resets a property to its inherited value if the property naturally inherits from its parent, and to its initial value if not.

What does the property value unset do?

The unset CSS keyword resets a property of an element to its inherited value if the property naturally inherits from its parent, or to its initial value if it does not inherit.

What does all unset do?

Thus, Applying the unset value to the all property will reset all the inherited properties to inherit and all of the non-inherited properties to initial. This is the only reason for the existence of the new unset keyword value! Otherwise, we could use the inherit and initial values instead.


1 Answers

According to your link:

unset is a CSS value that's the same as "inherit" if a property is inherited or "initial" if a property is not inherited

Here is an example:

pre {    color: #f00;  }  .initial {    color: initial;  }  .unset {    color: unset;  }
<pre>    <span>This text is red because it is inherited.</span>    <span class="initial">[color: initial]: This text is the initial color (black, the browser default).</span>    <span class="unset">[color: unset]: This text is red because it is unset (which means that it is the same as inherited).</span>  </pre>

A scenario where the difference matter is if you are trying to override some CSS in your stylesheet, but you would prefer the value is inherited rather than set back to the browser default.

For instance:

pre {    color: #00f;  }  span {    color: #f00;  }  .unset {    color: unset;  }  .initial {    color: initial;  }
<pre>    <em>Text in this 'pre' element is blue.</em>    <span>The span elements are red, but we want to override this.</span>    <span class="unset">[color: unset]: This span's color is unset (blue, because it is inherited from the pre).</span>    <span class="initial">[color: initial]: This span's color is initial (black, the browser default).</span>  </pre>
like image 69
Josh Crozier Avatar answered Oct 19 '22 13:10

Josh Crozier