css - 为什么 CSS 框架不必要地使用 !important 标签?

标签 css twitter-bootstrap zurb-foundation css-frameworks cascade-framework

与其说这是一个问题,不如说这是一场辩论,但我觉得互联网上没有太多内容涵盖这个主题。

例如,foundation 带有数百个 !important 标签,这些标签在我看来并不需要它们:

.text-center { text-align: center !important; } 

有很多与此类似的 css,在我看来这是不好的做法,我想回答的问题是为什么 css 框架要使用它们? Bootstrap 和 Foundation 是两个主要的 css 框架,都使用它们。

我一直被告知在 css 中使用重要的标签是非常糟糕的做法,应该只用于 IE。

最佳答案

如果您编写自己的 CSS,则可以在需要时自由添加更具体的规则:

.center        { text-align: center; }
.foobar        { text-align: left; }
.foobar.center { text-align: center; }

但是,CSS 框架无法预测您将如何排列 HTML。因此,执行 !important 而不是生成数千个更具体规则的组合更容易。示例:

.center               { text-align: center; }
.foobar               { text-align: left; }
.barbaz               { text-align: right; }
 /*
  * assuming .center must be centered regardless of other rules and
  * !important must be avoided at the same time, we need to do this
  */
.foobar.center        { text-align: center; }
.barbaz.center        { text-align: center; }
.foobar.barbaz.center { text-align: center; }

关于css - 为什么 CSS 框架不必要地使用 !important 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28042571/

相关文章:

css - 显示特定的 Tick 值

javascript - 如何使用 JavaScript 动态地将工作按钮添加到我的增量奖励系统中?

javascript - Bootstrap 错误验证颜色未显示

zurb-foundation - 在 zurb foundation 中禁用选项卡

css - bootstrap 3 navbar-right 没有带 navbar-fixed-top 的填充

css - 从 CSS 样式中排除多个页面

php - WordPress 在主题支持中检索缩略图

html - 用 CSS 变量覆盖 Bootstrap 4 Sass 变量?

css - 任何熟悉移动可见性和 zurbs 基础的人

javascript - 最小数值为 125 的正则表达式?