[英]How can I code a CSS select for one class being present and even one of a few others not?
I have this Less code: 我有这个更少的代码:
btn {
&.answered {
color: #bbb;
}
}
How could I modify this so the color is set to #bbb only if there is a .answered
class and if there is not a btn-success
or btn-danger
or btn-warning
class? 我如何修改此设置,以便仅在存在.answered
类并且没有btn-success
或btn-danger
或btn-warning
类的情况下,将颜色设置为#bbb?
I do realize that I could do this by adding the btn-success
, btn-danger
and btn-warning
classes after this code but I really would like to find a selector that would do this for me so it does not depend on the position of the other classes. 我确实意识到我可以通过在此代码之后添加btn-success
, btn-danger
和btn-warning
类来做到这一点,但是我真的很想找到一个选择器来为我做这件事,因此它不依赖于其他班。
You should chain the :not
pseudo-selector like in the below snippet. 您应该像下面的代码片段那样链接:not
伪选择器。 This will select the button when it doesn't have even one of the other 3 classes. 当它没有其他三个类之一时,将选择按钮。 In other words, this is equivalent to saying exclude all elements where x OR y OR z is present. 换句话说,这等同于说排除存在x OR y OR z的所有元素。
Less Code: 更少的代码:
button{
.answered{
&:not(.btn-success):not(.btn-warning):not(.btn-danger) {
color: red;
}
}
}
Demo based on compiled output: 基于编译输出的演示:
button.answered:not(.btn-success):not(.btn-warning):not(.btn-danger) { color: red; }
<button class='answered'>Click Me</button> <button class='answered btn-success'>Click Me</button> <button class='answered btn-danger'>Click Me</button> <button class='answered btn-warning'>Click Me</button> <button class='answered btn-warning btn-success'>Click Me</button> <button class='answered btn-danger btn-success'>Click Me</button> <button class='answered btn-warning btn-danger'>Click Me</button> <button class='answered btn-warning btn-danger btn-success'>Click Me</button>
A comma separated list of :not
selectors (grouping) would exclude only the element which has all 3 other classes. :not
选择器(分组)的逗号分隔列表将仅排除具有所有其他3个类的元素。 It will not work (exclude) when even one of the other class is present on the element. 当元素上甚至存在另一个类时,它也不起作用(排除)。 In other words, it is equivalent to saying exclude all elements where x AND y AND z are present. 换句话说,这等同于说排除存在x AND y AND z的所有元素。
You can see in the below snippet how it applies red color to all elements which don't have all the other classes together. 您可以在下面的代码段中看到如何将红色应用于没有其他所有类的所有元素。 It behaves in this way because when the element has .btn-success
, it still doesn't have one of the other two. 它的行为方式是,当元素具有.btn-success
,它仍然不具有其他两个之一。
button.answered:not(.btn-success), button.answered:not(.btn-danger), button.answered:not(.btn-warning) { color: red; }
<button class='answered'>Click Me</button> <button class='answered btn-success'>Click Me</button> <button class='answered btn-danger'>Click Me</button> <button class='answered btn-warning'>Click Me</button> <button class='answered btn-warning btn-success'>Click Me</button> <button class='answered btn-danger btn-success'>Click Me</button> <button class='answered btn-warning btn-danger'>Click Me</button> <button class='answered btn-warning btn-danger btn-success'>Click Me</button>
btn {
&.answered:not(.btn-success):not(.btn-danger):not(.btn-warning) {
color: #bbb;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.