css - :root, html, * 选择器。有什么不同吗?
问题描述
这三个 CSS 规则之间有什么区别吗?
* {
font-size: 24px
}
:root {
font-size: 24px
}
html {
font-size: 24px
}
解决方案
是,有一点不同。下面是一些结果不一样的例子
使用*
* {
font-size: 24px
}
p {
font-size:2em;
}
<div>
<p>some text <span>here</span></p>
</div>
使用html
(或:root
)
html {
font-size: 24px
}
p {
font-size:2em;
}
<div>
<p>some text <span>here</span></p>
</div>
应用于font-size
所有元素不同于将 应用于font-size
html 元素并让所有元素继承该值。
在第一个示例中,span 将具有font-size
等于,24px
因为它是由 选择的*
。在第二个示例中,span 将继承 的计算值,p
因为没有选择器以它为目标。
之间html
有:root
一场特异性战争,其中:root
将是赢家:
html {
font-size: 999px;
}
:root {
font-size:24px;
}
<div>
<p>some text <span>here</span></p>
</div>
:root {
font-size:24px;
}
html {
font-size: 999px;
}
<div>
<p>some text <span>here</span></p>
</div>
推荐阅读
- java - 仅使用一种方法重置与 Swing 不同
- c++ - 具有复制构造函数和指针的无限复制列表循环
- javascript - Instagram API 和 Flickity 未在加载时启动
- azure-devops - 有关没有工件的 Azure DevOps 发布管道的问题
- hashicorp-vault - 拒绝 root 在 Vault 中查找令牌的权限
- c++ - How to define boost tokenizer to return boost::iterator_range
- javascript - 使用javascript隐藏和取消隐藏div
- c# - 如何始终在 ASP.NET Core 应用程序中为给定类型发送特定值?
- shell - 如何在 MobaXterm 上自定义 URL 解释
- android - 具有 15 分钟计时器的 Android 通知