且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何给一个CSS类的优先级高于ID?

更新时间:2023-01-08 16:16:10

不要使用!important ,因为它是最糟糕的解决方案,如果你想要切换样式,然后以这种方式做。

Do not use !important because it is the worst solution, if you want to switch the styling then do it that way.

#idname{
  border: 2px solid black;
}

#idname.classname {
  border: 2px solid gray;
}

<div id = "idname" class="classname">it is a test</div>

如果你还要使用类 classname 来定位其他元素,而不仅仅是 #idname / p>

If you want to target also other elements with the class classname and not only the #idname then use:

#idname.classname, .classname {
  border: 2px solid gray;
}