【问题标题】:Is this the right way to set custom font?这是设置自定义字体的正确方法吗?
【发布时间】:2021-10-18 05:20:23
【问题描述】:

我有一个字体

/static/font/input_regular.ttf

我想用于code 标签,我的 styles.css 中有这个:

@font-face {
  font-family: "Input_reg";
  src: url("/font/input_regular.ttf") format("truetype");
}

pre, code {
  font-family: "Input_reg", monospace;
  font-size: inherit;
}

网站的其余部分使用 Source Code Pro 字体。 问题是,它不起作用,并且永远不会加载这种新字体,而是像页面的其余部分一样只使用 Source Code Pro。我在这里做错了什么? 如果重要的话,该网站正在使用 hugo,并且托管在 github 页面上。

【问题讨论】:

  • 或多或少,你可以使用webfont生成器,例如:transfonter.org,这将为你生成正确的字体css。
  • 如果您有答案,请将其发布在答案部分并在 2 天内将其标记为正确

标签: html css github-pages hugo


【解决方案1】:

你试过了吗

@font-face {
  font-family: "input_reg";
  src: url("font/input_regular.ttf");
}

网址中没有尾随的 /。此外,您不需要指定这是一个 trueType 并且全部小写总是对于互联网上的几乎任何东西都是一个好主意:-) 如果这没有帮助,请在浏览器中按 F12,重新加载页面并查看从哪个 URL 加载字体 - 这可能是错误的。

【讨论】:

    【解决方案2】:

    提供最深入的浏览器支持。

    @font-face {
      font-family: 'Input_reg';
      src: url('/static/font/input_regular.eot'); /* IE9 Compat Modes */
      src: url('/static/font/input_regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
           url('/static/font/input_regular.woff2') format('woff2'), /* Super Modern Browsers */
           url('/static/font/input_regular.woff') format('woff'), /* Pretty Modern Browsers */
           url('/static/font/input_regular.ttf')  format('truetype'), /* Safari, Android, iOS */
           url('/static/font/input_regular.svg#svgFontName') format('svg'); /* Legacy iOS */
    }
    
    pre, code {
      font-family: "Input_reg", monospace;
      font-size: inherit;
    }

    【讨论】:

    • 他的问题似乎不是浏览器支持 - 他已经在使用 Source Code Pro,所以我猜这是路径问题。因此,如果它不起作用,如我的第一个答案中所述:尝试在打开 F12 - 网络选项卡的同时加载页面 - 查看 efonbt 是否正确加载或者是否在字体上获得 404。如果确实给出了 404,请右键单击 - 复制 - url,然后查看它尝试从哪里加载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2013-02-13
    • 2022-12-28
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多