【问题标题】:css :not() selector not working with universal selector (*) [duplicate]css:not()选择器不适用于通用选择器(*)[重复]
【发布时间】:2020-06-17 03:23:32
【问题描述】:

看起来 css 选择器 :not() 不适用于 * 选择器。

有什么办法吗? 还是我做错了什么?

*:not(.nope){
  color: green;
}
<div>hai</div>
<div>I</div>
<div class="nope">am</div>
<div>Jhon</div>

我仍然将'am' 设为绿色。

提前致谢!

【问题讨论】:

标签: css css-selectors pseudo-class


【解决方案1】:

通用选择器 (*) 不是问题。这是color 属性的继承。

当你说...

*:note(.nope)

这很好,但是您忘记了* 也将颜色应用于bodyhtml 元素。

所以.nope 从其父级获得绿色。

如果您使用未继承的属性(如border),则不会出现此问题。

*:not(.nope){
  border: 1px solid red;
}

* {
  margin: 5px;
}
<div>hai</div>
<div>I</div>
<div class="nope">am</div>
<div>Jhon</div>

注意.nope 没有边框。

要使颜色随心所欲,请更具体。

div:not(.nope) {
  color: green;
}
<div>hai</div>
<div>I</div>
<div class="nope">am</div>
<div>Jhon</div>

【讨论】:

  • 哦,好的,谢谢。我发现我也可以做 *{ color: green; } .nope{ 颜色:蓝色; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 2019-07-24
  • 2014-09-24
  • 2016-11-12
相关资源
最近更新 更多