我应该避免在CSS中使用!important吗?

时间:2022-07-15 08:43:17

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非常有用,例如,如果我为网站上的所有链接定义了常用样式(选择器,比方说,a),但是当我想覆盖某些规则时,我有以下选择:

  • Use more specific (longer) selector
  • 使用更具体(更长)的选择器

  • Use !important

Which way is better and may be there are some guidelines?

哪种方式更好,可能有一些指导方针?

3 个解决方案

#1


15  

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, at which point you often have to either refactor your styles, or use another !important rule and contribute to the problem.

使用!非常重要,非常谨慎 - 它覆盖了所有内容,甚至是内联样式,并且以一种不那么明显的方式混淆了风格规则的“级联”,这使CSS成为其名称。它易于使用,并且往往会倍增,特别是在误用时。你可以很容易地得到一个你想要覆盖的!重要规则的元素,此时你经常需要重构你的样式,或者使用另一个重要的规则并解决问题。

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.

一旦它被传播并且你在任何地方都在使用它,你就会在没有它的情况下恢复你所处的相同情况(难以指定特别足以覆盖其他风格的元素),但你也没有!重要不再导致其他一切也在使用它。

When faced with a situation where !important looks appealing -- or worse, one where it's already in use and spreading -- prefer to refactor your CSS if you can. (Frankly, if you need !important outside of a user style sheet, it's usually because your selectors are already way too specific, and/or you're not taking advantage of the C in CSS.) You'd do better to define your basic styles as close as possible to the html or body elements, and when you want to override, use as little specificity as you can get away with. That way, you have plenty of room to make changes. Usually there's a reason you want to override a style, and those cases can quite often be boiled down to a class name, a particular section of the page (read: a particular parent element), etc.

当面临一种情况:重要的看起来很吸引人 - 或者更糟糕的是,它已经在使用和传播的情况 - 如果可以的话,更愿意重构你的CSS。 (坦率地说,如果你需要!在用户样式表之外很重要,通常是因为你的选择器已经太具体了,和/或你没有利用CSS中的C。)你最好定义你的基本样式尽可能接近html或body元素,当你想要覆盖时,使用尽可能少的特异性。这样,您就有足够的空间进行更改。通常有一个你想要覆盖样式的原因,这些情况通常可以归结为类名,页面的特定部分(读:特定的父元素)等。

(The only real exception that springs to mind is if the styles you're overriding are effectively out of your control. (If you use a framework that has very strong opinions on how your page should look, for example, you might find it annoyingly difficult to override anything. I've worked with applications that actually inserted their own inline styles, where nothing but an !important rule could override them.) If you don't have full access to the code, overriding and refactoring can easily be more trouble than they're worth. You can use !important to claw back some control, as long as you're aware of the consequences.)

(唯一真正的例外是,如果您覆盖的样式实际上超出了您的控制范围。(例如,如果您使用的框架对页面的外观有非常强烈的意见,那么您可能会发现它很烦人很难重写任何东西。我已经使用了实际插入自己的内联样式的应用程序,除了重要的规则之外什么都不能覆盖它们。)如果你没有对代码的完全访问权限,那么覆盖和重构很容易就会更多麻烦,而不是他们的价值。只要你知道后果,你可以使用!important来重新控制一些控制权。)

#2


2  

What i can say is that you should try to avoid it as much as you can because it means that there are redundant code, and give an indication that the CSS and styles are not well structured.

我可以说你应该尽可能地避免它,因为它意味着有冗余的代码,并指出CSS和样式没有很好的结构。

But in some cases its a must to use it and no way to avoid.

但在某些情况下,必须使用它,无法避免。

So just do your best.

所以尽力做到最好。

#3


2  

Avoid it when you can as this is what most advocate but at the same time it is important.

尽可能避免它,因为这是最主张的,但同时它很重要。

Have a look at:

看一下:

The importance of !important in CSS

CSS中重要的!重要性

#1


15  

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, at which point you often have to either refactor your styles, or use another !important rule and contribute to the problem.

使用!非常重要,非常谨慎 - 它覆盖了所有内容,甚至是内联样式,并且以一种不那么明显的方式混淆了风格规则的“级联”,这使CSS成为其名称。它易于使用,并且往往会倍增,特别是在误用时。你可以很容易地得到一个你想要覆盖的!重要规则的元素,此时你经常需要重构你的样式,或者使用另一个重要的规则并解决问题。

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.

一旦它被传播并且你在任何地方都在使用它,你就会在没有它的情况下恢复你所处的相同情况(难以指定特别足以覆盖其他风格的元素),但你也没有!重要不再导致其他一切也在使用它。

When faced with a situation where !important looks appealing -- or worse, one where it's already in use and spreading -- prefer to refactor your CSS if you can. (Frankly, if you need !important outside of a user style sheet, it's usually because your selectors are already way too specific, and/or you're not taking advantage of the C in CSS.) You'd do better to define your basic styles as close as possible to the html or body elements, and when you want to override, use as little specificity as you can get away with. That way, you have plenty of room to make changes. Usually there's a reason you want to override a style, and those cases can quite often be boiled down to a class name, a particular section of the page (read: a particular parent element), etc.

当面临一种情况:重要的看起来很吸引人 - 或者更糟糕的是,它已经在使用和传播的情况 - 如果可以的话,更愿意重构你的CSS。 (坦率地说,如果你需要!在用户样式表之外很重要,通常是因为你的选择器已经太具体了,和/或你没有利用CSS中的C。)你最好定义你的基本样式尽可能接近html或body元素,当你想要覆盖时,使用尽可能少的特异性。这样,您就有足够的空间进行更改。通常有一个你想要覆盖样式的原因,这些情况通常可以归结为类名,页面的特定部分(读:特定的父元素)等。

(The only real exception that springs to mind is if the styles you're overriding are effectively out of your control. (If you use a framework that has very strong opinions on how your page should look, for example, you might find it annoyingly difficult to override anything. I've worked with applications that actually inserted their own inline styles, where nothing but an !important rule could override them.) If you don't have full access to the code, overriding and refactoring can easily be more trouble than they're worth. You can use !important to claw back some control, as long as you're aware of the consequences.)

(唯一真正的例外是,如果您覆盖的样式实际上超出了您的控制范围。(例如,如果您使用的框架对页面的外观有非常强烈的意见,那么您可能会发现它很烦人很难重写任何东西。我已经使用了实际插入自己的内联样式的应用程序,除了重要的规则之外什么都不能覆盖它们。)如果你没有对代码的完全访问权限,那么覆盖和重构很容易就会更多麻烦,而不是他们的价值。只要你知道后果,你可以使用!important来重新控制一些控制权。)

#2


2  

What i can say is that you should try to avoid it as much as you can because it means that there are redundant code, and give an indication that the CSS and styles are not well structured.

我可以说你应该尽可能地避免它,因为它意味着有冗余的代码,并指出CSS和样式没有很好的结构。

But in some cases its a must to use it and no way to avoid.

但在某些情况下,必须使用它,无法避免。

So just do your best.

所以尽力做到最好。

#3


2  

Avoid it when you can as this is what most advocate but at the same time it is important.

尽可能避免它,因为这是最主张的,但同时它很重要。

Have a look at:

看一下:

The importance of !important in CSS

CSS中重要的!重要性