简体   繁体   English

如何为存在的一个类编写CSS选择代码,而为其他几个不编写代码呢?

[英]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-successbtn-dangerbtn-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-successbtn-dangerbtn-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.

相关问题 我可以让我的CSS依赖于不存在的类吗? - Can I make my CSS depend on a class not being present? 如何将 class 的一个属性更改为唯一,但使其他属性在 CSS 中相同 - How to change one property of a class to be unique but have others be the same in CSS 如何使用CSS和JS编写的多选代码仅选择一个选项? 目前,我可以同时选择 - How do I make my multiple choice code written in CSS & JS to only select one option? Currently I can select both 我如何在 css 中使用 * 来选择所有但只有一个 - How can i use * in css to select all but only one 如何使一个<a>具有自己类的项目与其他项目具有不同的颜色?</a> - How can I make one <a> item with its own class a different colour from others? 里面的一个元素<div>使用 flex 时比其他的要宽得多。 我怎样才能使分离均匀? - One element inside <div> is much wider than the others while using flex. How can I make the separation even? 当我直接使用它们而不是 css 上的“奇数”和“偶数”时,如何 select 不止一个数字? - How to select more than one number when I use them directly instead of “odd” and “even” on css? 我怎样才能在css中将select中的class中的class中的class? - How can I select a class in a class in css? 如何在我的代码中为几行加载自定义 CSS? - How can I load a custom CSS for a few lines in my code? 如何显示一个 div 并隐藏所有其他 div - how can i show one div and hide all the others
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM