是否可以使用scss从目录导入字体?

Pau*_*aul 10 sass

我有一个scss文件,以这种方式实现字体导入:

@import url(https://fonts.googleapis.com/css?family=PT+Sans+Caption:400,700&subset=latin-ext,cyrillic); 
Run Code Online (Sandbox Code Playgroud)

我知道使用CDN可以为用户提供缓存优势,但这是内部站点,可以在服务器上使用而无需访问wide web.而且我不确定用户机器是否也可以访问Internet.所以我想用其他页面静态文件服务前端.

SCSS中有没有办法从服务器上的某个目录导入字体?就像是:

@import dir(/path/to/fonts/file)
Run Code Online (Sandbox Code Playgroud)

或者SCSS没有这个功能?

JTr*_*x16 23

据我所知,您无法@import在SCSS中导入字体.您可以使用包含字体@font-face.例如:

@font-face {
    font-family: 'Open Sans';
    src: url(path/to/file) format(Example: 'truetype' or 'opentype' depending on the file extension of your font);
}


// USAGE
body {
    font-family: 'Open Sans', sans-serif;
}
Run Code Online (Sandbox Code Playgroud)

  • 效果很好。现在还建议在“@font-face”规则中添加“font-display: swap;”。给你一些额外的谷歌积分;)参见https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display (5认同)
  • 这就是来自Google字体的`@ import`。如果将导入URL放到浏览器中,这就是您所看到的(一堆`@ font-face`)。 (2认同)

小智 5

虽然有点晚了,但您可以使用以下格式与 libsass 进行外部导入

@import url("http...");