CSS中的初始值和未设置值之间有什么区别?

前端之家收集整理的这篇文章主要介绍了CSS中的初始值和未设置值之间有什么区别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一个简单的例子:

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;
}

初始和未设置之间有什么区别?仅支持浏览器

CanIUse: CSS unset value

Developer Mozilla Web CSS initial

解决方法

将我的评论移至答案

根据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

这是一个例子:

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>

如果您尝试覆盖样式表中的某些CSS,则差异很重要的情况,但您希望继承该值而不是设置回浏览器默认值。

例如:

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>

猜你在找的CSS相关文章