如何导入 .woff2 字体并在 sass 设置中使用它?

Joh*_*ews 3 css fonts sass font-face gulp

我有一个文件,它是gulp的sass设置_variables.scss的一部分。

在此文件中,我能够导入 Google 字体,如下所示:

$font: "https://fonts.googleapis.com/css?family=Nunito:300,400,600,700" !default;
$font-family-custom-sans-serif: "Nunito", sans-serif !default;
$font-family-base: $font-family-custom-sans-serif !default;
Run Code Online (Sandbox Code Playgroud)

我现在尝试用文件中的自定义导入替换此字体.woff2。我尝试了以下方法,但这似乎不起作用:

@font-face {
  font-family: 'Gellix';
  src:  url("../../assets/img/brand/Gellix-Bold.woff2") format('woff2'),
        url("../../assets/img/brand/Gellix-Regular.woff2") format('woff2')
}

$font: "Gellix", sans-serif !default;
$font-family-custom-sans-serif: "Gellix", sans-serif !default;
$font-family-base: $font-family-custom-sans-serif !default;
Run Code Online (Sandbox Code Playgroud)

小智 10

您需要为每个字体变体编写一个单独的@font-face

在你的情况下,它应该看起来像这样:

@font-face {
  font-family: 'Gellix';
  font-weight: 400;
  font-style: normal;
  src: url("../../assets/img/brand/Gellix-Regular.woff2") format('woff2');
}
@font-face {
  font-family: 'Gellix';
  font-weight: 700;
  font-style: normal;
  src: url("../../assets/img/brand/Gellix-Bold.woff2") format('woff2');
}
Run Code Online (Sandbox Code Playgroud)

然后检查浏览器控制台看看你指定的路径是否正确

欲了解更多信息:https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face