css - SCSS 嵌套祖先
问题描述
请在我的 SCSS 代码中查看我的评论。
这种更深的嵌套在 SASS 中是可能的吗?我确信我在几年前找到了一个令我满意的解决方案,但我已经忘记了。有人可以帮助阐明我如何进行这种嵌套。
SCSS
.class {
color: red;
&__nested {
color: green;
}
&__link {
color: yellow;
&:hover {
border: 1px solid yellow;
^^&__nested { // Is something like this one possible?
color: pink;
}
}
}
}
编译
.class {
color: red;
}
.class__nested {
color: green;
}
.class__link {
color: yellow;
}
.class__link:hover {
border: 1px solid yellow;
}
.class__link:hover .class__nested { // Is something like this one possible?
color: pink;
}
我不想这样定义一个变量。
.class {
$parent: .class;
&__link {
color: yellow;
&:hover {
border: 1px solid yellow;
#{$parent}__nested {
color: pink;
}
}
}
}
解决方案
你说没有像“.class”这样的变量,但如果你只是想避免强输入它并且没有什么能阻止你实际使用变量,你可以这样做&
:
.class {
$self: &;
color: red;
&__link {
color: yellow;
}
&__nested {
color: green;
&:hover + #{ $self }__link {
color: pink;
}
}
}
工作示例的 JSFiddle: https ://jsfiddle.net/gy3zLqsm/
推荐阅读
- sql - TSQL - 在选择查询中返回布尔结果
- django - Django - Apache2 内部服务器错误
- odoo - 在搜索视图中按相关 one2many 字段过滤
- azure-iot-sdk - 基于 websockets 的 IoT Edge Mqtt
- search - 有没有一种通用的方法来计算产品型号或规格之间的相似度?
- ios - Swift UIRefreshControl 在 iPhone 8 上不起作用
- ios - 在 Swift 中将数据传递给 ViewModel
- jquery - 隐藏 div 出现时让整个表单保持焦点
- vb.net - 没有 FormBorderStyle 的表单,最大化时变为全屏
- php - 超级表单 Wordpress 插件可以在不同的操作系统上使用 php 7.0 版吗?