【问题标题】:using different font weight of the same google font in css在css中使用相同谷歌字体的不同字体粗细
【发布时间】:2016-11-11 18:01:09
【问题描述】:

所以我正在研究这种名为 Roboto 的 Google 字体

https://fonts.google.com/specimen/Roboto?selection.family=Roboto

它有不同的风格,例如轻、常规、中等。

网站说导入你可以做

@import url('https://fonts.googleapis.com/css?family=Roboto');

然后在你的样式表中你可以这样做:

font-family: 'Roboto', sans-serif;

嗯,那很好,但我需要它们的混合。比如轻巧,规律,不只是一个。

这样我就可以了

@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,500');

这会将它们全部选中。

但它仍然在样式表中说你可以这样做:

font-family: 'Roboto', sans-serif

如果你这样做,那么它只会坚持第一个。我需要一种样式为300,一种为400,一种为500。那么如何在css中指定哪一种呢?

我试过了

font-family: 'Roboto:300', sans-serif

font-family: 'Roboto 300', sans-serif

font-family: 'Roboto-300', sans-serif

但他们都没有工作。有人可以帮忙吗?

【问题讨论】:

    标签: css fonts


    【解决方案1】:

    使用font-weight 属性

    http://www.w3schools.com/cssref/pr_font_weight.asp

    例子:

    p.normal {
        font-weight: normal;
    }
    
    p.thick {
        font-weight: bold;
    }
    
    p.thicker {
        font-weight: 900;
    }
    

    【讨论】:

    • 所以你是说我只需要'常规'?
    • 没有。您必须导入所有要使用的字体类型(普通、斜体、粗体、斜体-粗体...),然后使用 font-weight 属性指定要在 css 中使用的字体。请注意,font-weight 可以设置文本的细度,font-style 可以将其设置为斜体或正常
    • 啊,我明白了。谢谢
    • 由于某种原因,似乎只有 2 个状态。正常和大胆。 900 与粗体相同。但 300 和 400 之间似乎没有区别。有什么想法吗?
    【解决方案2】:

    我建议有一个定义要使用的字体的类 即在导入 google 字体后,在你的 css 中添加:

    @import url('https://fonts.googleapis.com/css?family=Roboto:300,400,600');
    
    .robotoriser{
    font-family: 'Roboto', sans-serif;
    font-weight: Normal; /*  This is just so when ever you call use this class, it uses the normal font weight by default */
    }
    
    .rbt-300{ /* you can also name this rbt-light or whatever you like*/
    font-weight:300;
    }
    
    .rbt-400{
    font-weight:400;
    }
    
    .rbt-600{
    font-weight:600;
    }
    

    ...等等。

    然后像这样在html中使用

    <p class="robotoriser rbt-300" >This is light text</p>
    
    <p class="robotoriser rbt-400" >This is medium text</p>
    
    <p class="robotoriser rbt-600" >This is heavy text</p>
    

    这是一个工作演示的fiddle

    请注意,您也可以在任何课程中使用它 例如

    .some_element{
    font-family: 'Roboto', sans-serif;
    font-weight: 300; /* Or any of the font weight values you included via the Google font url  */
    }
    
    <p class="some_element"> Dragons are goats that can fly :D </p>
    

    【讨论】:

    • 检查这些你必须确保
    • @user6950100 , font-weight:300 和 font-weight:300 与我上面的答案中所示的不同。检查这些 - 检查您是否在 Google 字体 URL 中添加了所有字体粗细并确保其有效 - 检查您的代码以找出另一个样式规则是否没有覆盖您想要样式的类或元素的字体粗细 如果您有一个我们可以检查的链接,那会很棒
    猜你喜欢
    • 2018-06-26
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2015-09-24
    • 2015-11-14
    • 2019-06-18
    • 2014-12-16
    • 2016-11-03
    相关资源
    最近更新 更多