指南针/蓝图有一个 CSS 规则“body.bp table”和“body.bp td”把所有的桌子都搞乱了,有什么解决办法吗?

发布于 2024-10-01 05:53:00 字数 529 浏览 4 评论 0原文

我有 CSS 定义,例如

.one-table
  width: 500px

.one-table td
  padding: 0

,但由于 Compass / Blueprint 0.8.17 有,

body.bp table { width: 100% }
body.bp td { padding: 4px }

所以我的表格混乱了。添加 table.one-table 也没有帮助。我认为它正在失去特异性......我对此感到非常惊讶,因为该类直接位于表格上,而另一个类则将类应用于body。为什么它会被覆盖?有什么好的解决方法吗?

我无法使用 id 因为它是一个模板,我需要使其足够通用。我也不愿意使用 !important 因为如果以后 IE css def 也需要使用 important 该怎么办。我无法升级到最新的指南针,因为这样做可能也意味着修复整个网站的问题。谢谢。

I have CSS definitions such as

.one-table
  width: 500px

.one-table td
  padding: 0

but since Compass / Blueprint 0.8.17 has

body.bp table { width: 100% }
body.bp td { padding: 4px }

so my tables are messed up. And adding table.one-table doesn't help either. I think it is losing to the specificity... which I am quite surprised because the class is directly onto the table, while the other one has the class applied onto body. Why is that overridden and what is a good workaround for it?

I can't use id because it is a template and I need to make it generic enough. I also feel reluctant to use !important because what if later the IE css def needs to use important also. I can't upgrade to the newest compass because doing so might mean fixing things for the whole website too. thanks.

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

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

发布评论

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

评论(1

玩心态 2024-10-08 05:53:00

以下是计算 CSS 特异性的方法。 id 选择器的特异性为 100,类选择器为 10,元素选择器为 1。要计算 CSS 特异性,只需将这些值相加即可。

body.bp table 的特异性为 12 (1 + 10 + 1),table.one-table 为 11 (1 + 10),因此 body.one-table 的特异性为 12 (1 + 10 + 1)。 bp 表 获胜。

要在保留 one-table 类的同时解决此问题,您可以尝试使用这些选择器:

# Specifity is 20 (vs 12).
.bp .one-table
  width: 500px

# Specifity is 21 (vs 12).
.bp .one-table td
  padding: 0

或者您可以仅使用相同的选择器并覆盖蓝图规则。不过,这些规则将适用于所有桌子。

# This must come after the blueprint rules.
body.bp table
  width: 500px

body.bp td
  padding: 0

蓝图 mixin 的作用域似乎是 bp 类。这可能会导致您必须将 .bp 放在许多 CSS 规则前面。如果不需要蓝图范围,您应该只使用蓝图的顶级 mixin:

+blueprint

Here's a how to calculate CSS specificity. id selector has a specificity of 100, class selector 10, and element selector 1. To calculate the CSS specificity, just sum the values.

body.bp table's specificity is 12 (1 + 10 + 1) and table.one-table is 11 (1 + 10), so body.bp table wins.

To fix this while keeping the one-table class, you can try using these selectors:

# Specifity is 20 (vs 12).
.bp .one-table
  width: 500px

# Specifity is 21 (vs 12).
.bp .one-table td
  padding: 0

Or you can just use the same selectors and override the blueprint rules. These rules will apply to all tables though.

# This must come after the blueprint rules.
body.bp table
  width: 500px

body.bp td
  padding: 0

It seems that the blueprint mixin is scoped to the class bp. This may cause you to have to put .bp in front of many of your CSS rules. If the blueprint scoping is not necessary, you should just use the top-level mixin for blueprint:

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