ChatGPT解决这个技术问题 Extra ChatGPT

Specifying Style and Weight for Google Fonts

I am using Google fonts in a few of my pages and hit a wall when trying to use variations of a font. Example: http://www.google.com/webfonts#QuickUsePlace:quickUse/Family:Open+Sans

I am importing three faces, Normal, Bold, ExtraBold via the link tag. The normal face displays correctly, but I cannot figure out how to use the variants of the font in my CSS

I tried all of the following as attributes for font-family but no dice:

'Open Sans Bold'

'Open Sans 700'

'Open Sans Bold 700'

'Open Sans:Bold'

The google docs themselves do not offer much help. Anyone have an idea of how I should write my CSS rules to display these variants?


C
Charming Robot

They use regular CSS.

Just use your regular font family like this:

font-family: 'Open Sans', sans-serif;

Now you decide what "weight" the font should have by adding

for semi-bold

font-weight:600;

for bold (700)

font-weight:bold;

for extra bold (800)

font-weight:800;

Like this its fallback proof, so if the google font should "fail" your backup font Arial/Helvetica(Sans-serif) use the same weight as the google font.

Pretty smart :-)

Note that the different font weights have to be specifically imported via the link tag url (family query param of the google font url) in the header.

For example the following link will include both weights 400 and 700:

<link href='fonts.googleapis.com/css?family=Comfortaa:400,700'; rel='stylesheet' type='text/css'>

For CSS2

<link href='fonts.googleapis.com/css2?family=Comfortaa:wght@400;700'; rel='stylesheet' type='text/css'>

this is partial truth, and is because if you dont assing the weight on the link css google wont download the proper "bold" format to achieve this you have to declare the "link href" as follow: <link href='fonts.googleapis.com/css?family=Comfortaa:400,700' rel='stylesheet' type='text/css'>
Is there any way to make the browser use weight 600 for old my old rules of "bold"? I think the 700 is too thick and don't want it anywhere on my site.
What do you mean? I guess you should just change the bold to being 600?. Do you mean changing "bold" behaviour on <strong> and <b>?
I am using @import url(fonts.googleapis.com/css?family=Open+Sans:400,300); as i cant access the html, and when i try and use the following... font-family: 'Open Sans', sans-serif; font-weight: 300; it doesnt change the font weight. ideas?
So then if I can use normal CSS for this, why does Google let me decide which weight I want to use?
E
Ely

Here's the issue: You can't specify font weights that don't exist in the font set from Google. Click on the SEE SPECIMEN link below the font, then scroll down to the STYLES section. There you'll see each of the "styles" available for that particular font. Sadly Google doesn't list the CSS font weights for each style. Here's how the names map to CSS font weight numbers:

Thin            100     
Extra Light     200
Light           300
Regular         400
Medium          500
Semi-Bold       600
Bold            700
Extra-Bold      800
Black           900

Note that very few fonts come in all 9 weights.


P
Pramesh Bajracharya

font-family:'Open Sans' , sans-serif;

For light: font-weight : 100; Or font-weight : lighter;

For normal: font-weight : 500; Or font-weight : normal;

For bold: font-weight : 700; Or font-weight : bold;

For more bolder: font-weight : 900; Or font-weight : bolder;


Great. But can the font be lighter than font-weight : 100 ?
No, minimum value is 100 only and maximum value is 900.
This is not correct, the following values are to consider: light: font-weight: 300; normal: font-weight: 400; semi-bold: font-weight: 600; bold: font-weight: 700; extra bold: font-weight: 800;
L
Laurent

The API has been updated, now you have to import the desired font-weight using ":wght@700"

Ex: "https://fonts.googleapis.com/css2?family=Inter:wght@700&display=swap"

source: https://developers.google.com/fonts/docs/css2


R
Ritam Das

you can use the weight value specified in the Google Fonts.

body{
 font-family: 'Heebo', sans-serif;
 font-weight: 100;
}

c
coreyl

In case anyone is looking for latest format with multiple font families + weights, see below (ex. w/ Open Sans + Roboto)

https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,600;0,700;0,800;1,800&family=Roboto:wght@500&display=swap

Note that the latest version of google fonts web interface has bad UX for displaying the code sample. There is a 'selected families' panel which displays and then disappears, and it overlaps the font variations 'add' buttons (the plus / minus signs), which are also located on the right. The solution, which is not intuitive, is to reload the page.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now