问题描述
Bootstrap响应实用程序都使用!important
,这对我来说没有太大意义。而且它不会像 col-xx-xx
那样进行继承。我想知道为什么他们为这些课程添加!important
。有人提到这是出于特异性。这意味着什么?另外,显示和隐藏类似乎也使用!important
。为什么?
Bootstrap responsive utilities are all using !important
, which doesn't make much sense to me. And it doesn't do inheritance like col-xx-xx
does. I'd like to know why they added !important
for these classes. Someone mentioned it was for specificity. What does that mean? In addition, show and hide classes seems to use !important
as well. Why?
推荐答案
虽然使用!important
却不是一个好主意,因为您已经注意到了自己,因为再次使用!important
会很麻烦,我相信TB对于响应式实用程序来说是有充分理由的。
While using !important
is rarely a good idea as you noticed yourself, since it is a pain to overwrite without using !important
yet again, I believe TB has a good reason for doing this for the responsive utilities.
如果您声明要例如一个在按钮上不可见的按钮在小型显示器上,通过将 hidden-sm
类设置为该按钮,您永远不会希望它在小型显示器上可见。如果您不想在该实用工具类上使用重要代码,例如想要通过添加 .btn-block
类在所有其他显示尺寸上使用块按钮,则由于,您的按钮将在小显示器上再次显示。 btn-block
将 display
属性设置回 block
。或者至少可以根据事物的顺序,因为 .hidden-sm
和 .btn-block
具有
If you state that you want e.g. a button to be not visible on e.g. small displays, by setting the hidden-sm
class to that button, you never want it to be visible on small displays. If you would not use important on that utility class, and you would e.g. want a block button on all the other display sizes by adding .btn-block
class, your button would become visible again on small displays, since the .btn-block
sets the display
property back to block
. Or at least it could, depending on the order of things, since .hidden-sm
and .btn-block
have the same specificity when it comes to the cascading rules of css.
如果没有!重要$ c $,您将无能为力c>声明。因此,这是边缘情况之一,您必须使用它来保证正确的行为。
Nothing much you can do about that without the !important
statement. So this is one of the edge cases where you would have to use it to guarantee the correct behavior.
但是您对此质疑是正确的,!important
只能作为不得已的手段!
But you are right in questioning this, !important
should only be used as a last resort!
这篇关于为什么引导程序将'!important'用于响应类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!