css - 将本地字体转换为 Google 字体 CDN
问题描述
我的网站上有一堆机器人字体。我想使用谷歌字体 CDN 而不是仅仅为了性能问题。当我将 google CDN url 放入 url("@import url('https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@100&display=swap'") 时,我遇到了一个问题不行。例如,下面的 CSS 使用本地机器人字体,我应该用谷歌字体 CDN 替换它。我应该怎么做才能解决这个问题?
@font-face {
font-family: roboto regular;
font-display: auto;
font-style: normal;
font-weight: 400;
src: local('Roboto Regular'), url(../webfonts/Roboto-Regular.woff) format('woff');
}
@font-face {
font-family: roboto medium;
font-display: auto;
font-style: normal;
font-weight: 500;
src: local('Roboto Medium'), url(../webfonts/Roboto-Medium.woff) format('woff');
}
@font-face {
font-family: roboto bold;
font-display: auto;
font-style: normal;
font-weight: 600;
src: local('Roboto Bold'), url(../webfonts/Roboto-Bold.woff) format('woff');
}
@font-face {
font-family: roboto slab regular;
font-style: normal;
font-weight: 400;
src: local('Roboto Slab Regular'), url(../webfonts/RobotoSlab-Regular.woff) format('woff');
}
- 机器人常规 400
- 机器人中型 500
- 机器人粗体 600
- 机器人平板常规 400
解决方案
您可以像这样在 HTML 中链接它:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Slab&family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
或将其导入您的 CSS:
@import url('https://fonts.googleapis.com/css2?family=Roboto+Slab&family=Roboto:wght@400;500;700&display=swap');
然后像这样将它应用到你的css:
font-family: 'Roboto', sans-serif;
font-family: 'Roboto Slab', serif;
然后你可以删除你的@font-face
实现。
来源:https ://fonts.google.com/share?selection.family=Roboto%20Slab%7CRoboto:wght@400;500;700
推荐阅读
- javascript - 尝试在我的 React 应用程序中使用链接但遇到问题
- inverse-kinematics - IK 的雅可比矩阵的两个伪逆矩阵之间的区别?
- python - Pandas:列值上的位移运算符(>>)
- c++ - 具有特定数据类型(如字符串)的多个模板的模板函数?
- php - PHP 会话变量在 Firefox 63.0、MacOS Mojave 上的 Safari 12.0 和 iOS 12.0.1 上的 Safari 中丢失
- linux - 无法安装快照
- java - 如何修复此错误操作数 (&&)
- sql - 使用 Join 优化 SQL 查询
- kernel - 如何在 2 个 FreeBSD 内核模块之间共享数据?
- python - 在 While 循环中从 Pandas 数据框中查找特定的数据行