【发布时间】:2020-08-19 20:06:23
【问题描述】:
我有这个内容的 scss 定义:
.dialog {
...
.dialog-content {
...
.dialog-content-header {
background-color: red;
}
}
}
这将导致css:
...
.dialog .dialog-content .dialog-content-header {
background-color: red;
}
我需要找到一种方法来达到这个目的:
...
.dialog.dialog-with-train .dialog-content .dialog-content-header {
background-color: blue;
}
额外的 .dialog-with-train 选择器应该以某种方式从 .dialog-content-header 上下文中添加。 有可能是这样的吗?
.dialog {
...
.dialog-content {
...
.dialog-content-header {
background-color: red;
@at-root getFirstItem(&).dialog-with-train getItems(&, 2, ?length?) {
background-color: blue;
}
}
}
}
在我的情况下,从 .dialog 到 .dialog-content-header 的路径非常复杂,我想以某种干净的方式进行更改,而无需修改和重复现有代码。
有什么建议吗?
EDIT1:看看https://sass-lang.com/documentation/modules/selector,有什么方法可以将选择器路径解析+组合成所需组合吗?
EDIT2:我通过使用@use "sass:string"; 对选择器进行字符串减法找到了一些解决方案。请参阅我接受的答案。
如果有更好或更清洁的解决方案我想知道。
【问题讨论】:
-
这能回答你的问题吗? SCSS target class before :hover
-
其实不是。结构很复杂,我提到的例子不适用于我的用例