简体   繁体   中英

How to combine two SCSS selectors?

I want to give a different color to the element if either of the two conditions exists. Here is the code:

  .btn-link{
    color: $color-black;
    margin: 0;

  &:hover{
    color: $color-light-blue;
  }
  &:not(.collapsed){
    color: $color-light-blue;
  }
}

Everything is good, but it will be better if you can combine the two selectors

I already tried:

 &:hover&:not(.collapsed){
        color: $color-light-blue;
      }

But only the hover is identified

Same way as you do in CSS:

&:hover, &:not(.collapsed) {
    color: $color-light-blue;
}

This sets the color only if the element is in a hover state or doesn't have the class collapsed .

You can put a comma between the two combining selectors, like this: &:hover, &:not(.collapsed) {

Full example:

HTML:

<span class="btn-link">one class</span>
<span class="btn-link collapsed">two class</span>

CSS:

$color-black: black;
$color-light-blue: lightblue;

.btn-link {
  color: $color-black;
  margin: 0;
  &:hover, &:not(.collapsed) {
    color: $color-light-blue;
  }
}

JSfiddle .

Sorry, no StackSnippet. We still can't handle SCSS here!

You can try this simple change your code

 /*SCSS*/ $color-light-blue : red; .btn-link { &:hover:not(.collapsed) { color: $color-light-blue; } } /*compiled CSS*/ .btn-link:hover:not(.collapsed) { color: red; } 
 <span class="btn-link">one class</span> <span class="btn-link collapsed">two class</span> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM