2016-11-11 305 views
2

所以我看着這個叫的RobotoCSS中使用相同的谷歌字體的不同字體粗細

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

,但沒有一次成功。誰能幫忙?

回答

2

使用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; 
} 
+0

那麼,你說我只需要'正規'? – Emily7687687

+0

不需要。您必須導入您要使用的所有類型的字體(普通,斜體,粗體,斜體粗體...),然後通過使用'font-weight'屬性指定在css中使用哪一個。請注意,使用'font-weight'可以設置文本的粗細,並且可以使用'font-style'將其設置爲斜體或正常 –

+1

啊我明白了。謝謝 – Emily7687687

0

我的建議是有一個類定義的字體導入谷歌的字體後使用 即在css add:

@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> 
+1

我唯一的問題是,把字體重量:300與字體重量:400和字體重量:500等是一樣的。除了'粗體',它只有1個重量。我似乎無法得到這些數字的工作,我不知道爲什麼。 – Emily7687687

+0

檢查這些您必須確保 – Ojanti

+0

@ user6950100,字體重量:300和字體重量:300是不一樣的,正如我在上面的答案的小提琴中所示。 檢查這些 - 檢查您是否在Google字體網址中添加了所有字體重量並確保其有效 - 檢查您的代碼以確定其他樣式規則是否覆蓋您想要的類或元素的字體重量風格 如果你有一個鏈接,我們可以檢查,這將是偉大的 – Ojanti