Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 当两个嵌套类都有时要应用的SASS(.scss)规则_Html_Css_Sass - Fatal编程技术网

Html 当两个嵌套类都有时要应用的SASS(.scss)规则

Html 当两个嵌套类都有时要应用的SASS(.scss)规则,html,css,sass,Html,Css,Sass,我正在为我当前的项目使用SASS(.scss) HTML <div class="colours colours--blue"> <!-- This should be blue --> </div> <div class="colours colours--yellow"> <!-- This should be yellow --> </div> <div class="colours colours-

我正在为我当前的项目使用SASS(.scss)

HTML

<div class="colours colours--blue">
  <!-- This should be blue -->
</div>

<div class="colours colours--yellow">
  <!-- This should be yellow -->
</div>

<div class="colours colours--blue colours--yellow">
  <!-- This should be green -->
</div>

SCSS

.colours {
   width: 50px;
   height: 50px;

   &--blue {
      background-color: blue;
   }

   &--yellow {
      background-color: yellow;
   }

   &--blue (AND) &--yellow { /* <<< HOW TO CREATE THIS SELECTOR? */
      background-color: green;
   }
}
.colors{
宽度:50px;
高度:50px;
&--蓝色的{
背景颜色:蓝色;
}
&--黄色的{
背景颜色:黄色;
}

蓝-(和)和-黄{/*

可以考虑使用如下变量:

$c:'.colours';

#{$c} {
   width: 50px;
   height: 50px;

   &--blue {
      background-color: blue;
   }

   &--yellow {
      background-color: yellow;
   }

   &--blue {
      &#{$c}--yellow { 
        background-color: green;
      }
   }
}

可以考虑使用如下变量:

$c:'.colours';

#{$c} {
   width: 50px;
   height: 50px;

   &--blue {
      background-color: blue;
   }

   &--yellow {
      background-color: yellow;
   }

   &--blue {
      &#{$c}--yellow { 
        background-color: green;
      }
   }
}

您可以使用选择器功能:

@at-root #{selector-unify(selector-append(&, "--blue"), selector-append(&, "--yellow"))} {
   background-color: green;
}
可能最好用另一种混合液将其包裹起来,以便更容易使用:

@mixin both-colours($a, $b) {
   @at-root #{selector-unify(selector-append(&, "--#{$a}"), selector-append(&, "--#{$b}"))} {
      @content;
   }
}
用作

@include both-colours(blue, yellow) {
   background-color: green;
}

您可以使用选择器功能:

@at-root #{selector-unify(selector-append(&, "--blue"), selector-append(&, "--yellow"))} {
   background-color: green;
}
可能最好用另一种混合液将其包裹起来,以便更容易使用:

@mixin both-colours($a, $b) {
   @at-root #{selector-unify(selector-append(&, "--#{$a}"), selector-append(&, "--#{$b}"))} {
      @content;
   }
}
用作

@include both-colours(blue, yellow) {
   background-color: green;
}

请记住,
&
的作用几乎就像Sass中的占位符。换句话说,只需使用插值util
{}
就可以更容易地避免字符串串联

所以答案很简单:

.colours {
   width: 50px;
   height: 50px;

   &--blue {
      background-color: blue;
   }

   &--yellow {
      background-color: yellow;
   }

   &--blue#{&}--yellow {
      background-color: green;
   }
}

请记住,
&
几乎像Sass中的占位符。也就是说,使用插值util
{}
可以更容易地避免字符串串联

所以答案很简单:

.colours {
   width: 50px;
   height: 50px;

   &--blue {
      background-color: blue;
   }

   &--yellow {
      background-color: yellow;
   }

   &--blue#{&}--yellow {
      background-color: green;
   }
}

如果这是唯一的方法,我会在DOM中引入另一个类来简化样式。这太复杂了,无法维护。如果这是唯一的方法,我会在DOM中引入另一个类来简化样式。这太复杂了,无法维护。