关于 CSS 特异性的具体问题

发布于 2024-12-01 02:53:38 字数 515 浏览 0 评论 0原文

我有一个关于 CSS Specificity 的非常具体的问题,我无法清楚地理解; http://coding.smashingmagazine.com/ 2007/07/27/css-specificity-things-you-should-know/

如果我有 2 个定义不矛盾的属性/属性的选择器,两者都会属性的仍然被应用或者它的工作方式是,它只是比较选择器,而不关心它们内部定义的内容。

所以如果我们有;

.menu1 {color:red}
p.menu1 {font-size:10px}

这里两个选择器都引用“menu1”,但定义了不相关的属性(颜色/字体大小)

所以我的问题是特异性仍然重要并且只考虑2个中的1个吗?我的问题更多是关于实际实施如何发生。

I have a very specific question on CSS Specificity, something which I could not clearly understand on;
http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

If I have 2 selectors which define non-contradicting properties/attributes, will both of the attributes still get applied OR the way it works is, it just compares the selectors, without bothering what is defined inside them.

So if we have;

.menu1 {color:red}
p.menu1 {font-size:10px}

Here both the selectors refer to "menu1", but define unrelated attribues (color/font-size)

So my question is does the Specificity still matter and only 1 of the 2 will be considered ? My question is more about how the actual implementation happens.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2024-12-08 02:53:38

不,在这种情况下,特异性并不重要,因为您只是向 menu1 类添加一个额外的属性,这不会更改任何现有规则。但是,如果您颠倒规则的顺序并尝试覆盖 color

p.menu1 {color:blue}
.menu1 {color:red}

不太具体规则 .menu1 {color:red} < em>不会覆盖更具体p.menu1 {color:blue},即使“红色”规则出现在之后>“蓝色”规则(尝试一下)。

当然,如果您将第二条规则更改为 p.menu1 {color:red} 它将被覆盖,因为这两个规则将具有相同的特异性。

No, in this case the specificity does not matter as you are simply adding an extra property to the menu1 class, which isn't changing any existing rules. However, if you reverse the order of the rules and attempt to override color:

p.menu1 {color:blue}
.menu1 {color:red}

then the less specific rule .menu1 {color:red} will not override the more specific p.menu1 {color:blue}, even though the 'red' rule appears after the 'blue' rule (try it out).

Of course, if you change the second rule to p.menu1 {color:red} it will override, as the two rules would then have the same specificity.

疾风者 2024-12-08 02:53:38

在文章的概述中,第 11 点说:

11。最后定义的规则将覆盖任何先前的冲突规则。

(其中“最后一个规则”可以替换为“最后一个或最具体的规则”)

因此,如果存在不同的属性,则不会覆盖任何内容。这两个规则相结合,因此 p.menu1 的字体大小均为 10 像素,颜色为红色。第二条规则仍然有一个更具体的选择器,但在您指定可能会被覆盖的通用样式之前,它根本不相关。

所有这些在实现中是如何完成的,是一个实现细节,所以我不知道。

In the article's overview, point 11 says:

11. The last rule defined overrides any previous, conflicting rules.

(where "last rule" may be replaced with "last or most specific rule")

So if there are different properties then nothing is overridden. The two rules are combined, so p.menu1 will all have a font size of 10 pixels and be red in color. The second rule still has a more specific selector, but it's simply not relevant until you specify a common style that will potentially be overridden.

How all of this is done in implementation is, well, an implementation detail, so I don't know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文