When to use “!important” to save the day (when working with CSS)

前端 未结 7 1865
北海茫月
北海茫月 2020-12-04 02:10
#div p {
    color: red !important;
}
...
#div p {
    color: blue;
}

I understand how !important works, in this case the div will ren

7条回答
  •  一向
    一向 (楼主)
    2020-12-04 02:20

    !important is useful when you're working with someone else's code and can't override an effect simply by using more specific selectors. When I'm asked to come in and add on a page or small set of pages to an existing site and I have to include the existing stylesheets, but can't edit them (either because of lack of access or because the trickle-down effect would break other things), that's where !important comes in handy. Otherwise you should be using selector specificity to override behavior.

提交回复
热议问题