如何更改元素的字体系列而不影响它的 ::before 或 ::after?

发布于 2025-01-18 20:52:46 字数 676 浏览 2 评论 0原文

许多站点在元素上使用::选择器通过客户端下载的字体文件加载图标,例如,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2025-01-25 20:52:46

不可能再次覆盖前后,

div {
  font-family: sans-serif;
}

div::before,
div::after {
  font-family: serif;
}

否则您只能将图标完全在另一个元素中使用。

<span class="fa-something"></span>
<span>Text here</span>

和asa sidenote :)
请使用文本来进行文本而不是divs(跨度也不是文本元素,它只是一个内联元素,没有任何语义信息)

It's not possible without then again overwriting the before and after again

div {
  font-family: sans-serif;
}

div::before,
div::after {
  font-family: serif;
}

Or you could just use the icon in another element entirely.

<span class="fa-something"></span>
<span>Text here</span>

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)

一绘本一梦想 2025-01-25 20:52:46

在 @Termani 的帮助下,我解决了将我喜欢的字体注入网站的问题,同时对通过 webfont 文件加载的大多数网站图标造成的损害最小:

::before, ::after {
    font-family: FontAwesome, "Font Awesome", "Font Awesome 5 Pro",
    "Font Awesome 5 Free", "Material Icons", "Material-Design-Iconic-Font",
    Flaticon, "CBSi Fantasy icomoon", CBSi_Fantasy_icomoon, icon-moon,
    icomoon, ui-icons, icons, NewYorkIcons, sans-serif !important;
}

毫无疑问,还有其他 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:

::before, ::after {
    font-family: FontAwesome, "Font Awesome", "Font Awesome 5 Pro",
    "Font Awesome 5 Free", "Material Icons", "Material-Design-Iconic-Font",
    Flaticon, "CBSi Fantasy icomoon", CBSi_Fantasy_icomoon, icon-moon,
    icomoon, ui-icons, icons, NewYorkIcons, sans-serif !important;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文