html - 防止儿童的默认 html 样式被覆盖
问题描述
div {
height: 400px;
cursor: pointer;
font-family: sans-serif;
}
<div>
div has pointer and sans-serif font
<p>and paragraph also has it</p>
<code>but code element doesn't have sans-serif font but pointer.</code>
</div>
p
元素默认具有光标text
和字体系列。serif
两者都被覆盖。
code
元素默认具有光标text
和字体系列。monospace
只有光标已被覆盖。
为什么会这样?在code
元素中,font-family
不会被覆盖。为什么?如何在某些选定元素中设置默认 html 样式?(p
和code
这里的元素)
显然,甚至all: revert
不起作用!
div {
height: 400px;
cursor: pointer;
font-family: sans-serif;
}
p {
all: revert;
}
code {
all: revert;
}
<div>
div has pointer and sans-serif font
<p>and paragraph also has it</p>
<code>but code element doesn't have sans-serif font but pointer.</code>
</div>
all: initial
有效,但我不想应用原始的 CSS 实现。我希望应用用户代理样式表规则。我用过revert
但没有用。
解决方案
根据 MDN:
默认情况下,内容文本使用用户代理的默认等宽字体显示。
和:
可以为代码选择器定义 CSS 规则以覆盖浏览器的默认字体。用户设置的首选项可能优先于指定的 CSS。
因此,您确实需要指定具体所需的字体。如果担心正确的设置未结转/将来可能会更改,您可以将其定义为 CSS 变量,因此只需在一处更改其值。
div {
height: 400px;
cursor: pointer;
--font: sans-serif;
font-family: --font;
}
p {
all: revert;
}
code {
all: revert;
font-family: --font;
}
<div>
div has pointer and sans-serif font
<p>and paragraph also has it</p>
<code>and the code element also has sans-serif</code>
</div>
推荐阅读
- node.js - 如何在 Vue.js 应用程序中显示 HTTP 流
- bash - 通过脚本更改密码
- c# - MongoDb C# Driver with Group and Project using Aggregate() 查询给出异常
- c# - 如何使用启用了多重身份验证的 Office 365 帐户中的 VB.NET 或 C# 代码发送电子邮件?
- node.js - What is the `PM2` for command `yarn run start`?
- xml - 使用 xslt 来自特定节点类型的 XML 到 csv
- angular - Angular6 & tinyMCE 编辑器
- if-statement - 带有检索和变量的 sparql 查询 if 语句
- php - 如何将图像名称保存在数据库中以便以后查看?
- excel - 删除 VBA 工作表中提到的多个变量列