css - 从更通用的选择器继承值
问题描述
定义了以下样式:
h1 {
margin-bottom: 2em;
}
h1:last-child {
margin-bottom: 0;
}
如果我有一个 h1 元素,它是我想为其应用普通 h1 元素的 margin-bottom 的最后一个子元素,有没有比为该元素再次显式定义 2em 的 margin-bottom 更好的方法?
非常感谢
解决方案
使用 CSS 变量:
h1 {
margin-bottom: var(--b,2em);
}
h1:last-child {
--b: 0; /* override the default 2em */
}
h1.custom {
--b:initial; /* get back to the default 2em*/
}
div {
border:1px solid;
}
<div>
<h1>some text</h1>
</div>
<div>
<h1 class="custom">some text</h1>
</div>
推荐阅读
- java - 如何在 Java 中使用 kubernetes-client 获取部署状态
- python - python中的加权相关函数
- c++ - 在另一个线程中调用插槽不会被调用
- c++ - 在一行中打印字符,在每个字符后使用 sleep() 函数
- python - Xpath 选择器 - 是否可以在 selenium 中按标题查找元素?
- sql-server - SQL 数据库设计 - 大型权限表
- xml - 使用 Qt Quick XmlListModel 读取本地 XML 文件
- c++ - 在 macOS Homebrew 上安装 CGAL
- math - 计算机是先对运算中的数字进行四舍五入还是对结果进行四舍五入?
- java - 基于 HostSNI 和 gRPC 连接 Traefik 中的服务