css - @fontface custom font not loading in Gatsby
问题描述
my custom font is not working, unless I install it manually in my computer.
//my css file//
@font-face {
font-family:'PROGRESS PERSONAL USE';
src: url('Progress.woff2')format('woff2'),
url('Progress.woff')format('woff'),
url('Progress.ttf') format('ttf');
}
Font family name is correct because, I opened the file font to check the correct spelling and also it shows correctly in the browser if I install the font and use it as:
h1{
font-family: "PROGRESS PERSONAL USE"
}
I thought that the problem was the path of the font files, so I placed them in the same folder and level as the CSS file. I also tried adding a /
to the path like url('/Progress.woff2')format('woff2')...
in front. Cleared caché, changed browser, and tried in mobile But not working.
Also in my browser inspector/Network shows that the fonts are found when refreshing the browser:
Request URL: http://localhost:8000/Progress.woff2
Request Method: GET
Status Code: 200 OK
Remote Address: 127.0.0.1:8000
Referrer Policy: strict-origin-when-cross-origin
Would be happy to get help with this!
解决方案
The self-hosted fonts in Gatsby should be located in the static folder. So I placed the files in the path:
--> static/fonts/Progress.woff2
-->static/fonts/Progress.woff
-->static/fonts/Progress.ttf
and then in my globalStyles.css which is in my src/ (not in the static folder) I place it like this:
@font-face {
font-family:"PROGRESS PERSONAL USE";
src: url('/fonts/Progress.woff2')format('woff2'),
url('/fonts/Progress.woff')format('woff'),
url('/fonts/Progress.ttf') format('ttf');
}
If you don't have a global css file you can define one for the fonts, and import it in your gatsby-browser.js.
Also there is the option of using the web fonts gatsby pluggin which is explained here
推荐阅读
- c# - 如何使用 PocketSphinx 库在 Xamarin Android (Visual Studio) 中实现 Speech to Text 系统?
- node.js - 如何使用 Mongoose 查找子文档?
- android - JobIntentService 定期运行?
- python - 类中的方法如何访问类常量变量?
- c# - TCP 套接字在 Xamarin.Forms 中未按预期工作
- javascript - Vue - 如何将父引用作为道具传递给孩子?
- java - 如何对作为字符串的整数进行排序?
- ignite - 使用 gridgain 代替 ignite 有什么好处?
- python - 如何在 django restapi[POST] 中编写将 field1 和 field2 的数据库值添加到 field3?
- c# - 将笛卡尔点转换为极坐标