如何更改元素的字体系列而不影响它的 ::before 或 ::after?
许多站点在元素上使用::
选择器通过客户端下载的字体文件加载图标,例如,
div {
font: 14px/1 FontAwesome;
}
div::before {
content: "\f1c8";
}
不幸的是,以下规则也适用于 :: 伪元素,打破图标显示:
div {
font-family: sans-serif !important;
}
不是可能可以:not(:: :: fron)
(source),因此您将如何定位元素,但不是:: ::
:: /代码>?
这很好地工作了,但是它错过了元素内部的文本(如果有):
div:not([class*="fa-"]) {
font-family: sans-serif !important;
}
甚至可能是不可能的。请不要JavaScript。
A lot of sites use the ::before
selector on an element to load icons via a client-downloaded font file, e.g.
div {
font: 14px/1 FontAwesome;
}
div::before {
content: "\f1c8";
}
Unfortunately the following rule also applies to the element's ::before
pseudo-element, which breaks the icon display:
div {
font-family: sans-serif !important;
}
It's not possible to :not(::before)
(source), so how would you go about targeting an element, but not it's ::before
?
This worked decently, but it misses the text (if any) inside the element:
div:not([class*="fa-"]) {
font-family: sans-serif !important;
}
It may not even be possible. No JavaScript, please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可能再次覆盖前后,
否则您只能将图标完全在另一个元素中使用。
和asa sidenote :)
请使用文本来进行文本而不是divs(跨度也不是文本元素,它只是一个内联元素,没有任何语义信息)
It's not possible without then again overwriting the before and after again
Or you could just use the icon in another element entirely.
And a s a sidenote :)
Please use textelements for text, not divs (and span is also not a text element, it simply is an inline element without any semantic information)
在 @Termani 的帮助下,我解决了将我喜欢的字体注入网站的问题,同时对通过 webfont 文件加载的大多数网站图标造成的损害最小:
毫无疑问,还有其他
font-family
名称开发人员使用,所以当我偶然发现它们时,这个列表会不断增长。如果我找到更好的解决方案,我会更新这个答案。
With @Termani's help above, this is how I solved the problem of injecting my preferred font into websites while doing minimal damage to most site's icons loaded via webfont files:
Without doubt there are other
font-family
names that developers use, so the list will grow as I stumble upon them.I'll update this answer if I find a better solution.