作者:hja2045905 | 来源:互联网 | 2023-05-18 04:28
Problem
I mainly use the following methods. (SASS)
我主要使用以下方法。 (SASS)
.person {
&.man {
.head { A }
}
&.woman {
.head { B }
}
.head { C }
}
But I want to use the following method. (SASS)
但我想使用以下方法。 (SASS)
.person {
.head {
C
{
A
}
{
B
}
}
}
compiled result (CSS)
编译结果(CSS)
.person .head { C }
.person.man .head { A }
.person.woman .head { B }
I want to know if there is such a function. Thank you.
我想知道是否有这样的功能。谢谢。
My result
I got the idea from @falsarella's @at-root approach. It seems a bit crude, but this is also possible. (I actually used deeper selectors than the example, so it was hard to solve with at-root and #{$} alone.)
我从@ falsarella的@ at-root方法中得到了这个想法。看起来有点粗糙,但这也是可能的。 (我实际上使用了比示例更深的选择器,所以很难用at-root和#{$}单独解决。)
.person {
$person: &;
.head {
C
@at-root #{$person}.man .head {
A
}
@at-root #{$person}.woman .head {
B
}
}
}
Or it would be more convenient and readable(If the parent selector is not a simple selector.) to use it by naming $parent and overriding the previous $parent.
或者它更方便和可读(如果父选择器不是一个简单的选择器。)通过命名$ parent并覆盖前一个$ parent来使用它。
When I think about it once, the current selector is named $parent, so it is confusing. It might be better to ignore '>', ':after', ... of a parent selector and just name it as $person. (or create naming conventions.)
当我考虑一次时,当前选择器被命名为$ parent,因此它令人困惑。最好忽略父选择器的'>',':after',...并将其命名为$ person。 (或创建命名约定。)
.earth {
$parent: &;
.person {
$parent: &;
.head {
C
@at-root #{$parent}.man .head {
A
}
@at-root #{$parent}.woman .head {
B
}
}
}
}
As a result of further Googling, postcss seems to support parent selectors I want.
由于进一步谷歌搜索,postcss似乎支持我想要的父选择器。
3 个解决方案