Skip to content
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

Open
haizhilin2013 opened this issue May 14, 2019 · 12 comments
Open

[css] 第29天 请描述css的权重计算规则 #103

haizhilin2013 opened this issue May 14, 2019 · 12 comments
Labels
css css

Comments

@haizhilin2013
Copy link
Collaborator

第29天 请描述css的权重计算规则

@haizhilin2013 haizhilin2013 added the css css label May 14, 2019
@yxkhaha
Copy link

yxkhaha commented May 15, 2019

  • !important>id>class>标签>子代、相邻选择器>通配符选择器

@AnsonZnl
Copy link
Contributor

AnsonZnl commented May 15, 2019

权重值计算

选择器 案例 权重值
!important !important Infinity
内联样式 style=".." 1000
ID #id 100
class .class 10
属性 [type='text'] 10
伪类 :hover 10
标签 p 1
伪元素 ::first-line 1
相邻选择器、子代选择器、通配符 * > + 0

比较规则

  • 1000>100。也就是说从左往右逐个等级比较,前一等级相等才往后比。
  • 在权重相同的情况下,后面的样式会覆盖掉前面的样式。
  • 继承属性没有权重值
  • 通配符、子选择器、相邻选择器等的。虽然权值为0,但是也比继承的样式优先。
  • ie6以上才支持important,并且尽量少用它。

参考:

网上查阅所得,如有不对,希望大家指出。

@tzjoke
Copy link

tzjoke commented May 28, 2019

这道题去年推特上一个大神发的,几千个人只有一半人对。。。

两个123颜色是啥?

<div class="red blue">123</div>
<div class="blue red">123</div>
.red {
  color: red;
}

.blue {
  color: blue
}

@Vi-jay
Copy link

Vi-jay commented Jul 29, 2019

这道题去年推特上一个大神发的,几千个人只有一半人对。。。

两个123颜色是啥?

<div class="red blue">123</div>
<div class="blue red">123</div>
.red {
  color: red;
}

.blue {
  color: blue
}

都是蓝色
障眼法
css计算规则和class先后顺序无关

@jiamianmao
Copy link

权重值计算

选择器 案例 权重值
!important !important Infinity
内联样式 style=".." 1000
ID #id 100
class .class 10
属性 [type='text'] 10
伪类 :hover 10
标签 p 1
伪元素 ::first-line 1
相邻选择器、子代选择器、通配符 * > + 0

比较规则

  • 1000>100。也就是说从左往右逐个等级比较,前一等级相等才往后比。
  • 在权重相同的情况下,后面的样式会覆盖掉前面的样式。
  • 继承属性没有权重值
  • 通配符、子选择器、相邻选择器等的。虽然权值为0,但是也比继承的样式优先。
  • ie6以上才支持important,并且尽量少用它。

参考:

网上查阅所得,如有不对,希望大家指出。

应该是不太对,我初学CSS的时候读过一本《精通CSS高级Web标准解决方案(第2版)》,里面说的是 id 的权重相当于 255 个 class

具体多少个我也没测试,但测试了一下11个 class 的权重依然是小于 id 的。

@Konata9
Copy link

Konata9 commented Aug 13, 2019

一般的权重规则如下。

!important (正无穷) > 内联样式(1000) > #id(100) > .class(10) > tag(1) > *(0)

其中伪类和属性选择器([type='xxx']) 的权重与 class 相同;伪元素的权重与 tag 相同。

权重从左往右进行比较,只有前一级相等时才会向后比较。当权重相同时,靠后的样式会覆盖靠前的样式(这里的靠后是 CSS 文件中的顺序,不是 class 中的顺序)

.title {
  color: red;
}

.title {
  /* 真正生效的是这个 */
  color: green;
}
<h1 class="title">2333</h1>

这题与 Day23 说说 CSS 的优先级是如何计算的? 是类似的。

@blueRoach
Copy link

!important(内联) > !importan(外联) > 内联 > ID(100) > Class(10) > tag(1) > *

@ferrinweb
Copy link

这道题去年推特上一个大神发的,几千个人只有一半人对。。。

两个123颜色是啥?

<div class="red blue">123</div>
<div class="blue red">123</div>
.red {
  color: red;
}

.blue {
  color: blue
}

即使都不会,统计学上也支持一半人答对。

@MrZ2019
Copy link

MrZ2019 commented Sep 29, 2020

一般的权重规则如下。

!important (正无穷) > 内联样式(1000) > #id(100) > .class(10) > tag(1) > *(0)

其中伪类和属性选择器([type='xxx']) 的权重与 class 相同;伪元素的权重与 tag 相同。

权重从左往右进行比较,只有前一级相等时才会向后比较。当权重相同时,靠后的样式会覆盖靠前的样式(这里的靠后是 CSS 文件中的顺序,不是 class 中的顺序)

.title {
color: red;
}

.title {
/* 真正生效的是这个 */
color: green;
}

2333

这题与 Day23 说说 CSS 的优先级是如何计算的? 是类似的。

@dealdot
Copy link

dealdot commented Apr 27, 2021

再来一个

.red.blue {
   color: purple;
}
.red {
 color: red;
}
.blue {
  color: blue;
}

<div class="red blue">123</div>
<div class="blue red">123</div>
<div class="blue">123</div>
<div class="red">123</div>

@zxcdsaqwe123
Copy link

标签选择器: 1
类选择器:10
ID选择器:100
!important:1000

@Iambecauseyouare
Copy link

权值的计算公式为:(第一等级选择器个数,第二选择器个数,第三选择器个数,第四选择器个数)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css css
Projects
None yet
Development

No branches or pull requests