gpt4 book ai didi

css - 初始值和未设置值之间有什么区别?

转载 作者:数据小太阳 更新时间:2023-10-29 09:10:42 25 4
gpt4 key购买 nike

一个简单的例子:

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

initialunset 有什么区别?仅支持浏览器

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 - 初始值和未设置值之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33834049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com