css - 将本地字体转换为 Google Fonts CDN

标签 css font-face webfonts google-webfonts roboto

我的网站上有一堆机器人字体。我想使用 Google 字体 CDN 而不是仅仅为了性能问题。当我将 google CDN url 放入 url("@import url('https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@100&display=swap'") 时遇到一个问题不工作。 例如,下面的 CSS 使用本地 Robot 字体,我应该将其替换为 Google 字体 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');
}
  1. 机器人常规 400
  2. 机器人中号 500
  3. 机器人粗体 600
  4. roboto 板常规 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

关于css - 将本地字体转换为 Google Fonts CDN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69193322/

相关文章:

css - .eot 字体是否通过 IE8 上的@font-face datauri 支持?

css - webfont 在 IE10、IE11 中的字距调整不正确

performance - 如果我们避免使用高流量站点的@font-face,对性能会有好处吗?

fonts - Safari 用户安装的字体不呈现

css - 从谷歌网络字体或从自己的服务器加载字体,哪一个在页面加载时间方面更有效?

css - Web 字体的 Cookie 请求 header

html - 页面加载之间的页面加载不同

JQuery - 获取可滚动元素的当前高度

CSS:屏幕中央位置加载指示器

css - 垂直对齐最大高度 li 元素并将其与所有其他元素匹配。