我如何给一个CSS类的优先级高于ID? [英] How do I give a CSS class priority over an id?

查看:686
本文介绍了我如何给一个CSS类的优先级高于ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的元素:

#idname{
  border: 2px solid black;
}

.classname{
  border: 2px solid gray;
}

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

想给它的CSS类而不是它的CSS id一个更大的优先级。可能吗? p>

I want to give a bigger priority to its CSS class instead of its CSS id. Is it possible? (In other word I want to set gray-color as its border)

推荐答案

不要使用!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;
}

这篇关于我如何给一个CSS类的优先级高于ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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