CSS选择器的优先级是什么 [英] What are the priorities among CSS selectors

查看:140
本文介绍了CSS选择器的优先级是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CSS问题:如果两个不同的选择器适用于一个元素,谁赢了?

CSS Question: If two different selectors apply to an element, who wins?

我知道这不应该发生,但我想调整一个遗留的应用程序,

I know this shouldn't happen, but I want to tweak a legacy application, and the CSS is getting in the middle.

推荐答案

gory细节在规范实际上是可读的。总结:

The gory details in the spec are actually reasonably readable. In summary:


  1. !important 规则和内联<$ c $

  1. !important rules and inline style rules win most.

否则,通常更具体的胜利。 是比更具体的选择器.classname 是比 tagname

Otherwise, normally the more specific wins. #id is a more specific selector than .classname is a more specific selector than tagname.

如果规则具有同等的特定性,则最后一次获胜的规则。

Where rules are equally specific, the one declared last wins.

没有特别的理由为什么这个不应该发生。指定一个宽刷规则是正常的,然后添加一个更具体的规则来定位一个案例;对单个元素的多个相同属性规则并不常见或不合需要。

There is no particular reason why this ‘shouldn't happen’. It's normal to specify a broad-brush rule and then add a more specific rule to target one case; multiple same-property rules on a single element are not unusual or undesirable.

这篇关于CSS选择器的优先级是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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