CSS 内有多个类 :not()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24266518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 02:35:18  来源:igfitidea点击:

Multiple classes inside :not()

csscss-selectors

提问by asfallows

I'm trying to use the :not()property to exclude a pair of classes from a rule, e.g.:

我正在尝试使用该:not()属性从规则中排除一对类,例如:

*:not(.class1, class2) { display: none; }

However, it looks like the not()property doesn't support comma separated classes, as show in this fiddle.

但是,该not()属性似乎不支持逗号分隔的类,如此 fiddle 所示

HTML:

HTML:

<div class='one'>
  foo
</div>
<div class='two'>
  foo
</div>
<div class='three'>
  foo
</div>
<div class='four'>
  foo
</div>

CSS:

CSS:

div {
  background-color: #CBA;
}

div:not(.one) {
  background-color: #ABC;
}

div:not(.one, .three) {
  color: #F00;
}

The first and second rules get applied, but the third doesn't.

第一个和第二个规则得到应用,但第三个没有。

I can't do *:not(.class1), *:not(.class2)because any element which has class2will be selected by *:not(.class1)and vice versa.

我不能这样做,*:not(.class1), *:not(.class2)因为任何元素都class2将被选择,*:not(.class1)反之亦然。

I don't want to do

我不想做

* { display: none;}
.class1, .class2 { display: inline; }

because not all .class1and .class2elements have the same original display property, and I want them to retain it.

因为并非所有.class1.class2元素都具有相同的原始显示属性,我希望它们保留它。

How can I exclude multiple classes from a rule, either with the not()property or otherwise?

如何使用not()属性或其他方式从规则中排除多个类?

回答by Beterraba

You can use:

您可以使用:

div:not(.one):not(.three) {
    color: #F00;
}

Fiddle

小提琴