【问题标题】:Google font locally with different weights but with the same nameGoogle 字体在本地具有不同的权重但具有相同的名称
【发布时间】:2019-09-03 12:01:13
【问题描述】:

这是我的问题:

为了提高我在谷歌速度洞察力上的 SPEED 标记,我不得不从阻止渲染的谷歌字体切换到加载本地谷歌字体。

到目前为止一切顺利,只是我遇到了一个大问题。

在我以这种方式加载字体之前:

<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700|Open+Sans+Condensed:300|Ubuntu+Condensed|Ubuntu:300,700" rel="stylesheet">

在我巨大的样式表中,我只是正常调用它们,例如:

body {
  font-family: 'Open Sans', sans-serif;
  font-weight: 400;
}

但是现在,由于我必须下载它们以避免渲染阻塞红旗,所以我这样称呼它们:

@font-face {
    font-family: "Opens Sans";
    src: url("/fonts/OpenSans-Regular.ttf");
    font-style: normal;
    font-weight: 400;
}
@font-face {
    font-family: "Opens Sans";
    src: url("/fonts/OpenSans-Light.ttf");
    font-weight: 300;
}
@font-face {
    font-family: "Opens Sans";
    src: url("/fonts/OpenSans-Bold.ttf");
    font-weight: 600;
}
@font-face {
    font-family: "Opens Sans";
    src: url("/fonts/OpenSans-ExtraBold.ttf");
    font-weight: 700;
}
@font-face {
    font-family: "Opens Sans Condensed";
    src: url("/fonts/OpenSansCondensed-Light.ttf");
    font-weight: 300;
}
@font-face {
    font-family: "Ubuntu Condensed";
    src: url("/fonts/UbuntuCondensed-Regular.ttf");
    font-weight: 300;
}
@font-face {
    font-family: "Ubuntu";
    src: url("/fonts/Ubuntu-Regular.ttf");
    font-weight: 300;
}
@font-face {
    font-family: "Ubuntu";
    src: url("/fonts/Ubuntu-Bold.ttf");
    font-weight: 700;
}

在这里你可以看到我的问题。

我用相同的名称称呼不同的字体,但它们的权重不同。显然,我可以用不同的名称来称呼它们,例如“Ubuntu Bold”,但是我必须更改我的整个样式表,例如,我现在应该声明:

body {
      font-family: 'Open Sans Normal', sans-serif;
      // font-weight: 400; //
}
p {
      font-family: 'Open Sans Bold', sans-serif;
      // font-weight: 700; //
}

本质上,没有更多的字体粗细,只有不同的字体系列名称来说明粗细。

我的问题有什么解决办法吗?

【问题讨论】:

    标签: css font-face font-family google-fonts


    【解决方案1】:

    即使字体文件不同,您也无需为每种字体变体设置不同的font-family。您可以只设置一个font-family,并在不同的@font-face 属性中指定不同的变体。

    您应该使用相同的font-family 名称定义每个@font-face,如下所示:

    @font-face {
        font-family: 'Opens Sans';
        src: url("/fonts/OpenSans-Regular.ttf");
        font-style: normal;
        font-weight: 400;
    }
    @font-face {
        font-family: 'Opens Sans';
        src: url("/fonts/OpenSans-Italic.ttf");
        font-style: italic;
        font-weight: 400;
    }
    @font-face {
        font-family: 'Opens Sans';
        src: url("/fonts/OpenSans-Bold.ttf");
        font-style: normal;
        font-weight: 600;
    }
    

    请注意,每个不同的字体文件都有一个单独的@font-face,具有与特定字体文件对应的不同属性,但它们具有相同的font-family。然后你可以像这样在你的css中使用字体:

    body {
        font-family: 'Open Sans', sans-serif;
        font-weight: 400;
    }
    .bold {
        font-family: 'Open Sans', sans-serif;
        font-weight: 600;
    }
    .italic {
        font-family: 'Open Sans', sans-serif;
        font-style: italic;
    }
    

    css 类中的其他属性(font-weightfont-style 等)将决定使用哪个 @font-face

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      相关资源
      最近更新 更多