-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[css] 第29天 请描述css的权重计算规则 #103
Comments
|
权重值计算
比较规则
参考:
网上查阅所得,如有不对,希望大家指出。 |
这道题去年推特上一个大神发的,几千个人只有一半人对。。。 两个123颜色是啥? <div class="red blue">123</div>
<div class="blue red">123</div> .red {
color: red;
}
.blue {
color: blue
} |
都是蓝色 |
应该是不太对,我初学CSS的时候读过一本《精通CSS高级Web标准解决方案(第2版)》,里面说的是 id 的权重相当于 255 个 class 具体多少个我也没测试,但测试了一下11个 class 的权重依然是小于 id 的。 |
一般的权重规则如下。
其中伪类和属性选择器([type='xxx']) 的权重与 class 相同;伪元素的权重与 tag 相同。 权重从左往右进行比较,只有前一级相等时才会向后比较。当权重相同时,靠后的样式会覆盖靠前的样式(这里的靠后是 CSS 文件中的顺序,不是 class 中的顺序) .title {
color: red;
}
.title {
/* 真正生效的是这个 */
color: green;
} <h1 class="title">2333</h1> 这题与 Day23 说说 CSS 的优先级是如何计算的? 是类似的。 |
!important(内联) > !importan(外联) > 内联 > ID(100) > Class(10) > tag(1) > * |
即使都不会,统计学上也支持一半人答对。 |
一般的权重规则如下。 !important (正无穷) > 内联样式(1000) > #id(100) > .class(10) > tag(1) > *(0) 其中伪类和属性选择器([type='xxx']) 的权重与 class 相同;伪元素的权重与 tag 相同。 权重从左往右进行比较,只有前一级相等时才会向后比较。当权重相同时,靠后的样式会覆盖靠前的样式(这里的靠后是 CSS 文件中的顺序,不是 class 中的顺序) .title { .title { 2333这题与 Day23 说说 CSS 的优先级是如何计算的? 是类似的。 |
再来一个
|
标签选择器: 1 |
权值的计算公式为:(第一等级选择器个数,第二选择器个数,第三选择器个数,第四选择器个数) |
第29天 请描述css的权重计算规则
The text was updated successfully, but these errors were encountered: