【问题标题】:Duplicate @font-face declaration in css, url fetched twice?css中重复的@font-face声明,url被提取了两次?
【发布时间】:2021-04-29 09:17:37
【问题描述】:

我正在覆盖 @font-face css 声明:

/* ORIGINAL */
@font-face {
  font-family: 'Font1';
  src: url('/PATH1/font1.eot');
  ...
}

/* MY OVERRIDE */
@font-face {
  font-family: 'Font1';
  src: url('/PATH2/font1.eot');
  ...
}

我注意到浏览器会尝试同时采用'/PATH2/font1.eot' 和'/PATH1/font1.eot' 两种路径。 有没有办法避免两个提取之一,只留下一个?

谢谢

【问题讨论】:

  • 您必须将其从原始字体中删除,否则,您会要求计算机加载字体,然后您要求他加载另一种字体并告诉他它会覆盖第一个字体.. .
  • 也许这可以帮助你stackoverflow.com/a/16904853/2887133

标签: css


【解决方案1】:

@font-face 规则定义了一个外部字体:

<style id="customfont">
@font-face {

  font-family: a_font;
  src: url(https://fonts.gstatic.com/s/quicksand/v22/6xK-dSZaM9iE8KbpRA_LJ3z8mH9BOJvgkP8o58a-wg.woff2);
}

body {
  font-family: a_font !important;
}
</style>
<p>Some text. The quick brown fox jumped over the lazy dog... but it didn't make it up all the way.</p>

要更改它,请修改元素的innerHTML 属性。

以下是修改元素 HTML 代码的方法:

+--------------+------------------------------+
| innerHTML    | HTML within the tags         |
+--------------+------------------------------+
| outerHTML    | HTML, with the tags          |
+--------------+------------------------------+
| appendChild  | add an element to the end    |
+--------------+------------------------------+
| prepend      | prepend element              |
+--------------+------------------------------+

这个例子使用了innerHTML:

<style id="customfont">
@font-face {

  font-family: a_font;
  src: url(https://fonts.gstatic.com/s/quicksand/v22/6xK-dSZaM9iE8KbpRA_LJ3z8mH9BOJvgkP8o58a-wg.woff2);
}

body {
  font-family: a_font !important;
}
</style>
<p>Some text. The quick brown fox jumped over the lazy dog... but it didn't make it up all the way.</p>
<button onclick="change()">Change @font-face src</button>
<script type="text/javascript">
function change() {
  var newstyle=document.getElementById('customfont');
  var newfontsrc="https://fonts.gstatic.com/s/sofia/v9/8QIHdirahM3j_su5uI0.woff2";
  var newtext = '@font-face {\n';
  newtext += 'font-family:a_font;\n';
  newtext += 'src:url('+newfontsrc+');\n';
  newtext += '}\n';
  newtext += '\nbody {\n'
  newtext += '  font-family:a_font !important;\n';
  newtext += '}'
  newstyle.innerHTML=newtext;
}
</script>

这应该会改变字体 src。

【讨论】:

    猜你喜欢
    • 2013-08-21
    • 2010-10-08
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2011-04-25
    相关资源
    最近更新 更多