css - 在SASS中展开之前的路径

标签 css sass

我现在对 Sass 中的路径/选择器有一个小问题。 我得到了这样的东西:

&.foo {
  some-stuff .class,
  some-stuff ul,
  other-stuff .all {
    color: red;
  }
}

现在,当第一个路径是 &.foo.bar 时,我想将颜色设为蓝色:

&.foo {
  some-stuff .class,
  some-stuff ul,
  other-stuff .all {
    color: red;
    // This obviously doesn't work, it's just here to show
    // what I want to do easier
    &.bar {
      color: blue;
    }
  }
}

// Compile to

.foo some-stuff .class,
.foo some-stuff ul,
.foo other-stuff .all {
  color: red;
}

.foo.bar some-stuff .class,
.foo.bar some-stuff ul,
.foo.bar other-stuff .all {
  color: blue;
}

有什么办法可以实现这一点吗?

我尝试使用变量来存储当前选择器 selector-parse Function但结果并没有像我想要的那样。提前致谢

最佳答案

您可以使用selector-replace函数而不是 selector-parse函数将完整父选择器中的 .foo 替换为 .foo.bar

div{
  &.foo {
    some-stuff .class,
    some-stuff ul,
    other-stuff .all {
      color: red;
      @at-root #{selector-replace(&, '.foo', '.foo.bar')} {
        color: blue;
      }
    }
  }
}

编译后,会生成以下 CSS:

div.foo some-stuff .class,
div.foo some-stuff ul,
div.foo other-stuff .all {
  color: red;
}
div.foo.bar some-stuff .class, 
div.foo.bar some-stuff ul, 
div.foo.bar other-stuff .all {
  color: blue;
}

关于css - 在SASS中展开之前的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40719982/

相关文章:

html - 在CSS中获取浅色透明文本字段

html - 防弹 html 电子邮件按钮

html - 页脚没有延伸到网站页面的底部

html - 定义列表光盘未显示

css - 如何使用自动供应商前缀将 SCSS 转换为 CSS?

css - 将 "Long Shadow"SASS mixin 转换为 Less

angular - 如何将样式应用于特定的 Ionic 组件而不覆盖其他组件

css - 如何在 Sass 3.4.22 版本中使用 & 选择器?

javascript - 如何使用 jquery 在点击时切换框阴影

javascript - 如何从 Javascript 调用访问伪元素样式?