@font-face只在本地机器上安装了字体后才起作用。

huangapple go评论99阅读模式
英文:

@font-face only working if the font is installed on the local machine

问题

我正在尝试使用自定义字体,该字体在Google Fonts中不可用。我解压了字体文件并将其放入了我的React项目的src/assets/fonts目录中。

在我的index.css文件中,我像这样加载字体:

@font-face {
  font-family: 'LemonMilk';
  src: local('LemonMilk'), url(./assets/fonts/LemonMilk.otf) format('otf');
}

我在本地主机上和托管在网络上的应用程序上都进行了测试,它在我的机器上可以正常工作,因为字体是本地安装的,但如果我在不同的机器上打开应用程序,它就无法正常工作。我还从我的机器中删除了字体文件,自那以后就显示了备用字体。我错过了什么吗?谢谢。

英文:

I am trying to use a custom font, which is not available via Google Fonts. I unzipped the font and put it into src/assets/fonts of my React project.

In my index.css I am loading the font like:

@font-face {
  font-family: 'LemonMilk';
  src: local('LemonMilk'), url(./assets/fonts/LemonMilk.otf) format('otf');
}

I tested the app both on the localhost and hosted it on the web for testing and it is working fine, since the font is installed locally on my machine, but if I open the app on a different machine it is not working. I also deleted the font from my machine and the fallback font started to show since then. What am I missing ? Thank you

答案1

得分: 2

The correct font name is Lemon/Milk, and you'll need to convert it to other formats like WOFF and WOFF2 as well.

Please Follow this:

  1. Go to this Website for a free online font generator.
  2. Upload your font and check TTF, EOT, WOFF, WOFF2 (See browser support).
  3. Click Convert > Download.
  4. Upload the newly converted fonts to your server.

Finally, your CSS should look similar to this.

@font-face {
    font-family: 'Lemon/Milk';
    src: url('LemonMilkbold.eot');
    src: url('LemonMilkbold.eot?#iefix') format('embedded-opentype'),
        url('LemonMilkbold.woff2') format('woff2'),
        url('LemonMilkbold.woff') format('woff'),
        url('LemonMilkbold.ttf') format('truetype');
    font-weight: bold;
    font-style: normal;
}

Please Note that you'll need to convert any other font weight too, and don't forget to add the correct path.

英文:

The correct font name is Lemon/Milk and you'll need to convert it to other format like WOFF WOFF2 as well.

Please Follow this:

  1. Go the this Website free online font generator
  2. Upload your font and check TTF, EOT, WOFF, WOFF2 (See browser support)
  3. Click Convert > Download
  4. Upload the newly converted fonts to your server

Finally your CSS should look like similar to this.

@font-face {
    font-family: 'Lemon/Milk';
    src: url('LemonMilkbold.eot');
    src: url('LemonMilkbold.eot?#iefix') format('embedded-opentype'),
        url('LemonMilkbold.woff2') format('woff2'),
        url('LemonMilkbold.woff') format('woff'),
        url('LemonMilkbold.ttf') format('truetype');
    font-weight: bold;
    font-style: normal;
}

Please Note that you'll need to convert any other font weight too. and don't forget to add the correct path

答案2

得分: 0

Sure, here's the translation:

看起来您的浏览器在理解字体文件方面出现了问题。尝试从字体文件创建网页字体包。有很多工具可以实现这一目标:

https://www.fontsquirrel.com/tools/webfont-generator

网页字体包将包含所有主要支持的格式的字体,并会为您生成CSS的@font指令。

免责声明:请确保您有权使用该字体。

英文:

It's looking like Your browser has a problem with understanding the font file. Try to make the webfont package from the font file. There is lot of tools to approach it:

https://www.fontsquirrel.com/tools/webfont-generator

Webfont package will contain the font in all major supported formats and will generate the css @font directive for You.

DISCLAIMER: Ensure that You have the rights to use the font.

答案3

得分: 0

我建议为您的字体创建一个不同的CSS文件,然后将其导入到您的主CSS中,还可以使用此网站生成字体的字体文件!

英文:

well i suggest creating a different css file for your fonts then import it to your main css also this site generate font-face for your font with css file!

huangapple
  • 本文由 发表于 2020年1月6日 22:53:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定