Sass SCSS级父级
我现在有代码:Sass SCSS级父级,sass,less,Sass,Less,我现在有代码: <div class="parent"> <div class="second select"> <div class="third"> <button>OK</button> </div> </div> </div> 是否可以向SCSS中的按钮添加选择类,而不为其创建单独的类型: .parent{ .s
<div class="parent">
<div class="second select">
<div class="third">
<button>OK</button>
</div>
</div>
</div>
是否可以向SCSS中的按钮添加选择类,而不为其创建单独的类型:
.parent{
.second{
.third{
button{
background: red;
}
}
&.select{
.third{
button{
background: green;
}
}
}
}
}
例如,使用mixin查找此项可能有现成的解决方案
.parent{
.second{
.third{
button{
background: red;
@include find('.select',2){ // 2 or '.second' => 2 it's position (second) or search class ".second"
background: green;
}
}
}
}
}
$颜色:红色、绿色、黄色、蓝色;
@对于$i,从1到长度$colors{
按钮:第n个子项{length$colors}n+{$i}{
背景:n$colors,$i;
}
}
.包装纸{
钮扣{
填充:15px 65px;
边界:0;
}
}
文件
好啊
好啊
好啊
好啊
好啊
好啊
好啊
好啊
好啊
好啊
$颜色:红色、绿色、黄色、蓝色;
@对于$i,从1到长度$colors{
按钮:第n个子项{length$colors}n+{$i}{
背景:n$colors,$i;
}
}
.包装纸{
钮扣{
填充:15px 65px;
边界:0;
}
}
文件
好啊
好啊
好啊
好啊
好啊
好啊
好啊
好啊
好啊
好啊
解决了我的问题。同时研究了SCSS: 混合:
@mixin find($find, $new) {
$newselector: ();
@each $selector in & {
$length: length($selector);
@for $i from 1 through $length {
$ff: nth($selector,$i);
@if($ff == $find){
$ff: $ff#{$new};
}
$newselector: append($newselector, $ff);
}
}
@at-root #{$newselector} {
@content;
}
}
SCSS:
解决了我的问题。同时研究了SCSS: 混合:
@mixin find($find, $new) {
$newselector: ();
@each $selector in & {
$length: length($selector);
@for $i from 1 through $length {
$ff: nth($selector,$i);
@if($ff == $find){
$ff: $ff#{$new};
}
$newselector: append($newselector, $ff);
}
}
@at-root #{$newselector} {
@content;
}
}
SCSS:
我不需要使用伪元素,我需要知道父元素是否有某个类,然后为它提供所需的样式。我把这个按钮设置为一个例子。可以使用div blocks您希望在按钮上显示红色,但只有在有任何类时才显示绿色。选择?再次检查代码。我已经更新了或者让我知道u wantI已经有了两个分支的代码,从第二个分支的类开始。我想知道如何缩短代码使用mixin,搜索树。上面的例子你能检查一下是否有用吗请再次检查:我不需要使用伪元素,我需要知道父元素是否有特定的类,然后给它所需的样式。我把这个按钮设置为一个例子。可以使用div blocks您希望在按钮上显示红色,但只有在有任何类时才显示绿色。选择?再次检查代码。我已经更新了或者让我知道u wantI已经有了两个分支的代码,从第二个分支的类开始。我想知道如何缩短代码使用mixin,搜索树。上面的例子你能不能检查一下这是否有帮助请再次检查:这只是我的意见,但你的解决方案很难理解。最好保持代码简单:KISS>DRY。此外,在本例中,存在嵌套选择器问题:。因此,如果删除一些选择器,这可能是一个很好的解决方案:。干杯这只是我的意见,但你的解决方案很难理解。最好保持代码简单:KISS>DRY。此外,在本例中,存在嵌套选择器问题:。因此,如果删除一些选择器,这可能是一个很好的解决方案:。干杯
.parent{
.second{
.third{
button{
background: red;
@include find('.second','.select'){
background: green;
}
}
}
}
}