【问题标题】:How do I use github to host a webfont?如何使用 github 托管 webfont?
【发布时间】:2017-02-03 06:22:57
【问题描述】:

更新:我使用了@brclz 的建议,这样解决了我的问题:

  1. 复制了家族各字体文件的GitHub地址,

例子:

https://github.com/h-ibaldo/Raleway_Fixed_Numerals/blob/master/font/rawline-100.woff
  1. 将网址中的github.com更改为raw.githubusercontent.com

例子:

https://raw.githubusercontent.com/h-ibaldo/Raleway_Fixed_Numerals/blob/master/font/rawline-100.woff
  1. 使用上述 URL 创建了一个 CSS 样式表,声明该系列的所有字体粗细和样式,

例子:

@font-face {
    font-family: 'rawline';
    src: url('https://cdn.rawgit.com/h-ibaldo/Raleway_Fixed_Numerals/master/font/rawline-100.eot');
    src: url('https://cdn.rawgit.com/h-ibaldo/Raleway_Fixed_Numerals/master/font/rawline-100.eot?#iefix') format('embedded-opentype'),
         url('https://cdn.rawgit.com/h-ibaldo/Raleway_Fixed_Numerals/master/font/rawline-100.woff2') format('woff2'),
         url('https://cdn.rawgit.com/h-ibaldo/Raleway_Fixed_Numerals/master/font/rawline-100.woff') format('woff'),
         url('https://cdn.rawgit.com/h-ibaldo/Raleway_Fixed_Numerals/master/font/rawline-100.ttf') format('truetype'),
         url('https://cdn.rawgit.com/h-ibaldo/Raleway_Fixed_Numerals/master/font/rawline-100.svg') format('svg');
    font-weight: 100;
    font-style: normal;
} 
  1. 将样式表拉到字体的 GitHub 存储库,

  2. 将字体嵌入到我的 HTML 文档的<head> 中,链接样式表,还将 URL 中的“github.com”更改为“raw.githubusercontent.com”,如下所示:

<link href="https://raw.githubusercontent.com/h-ibaldo/Raleway_Fixed_Numerals/master/css/rawline.css" rel="stylesheet">

  1. 然后我使用 CSS 规则来指定字体的系列、粗细和样式,

例子:

font-family: 'rawline', sans-serif; 
font-weight: 100;
font-style: italic;

效果很好。

  1. 我还尝试使用 @import 将其导入另一个样式表,如下所示:

    @import 'https://raw.githubusercontent.com/h-ibaldo/Raleway_Fixed_Numerals/master/css/rawline.css';

它也有效。

这是最终的 GitHub 项目,如果您想查看它:
https://github.com/h-ibaldo/Raleway_Fixed_Numerals


我刚刚做了一个Raleway系列的webfont版本,其中所有的文字数字都被字体的衬里数字所取代,以一劳永逸地解决使用Raleway的衬里数字的问题。

我还为它准备了 CSS 样式表,它很好地解决了我的问题。知道这是其他人的问题,我将通过 GitHub 提供字体文件和 CSS。

我的问题是:我如何使用 GitHub 来托管 webfont,以便人们打算使用不需要托管字体文件,而是导入它,例如 Google Fonts @import 选项,例如:

@import 'https://fonts.googleapis.com/css?family=Raleway:400,700,900';

或者像 Fontawesome 用脚本做的那样:

<script src="https://use.fontawesome.com/28e4d6013b.js"></script>

有什么想法吗?

【问题讨论】:

    标签: github fonts webfonts truetype google-webfonts


    【解决方案1】:

    使用 GitHub,您可以使用文件 url 中的域“raw.githubusercontent.com”(而不是 github.com)以原始格式显示源代码。

    所以,https://github.com/USERNAME/REPOSITORY/blob/master/FILE 变为 https://raw.githubusercontent.com/USERNAME/REPOSITORY/blob/master/FILE

    但是,GitHub 将使用 PLAIN TEXT mime 类型提供文件,这可能会使 CSS 导入不起作用。

    我能想到的唯一解决方案是使用像Raw Git 这样的第三方服务来实现真正的CDN 行为。

    【讨论】:

      猜你喜欢
      • 2011-10-30
      • 2020-04-06
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 2022-08-13
      • 2018-01-20
      • 2018-05-13
      相关资源
      最近更新 更多