html - 如何根据 div 的类对 div 中的元素进行样式设置?
问题描述
基本上,我正在为我的网站创建一个深色主题系统,并在dark
调用适当的函数时将类添加到 html 标记中。我正在使用 CSS 变量--light-theme-bg: white;
,并使用var(--light-theme-bg);
. 如何hr
根据dark
该类是否附加到html
元素来设置特定元素的样式。我怎样才能做到这一点?
解决方案
范围界定是你的朋友。您需要在 CSS 中添加两条规则。一种用于深色主题,一种用于浅色主题。
在这些规则中,您可以定义一个--background
var。
所有引用该 var 的子元素都会尊重它。
.light {
--background: #f9f9f9;
}
.dark {
--background: #191919;
}
.first,
.second {
color: red;
background: var(--background);
}
<div class="light">
<div class="first"> I'm the first div</div>
<div class="second">I'm the second div</div>
</div>
<div class="dark">
<div class="first"> I'm the first div</div>
<div class="second">I'm the second div</div>
</div>
推荐阅读
- java - g1 gc中的`Ref Enq`做了什么
- orientdb - orientdb 在查询到来时如何找到合适的主节点以及如何知道数据是最新的?
- python-3.x - 值未从 UDF 中的输入打印
- twitter-bootstrap - 引导页面在 PyCharm 的 python 项目中没有任何影响
- unit-testing - 将 GoogleTest 与 C 项目一起使用的示例 CmakeList.txt
- python-2.7 - Python matplot 缺少条形图中的条形
- python - 在 python 中将 b'\x10' 转换为 int 16
- angular - 拖放不适用于 ag-grid-angular
- python - 我应该如何在 Travis 上使用 moto 测试 AWS S3?
- ocaml - ocamlc可以编译ml和mli以外的文件吗?