问题描述
有时!important
是有用的,例如如果我已经为网站上的所有链接定义了公共样式(选择器,比如说 code>),但是当我想覆盖一些规则时,我有以下选择:
Sometimes !important
is useful, for example if I have defined common style for all links on the site (selector, say, a
), but when I want to override some rules I have the following choices:
- 使用更具体
- 使用
!important
- Use more specific (longer) selector
- Use
!important
推荐答案
使用!important
非常,非常谨慎 - 它覆盖了所有的东西,甚至内联样式,并在一个不太明显的方式与级联的风格规则,给CSS CSS的名称混乱。它很容易使用不好,并且往往繁殖,特别是当误用。你可以很容易地结束一个具有!important
规则的元素,而不重构你的样式,通常唯一的方法是使用另一个!important
规则。
Use !important
very, VERY sparingly -- it overrides just about everything, even inline styles, and messes in a less-than-obvious way with the "cascade" of style rules that gives CSS its name. It's easy to use badly, and tends to multiply, particularly when misused. You can easily end up with a element with !important
rules that you want to override, and without refactoring your styles, often the only way to override is with another !important
rule.
一旦它传播并且你在任何地方都使用它,你在同样的情况下没有它(难以指定足够的元素以覆盖其他样式),但你也没有!important
任何更多,因为一切都在使用它。
And once it's spread and you're using it everywhere, you're back up in the same situation you'd be in without it (difficulty in specifying elements specifically enough to override other styles), but you also don't have !important
anymore cause everything else is using it too.
这篇关于我应该避免使用!重要在CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!