简体   繁体   English

如何在 Safari 中加载带有 CSS 的自定义字体?

[英]How can I load a custom font with CSS in Safari?

I am creating a simple web page and I am applying a custom font (Roboto).我正在创建一个简单的 web 页面,并且正在应用自定义字体 (Roboto)。 I have declared them in the CSS file but Chrome detects it fine and Safari does not.我已经在 CSS 文件中声明了它们,但 Chrome 检测到它很好,而 Safari 没有。

@font-face {
    font-family: 'Roboto';
    src: url('/fonts/roboto500.eot');
    src: url('/fonts/roboto500.eot') format('embedded-opentype'), 
         url('/fonts/roboto500.woff2') format('woff2'), 
         url('/fonts/roboto500.woff') format('woff'), 
         url('/fonts/roboto500.ttf') format('truetype'), 
         url('/fonts/roboto500.svg') format('svg');
}

@font-face {
    font-family: 'Roboto';
    src: url('/fonts/roboto700.eot');
    src: url('/fonts/roboto700.eot') format('embedded-opentype'), 
         url('/fonts/roboto700.woff2') format('woff2'), 
         url('/fonts/roboto700.woff') format('woff'), 
         url('/fonts/roboto700.ttf') format('truetype'), 
         url('/fonts/roboto700.svg') format('svg');
}

h1.site-title,
h2.subtitles {
    font-family: 'Roboto' !important;
    font-weight: bold;
}

h4.subtitles {
    font-family: 'Roboto' !important;
    font-weight: bold;
}

p {
    font-family: 'Roboto' !important;
    font-weight: normal;
    color: rgb(90, 90, 90);
}

I checked with the Safari console to see if it was getting the fonts, and it does, but when I look at the files one by one, they don't match the original font.我检查了 Safari 控制台,看它是否正在获取 fonts,确实如此,但是当我一个一个地查看文件时,它们与原始字体不匹配。

Finally, thanks to some comments and such, I got it to work using another download link:最后,感谢一些评论等,我使用另一个下载链接让它工作:

https://fonts.googleapis.com/css?family=Roboto https://fonts.googleapis.com/css?family=Roboto

From here Google offers different components to add to the CSS of your project, what I did was, just in case in the future that typography was no longer available in that link and I lost it, I downloaded it from the link that appears and I put it locally in my project and linked it with the relative path.从这里 Google 提供了不同的组件来添加到您项目的 CSS,我所做的是,以防万一将来该链接中的排版不再可用而我丢失了它,我从出现的链接下载了它并且我将它放在我的项目本地并将其与相对路径链接。

I have no idea if it was because the files I downloaded didn't work or any other reason, but the fact is that from this link and using only the one in woff2 format, it works both in Chrome and Safari.我不知道是不是因为我下载的文件不起作用或任何其他原因,但事实是,通过此链接并仅使用 woff2 格式的文件,它在 Chrome 和 Safari 中均有效。

@font-face {
    font-family: 'Roboto';
    font-style: normal;
    font-weight: 500;
    src: url(../fonts/roboto/roboto.woff2) format('woff2');
}

I hope this may be of help to someone in the future.我希望这可能对将来的某个人有所帮助。 Many thanks to all of you who have responded.非常感谢所有回复的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM