CSS权重

作者: 隐号骑士 | 来源:发表于2021-02-04 23:54 被阅读0次

权重计算

w3c 官网介绍

A selector's specificity is calculated as follows:

  • count 1 if the declaration is from is a 'style' attribute rather than a rule with a selector, 0 otherwise (= a) (In HTML, values of an element's "style" attribute are style sheet rules. These rules have no selectors, so a=1, b=0, c=0, and d=0.)
  • count the number of ID attributes in the selector (= b)
  • count the number of other attributes and pseudo-classes in the selector (= c)
  • count the number of element names and pseudo-elements in the selector (= d)
  • a: 内嵌样式
  • b: id选择器数量
  • c: 其它属性选择器(包括class)和伪类选择器的数量
  • d: 标签选择器和伪元素选择器的数量

具体计算时遵循a>b>c>d
理论上上一级的比较结果会完全覆盖下一级的
实际上由于内存溢出,在下一级达到256或更高(根据不同的浏览器版本)的计数时会向前“进位”,影响上一级的比较结果

后来居上

当以上计算结果相同时,会使用较为靠后的样式

!important

不属于规范权重计算范围,会覆盖其它任何权重
官方不建议使用

Using !important, however, is bad practice and should be avoided because it makes debugging more difficult by breaking the natural cascading in your stylesheets.

相关文章

  • 归零——CSS-第四天

    CSS学习 CSS权重 选择器 权重...

  • CSS权重

    原文 博客原文 大纲 1、CSS层叠2、CSS权重3、权重等级4、权重的规则5、!important 1、CSS层...

  • 前端面试题整理

    css相关 1、css定义的权重: 标签的权重为1,class的权重为10,id的权重为100如果权重相同,则最后...

  • CSS权重

    CSS权重:1、权重决定了你css规则怎样被浏览器解析直到生效。“css权重关系到你的css规则是怎样显示的”。2...

  • 前端方法

    css 权重 !important具有最高权重 如何和何时使用CSS的!important https://www...

  • css选择器权重

    css选择器权重示例: .html .css

  • CSS权重

    权重决定了你css规则怎样被浏览器解析直到生效。“css权重关系到你的css规则是怎样显示的 权重记忆口诀。从0开...

  • H5和CSS3

    1、CSS权重 CSS权重指的是样式的优先级,当有多个样式作用于元素的时候,权重值大的样式起作用。权重一样的时候,...

  • 2020-01-02

    知识点: 1.H5的新特性 css3新特性 2.css的权重,css模块化 权重: 元素的权重0.类选择器10.i...

  • css权重

    CSS权重 CSS权重指的是样式的优先级,有两条或多条样式作用于一个元素,权重高的那条样式对元素起作用,权重相同的...

网友评论

      本文标题:CSS权重

      本文链接:https://www.haomeiwen.com/subject/hmqmtltx.html