css - 为什么较早的 CSS 规则会覆盖较晚的规则?

标签 css css-selectors

在我的样式表中,.four-across li 在第 8806 行定义了 width: 174px;。在第 9603 行的规则下方,.no-search-结果 定义了width: auto;。但是,174px 规则覆盖了具有 .no-search-results 的元素。为什么会这样?

CSS in Chrome Developer Tools

最佳答案

您应该阅读有关 CSS 特异性的内容。

.four-across li.no-search-results更具体,因此具有更高的重要性级别。

Specificity is calculated by counting various components of your css and expressing them in a form (a,b,c,d). This will be clearer with an example, but first the components.

  • Element, Pseudo Element: d = 1 – (0,0,0,1)
  • Class, Pseudo class, Attribute: c = 1 – (0,0,1,0)
  • Id: b = 1 – (0,1,0,0)
  • Inline Style: a = 1 – (1,0,0,0)

来自Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade

仅当给定的特异性完全相同时,文档顺序才重要。在您的示例中,第一个选择器是 (0,0,1,1),第二个是 (0,0,1,0),因此无论它们在 CSS 文档中如何排序,第一个会覆盖第二个。

关于css - 为什么较早的 CSS 规则会覆盖较晚的规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18471722/

相关文章:

javascript - CSS 旋转。旋转 div 但保持在完全相同的坐标?

html - 将绝对定位元素宽度设置为它的内容(它只是一个文本)

html - 谷歌浏览器径向渐变闪烁

css - 我怎样才能创建这个特殊的形状?

使用 first-child 及之前的 CSS

jquery - 为什么我的 nth-of-type 选择器不工作?

php - Jquery .hover 不工作

html - 悬停CSS时背景颜色不会改变

css - 如何在 json 中的 css 选择器的 "or"字段中使用 "and"和 "value"运算符

css - 是否有 "reverse general siblings"CSS 选择器?