何时使用“!重要”保存一天(使用CSS时) [英] When to use "!important" to save the day (when working with CSS)

查看:82
本文介绍了何时使用“!重要”保存一天(使用CSS时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#div p {
    color: red !important;
}
...
#div p {
    color: blue;
}

我明白!important 工作,在这种情况下,div将呈现红色,因为现在它有优先级(!important)。但我仍然不能找出一个合适的情况使用它。是否有人知道!important 节省了这一天的任何示例?

I understand how !important works, in this case the div will render red because now it has priority (!important). But I can't still figure out an appropriate situation to use it. Does anybody know any example where !important saves the day?

推荐答案

请考虑这个:

#someElement p {
    color: blue;
}

p.awesome {
    color: red;
}

如何使 awesome 段总是变成红色,甚至在 #someElement 中的那些?如果没有!important ,第一条规则将具有更多的特异性,并胜过第二条规则。

How do you make awesome paragraphs always turn red, even ones inside #someElement? Without !important, the first rule will have more specificity and will win over the second rule.

这篇关于何时使用“!重要”保存一天(使用CSS时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆