html - 我如何使用 Sass 的“&”(与号)来检查元素是否是某个标签(例如:div)
问题描述
我正在使用 Sass 对表格进行样式设置,并且我想根据该元素的标签(<td />
或<th />
)以不同的方式自定义具有某个类的元素的样式。
我想过使用
.foo {
/* td styles here */
& th { /* th styles here */ }
}
但这不起作用。(它选择具有 th 标签的“.foo”的孩子)
有什么方法可以使用 Sass 的“&”(与号)根据标签对具有相同类的元素进行样式设置?
解决方案
利用:is()
.foo {
&:is(td) { /* td styles here */ }
&:is(th) { /* th styles here */ }
}
编译后的 SCSS 将产生这个 CSS
.foo:is(td) {
color: #9bc
}
.foo:is(th) {
color: #666
}
<table>
<tr>
<th class="foo">heading</th>
<td class="foo">table cell</td>
</tr>
</table>
推荐阅读
- objective-c - 无法从 Objective C 中的单独 NSWindow 执行 NSPanel 中的方法
- i3 - i3:来回旋转容器布局
- android - Xamarin Forms [Android] 应用程序在后台静默推送 => 冻结
- flutter - 此处不能使用前缀“Hive”,因为它被本地声明所遮蔽
- php - 我如何直接从工具栏菜单导航而不手动编辑它
- spring - 忽略解析模板并在 SpringREST API 中返回 JSON 响应
- passwords - 如果我不能选择与以前相同或相似的密码,它是否以明文形式存储在某处?
- codeblocks - 使用代码块启动调试对象失败
- admin - 管理部分的喊话箱?
- python - 为什么 pandas 在我的 CSV 中的这个位置写入 escapechar?