SCSS修改父选择器
有没有办法将类添加到带有.scs的嵌套选择器的第一个元素中? 想象以下示例:
body p {
color: black;
}
body.other-mode p {
color: red;
}
可以这样表达它(或以类似的方式表达)
body p {
& {
color: black;
}
.other-mode& {
color: red;
}
}
我知道 问题的示例。
body {
& p {
color: black;
}
&.other-mode p {
color: red;
}
}
我还尝试了使用一些SCSS选择器,但它们的工作状态不如预期,在下面的情况下最终为身体P身体。代码>
body p {
& {
color: black;
}
#{selector-replace(&, "body", "body.other-mode")} {
color: red;
}
}
Is there a way to add a class to the first element of a nested selector with .scss?
Imagine the example below:
body p {
color: black;
}
body.other-mode p {
color: red;
}
Would it be possible to express it like this (or in a similar way, since this won't compile)
body p {
& {
color: black;
}
.other-mode& {
color: red;
}
}
I'm aware this can be written like this, but that's just a different way of "solving" an example of the issue.
body {
& p {
color: black;
}
&.other-mode p {
color: red;
}
}
I also tried using some scss selectors but they don't work quite as expected, in the case below the selectors ends up being body p body.other-mode p
instead of body p
body p {
& {
color: black;
}
#{selector-replace(&, "body", "body.other-mode")} {
color: red;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使您的最后解决方案起作用,您可以使用@at-at-root 。
编译到
但就个人而言,我发现您的原始解决方案是最可读的。
body {&。hother-mode p {color:red;}}
我发现Split
Body
和P
在SCSS中更方便。To make your last solution work, you can use @at-root.
compiles to
But personally, I find your original solution the most readable.
body { &.other-mode p {color: red;} }
I find the split
body
andp
more convenient in SCSS.