多个字体权重,一个@ font-face查询

Rve*_*urt 85 css fonts font-face

我必须导入Klavika字体,我收到了多种形状和大小:

Klavika-Bold-Italic.otf
Klavika-Bold.otf
Klavika-Light-Italic.otf
Klavika-Light.otf
Klavika-Medium-Italic.otf
Klavika-Medium.otf
Klavika-Regular-Italic.otf
Klavika-Regular.otf
Run Code Online (Sandbox Code Playgroud)

现在我想知道是否有可能只用一个@font-face-query 将它们导入到CSS中,我weight在查询中定义了它.我想避免复制/粘贴查询8次.

所以类似于:

@font-face {
  font-family: 'Klavika';
  src: url(../fonts/Klavika-Regular.otf), weight:normal;
  src: url(../fonts/Klavika-Bold.otf), weight:bold;
}
Run Code Online (Sandbox Code Playgroud)

mai*_*man 205

实际上有一种特殊的@ font-face风格可以满足您的要求.

这是一个使用相同字体系列名称的示例,其中不同的样式和权重与不同的字体相关联:

@font-face {
font-family: 'DroidSerif';
src: url('DroidSerif-Regular-webfont.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'DroidSerif';
src: url('DroidSerif-Italic-webfont.ttf') format('truetype');
font-weight: normal;
font-style: italic;
 }
@font-face {
font-family: 'DroidSerif';
src: url('DroidSerif-Bold-webfont.ttf') format('truetype');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'DroidSerif';
src: url('DroidSerif-BoldItalic-webfont.ttf') format('truetype');
font-weight: bold;
font-style: italic;
}
Run Code Online (Sandbox Code Playgroud)

您现在可以指定font-weight:boldfont-style:italic任何您喜欢的元素,而无需指定font-family或覆盖font-weightfont-style.

body { font-family:"DroidSerif", Georgia, serif; }
h1 { font-weight:bold; }
em { font-style:italic; }
strong em {
font-weight:bold;
font-style:italic;
}
Run Code Online (Sandbox Code Playgroud)

有关此功能和标准用法的完整概述,请参阅本文.


示例笔

  • 就我而言,这只使用最低的@font-face。那将是 font-weight: bold; 字体样式:斜体;每次我提到 font-family: 'DroidSerif'; (7认同)
  • 这种方法是正确的,并在这篇 Mozilla 文章中进行了概述:https://hacks.mozilla.org/2009/06/beautiful-fonts-with-font-face/。您可以看出笔正在工作,因为它将“粗体”字体定义为斜体字体。如果浏览器伪造它,它会显示为粗体,但它会按预期显示为斜体。此方法的优点还在于它可以正确设置后备字体的样式,因为“font-weight:bold”(或其他)与“font-family”声明一起使用。 (3认同)
  • 你有测试证明这个方法确实有效吗?您如何证明浏览器不仅仅是伪造粗细或样式而不读取正确的字体文件? (2认同)
  • @rojobuffalo提出了一个非常有效的观点.该笔没有做任何事情来验证断言的有效性,即它将按预期运行.具体来说,我从CSS示例中删除了粗体font-face声明并再次运行它.外观相同.是从缓存中拉出来的吗?浏览器是否只是摆弄常规字体的显示重量? (2认同)

Mir*_*ová 13

@font-face {
  font-family: 'Klavika';
  src: url(../fonts/Klavika-Regular.otf) format('truetype') font-weight-normal,
       url(../fonts/Klavika-Bold.otf) format('truetype') font-weight-bold,
       url(../fonts/Klavika-Bold-Italic.otf) format('truetype') font-italic font-weight-bold;
}
Run Code Online (Sandbox Code Playgroud)

  • 请写一些关于为什么这会解决问题的解释。它可以是一个简单的句子。 (26认同)
  • 我在哪里可以阅读有关此符号的信息?MDN 上没看到 (10认同)
  • 字体粗细 100、200 等怎么样? (8认同)
  • 它似乎不起作用,我找不到资源,但似乎有一些道理。 (4认同)
  • 与前一个解决方案相同。只是更短的符号:) (3认同)
  • 这个解决方案对我来说也不起作用。 (3认同)
  • 我喜欢这种语法,但我在网上找不到任何对此的参考。它在 Chrome 中对我不起作用,并且被 jigsaw.w3.org 认为是无效的 CSS (2认同)

her*_*zel 8

2022 年更新:使用可变字体

\n

参考原来的问题:\n不,你绝对不能在单个@font-face规则中加载多个粗细或样式变体(例如斜体或压缩),除非你正在加载可变字体。

\n

如果您的字体可用作可变字体,您实际上可以在单个规则中组合多个字体粗细,@font-face如下所示:

\n
@font-face {\n  font-family: \'Roboto Flex\';\n  font-style: normal;\n  font-weight: 100 1000;\n  font-stretch: 0% 200%;\n  src: url(https://fonts.gstatic.com/s/robotoflex/v9/NaNeepOXO_NexZs0b5QrzlOHb8wCikXpYqmZsWI-__OGfttPZktqc2VdZ80KvCLZaPcSBZtOx2MifRuWR28sPJtUMbsFEK6cRrleUx9Xgbm3WLHa_F4Ep4Fm0PN19Ik5Dntczx0wZGzhPlL1YNMYKbv9_1IQXOw7AiUJVXpRJ6cXW4O8TNGoXjC79QRyaLshNDUf9-EmFw.woff2) format(\'woff2\');\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我们需要通过添加 2 个值来指定字体粗细范围:

\n
font-weight: 100 1000;\n
Run Code Online (Sandbox Code Playgroud)\n

\r\n
\r\n
@font-face {\n  font-family: \'Roboto Flex\';\n  font-style: normal;\n  font-weight: 100 1000;\n  font-stretch: 0% 200%;\n  src: url(https://fonts.gstatic.com/s/robotoflex/v9/NaNeepOXO_NexZs0b5QrzlOHb8wCikXpYqmZsWI-__OGfttPZktqc2VdZ80KvCLZaPcSBZtOx2MifRuWR28sPJtUMbsFEK6cRrleUx9Xgbm3WLHa_F4Ep4Fm0PN19Ik5Dntczx0wZGzhPlL1YNMYKbv9_1IQXOw7AiUJVXpRJ6cXW4O8TNGoXjC79QRyaLshNDUf9-EmFw.woff2) format(\'woff2\');\n}\n
Run Code Online (Sandbox Code Playgroud)\r\n
font-weight: 100 1000;\n
Run Code Online (Sandbox Code Playgroud)\r\n
slider_weight.addEventListener("input", function () {\n  var axisValue = slider_weight.value;\n  testarea.style.fontWeight = axisValue;\n  value_weight.innerText = axisValue;\n});\n\nslider_width.addEventListener("input", function () {\n  var axisValue2 = slider_width.value;\n  testarea.style.fontStretch = axisValue2+\'%\';\n  value_width.innerText = axisValue2;\n});
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n

检索谷歌可变字体......棘手

\n

我强烈推荐这篇文章,因为当前的谷歌字体用户界面并不是特别有用:
\n如何从谷歌字体获取可变字体链接?

\n

请记住,@font-face不会自动将字体粗细应用于诸如等的强调<strong>元素<h1><h2><h3>

\n

因此,请确保为可预测的渲染指定所需的字体粗细如下所示:

\n
/* Variable fonts: we can apply intermediate weight values - yay! */  \nstrong{\n   font-weight: 623 \n}\n
Run Code Online (Sandbox Code Playgroud)\n

不要依赖基于关键字的权重或样式(例如半粗体、中等、半压缩等)

\n

例如:“中等”和“半粗体”有什么区别?
\n...这取决于字体设计、字体的粗细范围以及实际上取决于您的设计决策。
\n某些字体默认情况下相当“粗体”,其他字体则相当“细”或“浅”(相关文章:某些 Google 字体没有 Semibold 字体类型

\n
/* Roboto 400 is too bold for me \xe2\x80\x93 map default/regular weight 400 to 300 */  \nbody{\n  font-family: "Roboto Flex";\n  font-weight: 300;\n}\n\n/* Roboto 700 is not bold enough for me \xe2\x80\x93 map default/bold weight 700 to 1000*/  \nstrong{\n  font-weight: 1000\n}\n\n/* ... OK a medium weight might be handy \xe2\x80\x93 lets say 625  */  \n.medium{\n  font-weight: 625\n}\n\n/* condensed or expanded styles: font-stretch < 100 = condensed; \nfont-stretch > 100 = expanded; */\n.condensed{\n  font-stretch:0%;\n}\n.semi-condensed{\n  font-stretch:50%;\n}\n\n.semi-expanded{\n  font-stretch:200%;\n}\n\n.expanded{\n  font-stretch:200%;\n}\n
Run Code Online (Sandbox Code Playgroud)\n


Sli*_*ion 5

如果像我一样,您需要在不访问 Google 服务器的情况下加载 Roboto 字体,它应该如下所示:

/* Thin */
@font-face {
    font-family: 'Roboto';
    src: url(../fonts/Roboto-Thin.ttf) format('truetype');
    font-style: normal;
    font-weight: 100;
}

/* Light */
@font-face {
    font-family: 'Roboto';
    src: url(../fonts/Roboto-Light.ttf) format('truetype');
    font-style: normal;
    font-weight: 300;
}

/* Normal */
@font-face {
    font-family: 'Roboto';
    src: url(../fonts/Roboto-Regular.ttf) format('truetype');
    font-style: normal;    
    font-weight: 400;
}

/* Medium */
@font-face {
    font-family: 'Roboto';
    src: url(../fonts/Roboto-Medium.ttf) format('truetype');
    font-style: normal;
    font-weight: 500;
}

/* Bold */
@font-face {
    font-family: 'Roboto';
    src: url(../fonts/Roboto-Bold.ttf) format('truetype');
    font-style: normal;    
    font-weight: 700;
}

/* Black */
@font-face {
    font-family: 'Roboto';
    src: url(../fonts/Roboto-Black.ttf) format('truetype');
    font-style: normal;
    font-weight: 900;
}
Run Code Online (Sandbox Code Playgroud)

如果需要,然后对斜体变体执行相同的操作。有关将名称映射到数字权重的线索,请参阅font-weight 文档。这是Roboto 字体下载链接