CSS 特异性的最佳实践?

发布于 2024-10-07 18:27:26 字数 1431 浏览 1 评论 0原文

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

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

发布评论

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

评论(7

妥活 2024-10-14 18:27:27

这是一个相当困难的问题,因为选择器本身及其在工作表中的位置都很重要。而且,编写 CSS 的真正方法并不只有一种,这一事实也无济于事。

我想如果你使用 ID 和标签选择器会是最好的。另外使用属性选择器:

#contact-form input { ... }
#contact-form input[type=email] { ... }
#contact-form select { ... }

但是您应该提到,强烈建议将该表放在其他表的顶部,例如:

<link type="stylesheet" href="/styles/contact-form.css" />
<link type="stylesheet" href="/styles/main.css" />

为什么采用这种方法?

通常,开发人员希望表单在整个网站上看起来都相同,这就是为什么他会使用标签名称选择器:

input  { ... }
select { ... }

这些选择器比 #contact-form input 弱,因此它们不会覆盖任何内容。然而,有时需要覆盖一些规则,因此开发人员将使用 #contact-form input 选择器,这在这种情况下是很自然的。

如果作为建议附加了工作表,则表示开发人员的样式将覆盖您的样式,尽管两者都具有具有完全相同强度的选择器。这就是规则的位置很重要的原因。

That's a quite hard cause both, the selector itself and its location in sheet do matter. Also the fact that there is no only one, true way of writing CSS doesn't help.

I guess it would be the best if you use ID and tag selector. Additionally use attribute selector:

#contact-form input { ... }
#contact-form input[type=email] { ... }
#contact-form select { ... }

However you should mention that it's strongly recommended to put that sheet on the top of others, eg:

<link type="stylesheet" href="/styles/contact-form.css" />
<link type="stylesheet" href="/styles/main.css" />

Why that approach?

Usually a developer will want to forms look the same all over the website, that's why he will use tag name selector:

input  { ... }
select { ... }

These selectors are weaker that #contact-form input so they won't override anything. However sometimes it's necessary to override some rules so the developer will use #contact-form input selector which is pretty natural in such case.

If sheets has been attached as a recommendation says developer's styles will override yours, despite the fact that both have selectors with exactly same strength. That's why the location of the rules matter.

微暖i 2024-10-14 18:27:27

我想你已经回答了你自己的问题:

#contactform .contact_input { 背景:白色; }

I think you've answered your own question on this one:

#contactform .contact_input { background: white; }

CSS!

苍风燃霜 2024-10-14 18:27:27

你最好的选择是使用类似于以下的模式:

input.JSCF_formInput{
   color: white !important;
   ...
}

这样你的样式是独特的{JSCF for Josiah Sprauge Contact Form}并且具体且指定为重要......

your best bet is to use a schema somewat similar to the following:

input.JSCF_formInput{
   color: white !important;
   ...
}

that way your styles are unique {JSCF for Josiah Sprauge Contact Form} and specific and specified as important...

国粹 2024-10-14 18:27:27

#site-wrapper 输入 { 背景:黄色; } [...]我不希望它推翻
我的联系表格样式参考
类,.contact_input { 背景:白色; }

如果您使用这些选择器,您基本上不能:#id.class 更具体,仅此而已。
我建议您从第一条规则中删除 #site-wrapper 选择器位,因为它意味着通用

#site-wrapper input { background: yellow; } [...] I don't want it to over-rule
my contact form styles that refer to
classes, .contact_input { background: white; }

You basically can't, if you use those selectors: #ids are more specific than .classes, and that's it.
I would suggest you drop the #site-wrapper selector bit from the first rule, since it's meant to be generic.

你げ笑在眉眼 2024-10-14 18:27:27

我想你应该采用某种“命名空间”模式。

例如,以 #ds_contactform 开始所有样式,并使其尽可能具体,而不会扰乱语义并方便维护代码。

我不认为使用特定的选择器(带有 #id 甚至多个 id)是低效的。

PS 我还推荐 Andy Clarke 的精彩文章 CSS:特异性战争

I guess you should adopt kind of "namespace" pattern.

E.g. start all your styles with #ds_contactform and make them as specific as you can without messing with semantics and convinient maintenance of the code.

I don't think that using specific selectors (with an #id or even several ids) is ineffecient whatsoever.

P.S. And I also recommend Andy Clarke's fantastic article CSS: Specificity Wars.

转身泪倾城 2024-10-14 18:27:27

您可以考虑使用 BEM 方法,它具有非常出色的类命名样式,可以通过多种方式解决特殊性问题。

You may consider of using BEM methodology, It has really awesome class naming styles which solves specificity issues in many ways.

夜血缘 2024-10-14 18:27:27

如果您使用 !important,它将始终覆盖 ID 或类的样式。查看此链接了解更多信息;
http://www.electrictoolbox.com/using-important-css/

您可以查看此链接以获得有关特异性的更多信息;
http://www.smashingmagazine.com/ 2007/07/27/css-speciality-things-you-should-know/

If you use !important, it will always override the style with respect to an ID or a class. Have a look at this link of further information;
http://www.electrictoolbox.com/using-important-css/

You can have a look at this link in order to get more information on specificity;
http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

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