继承的CSS属性的特性 [英] Specificity of inherited CSS properties

查看:237
本文介绍了继承的CSS属性的特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继承属性所接受的CSS特性的级别是什么?我阅读了W3建议关于CSS的特性,所以我明白如何计算直接针对同一元素的CSS规则的不同特性,但我没有提到具有给予继承的属性的水平。

What is the level of CSS specificity received by inherited properties? I read through the W3 recommendation regarding CSS specificity and so I understand how to calculate the different specificities of css rules which are directly targeting the same element, but I see no mention there of the level of specificity given to inherited attributes.

特别是,我遇到的问题与标题元素有关,但我非常想知道这一点。

In particular, the issue I'm encountering has to do with header elements, though I would be very interested to understand this in general.

例如,下面是一段HTML代码片段:

For example, here's a snippet of HTML:

<h2>This should be black</h2>
<div class="all_red_text">
    <h2>This should be red</h2>
</div>

现在,如果我包含这样的CSS:

Now if I include some CSS like this:

.all_red_text { color: red; }

我会得到我期望的结果。另一方面,如果我包括的css是

I will get the result I expect. On the other hand, if I the css which I included was

h2 { color: black; }
.all_red_text { color: red; }

然后全部在第一种情况下,有一些默认的浏览器CSS可以被继承属性覆盖,但是当第二个示例中手动指定相同的属性时,它优先于继承的属性。

then all the text will be black. In the first case there is some default browser CSS which is able to be overridden by the inherited property, but then when the same property is manually specified in the second example it takes precedence over the inherited property.

推荐答案

任何与元素直接匹配的声明将优先于从元素父元素继承的属性。特异性与那无关。

Any declaration that matches element directly will get priority over the property that's inherited from the element's parent. Specificity has nothing to do with that.

这篇关于继承的CSS属性的特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆