类选择器在CSS中不起作用,但ID可用于添加某些样式 [英] class selector not working in css, but id works for add some styles

查看:329
本文介绍了类选择器在CSS中不起作用,但ID可用于添加某些样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试设置某些样式,但类选择器为。不起作用,但是如果我使用id选择器#,则可以起作用

Trying to set some styles but class selector "." is not working, but if I use id selector "#" it works

此处的代码

<style>
    .activeOption{
        color: #fff;
        background: rgba(255,255,255,0.2);
        text-decoration: none;
    }
</style>
    <li ><a id="" class="activeOption" href="#" onclick="Javascript : console.log('sisas')">Datos del Cliente</a></li>

问题是我想对许多支持者使用这种样式,因此不会使用id选择器

the thing is that I wanna use that style for many comoponents so id selector is not apropiated

这是检查元素时看到的内容

感谢有人能帮助我

推荐答案

该屏幕截图表示,由于特殊性。

What that screenshot is saying is that other styles are overriding this style due to specificity.

解决特异性问题的最佳方法是降低覆盖它的选择器的特异性。不过,这并非总是可能的,因此,万不得已时,您可以通过将类链接到自己来提高特异性。

The best way to solve specificity issues is to decrease the specificity of the selectors overriding it. That isn't always possible though, so as a last resort you can increase your specificity by chaining classes to themselves.

.activeOption.activeOption {
     [styles]
}

您可以链接多次根据需要,链越多,它覆盖的特异性就越强。请注意,如果尝试覆盖ID,则此方法无效,因为它需要255个链。重构ID是我的建议。

You can chain as many times as needed, the more chains the more specificity it overrides. Note that if trying to override an ID this method doesn't work as it requires 255 chains. Refactor the ID is my advice here.

您可以看到如何特异性是在这里计算的。

这篇关于类选择器在CSS中不起作用,但ID可用于添加某些样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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