How to change fonts
Goal
- โก In this step, we'll learn how to change the font on our website.
font-family
To change the font of the whole website, you can change the font-family
of the body
as follows.
Let's try it in Chrome Dev Tools.
You see that the font has been changed.
Tangerine
font
Now, let's try the Tangerine
font. The Tangerine
font looks like the following image.
Now, let's try the following CSS in Chrome Dev Tools as before.
The font has changed, but this is not the Tangerine
font, but the cursive
of the fallback font has been applied.
caution
The reason for this is that the Tangerine
font is not installed in the chrome browser by default.
*I will explain about fallback font
later.
So how do you use a font that is not installed in your browser by default?
=> The answer is to use Google Fonts. ๐ธ
Google Fonts
If you want to use a particular font on your website, it is better to use Google Fonts.
Using Google Fonts
Now let's take a look at how to use Google Fonts.
- First, go to https://fonts.google.com/
- Next, search for the name of the font you want to use. This time, do a search for
Tangerine
. - Next, select the font weight you want to use. Let's choose all this time.
- Please copy Embed
<link>
at the end.
Paste copied embeded <link>
into the <head>
tag as follows.
Now, let's check the following CSS in Chrome Dev Tools again.
This time, thanks to loading Google Fonts, you'll see that the Tangerine
font was applied correctly.
About Fallback Fonts
A fallback font is a font that is applied when the specified font is unavailable for some reason.
Start with the font you want to use and then multiple fallback fonts as follows.
'Tangerine'
is missing => 'Oswald'
is missing => ... cursive
.
The last cursive
is the name of the font type, not the font name, and the browser will automatically select and apply the installed font of this type.
Besides cursive
, there are also serif
, sans-serif
, fantasy
, and monospace
, but it is sufficient to use CSS shown in Google Fonts.
See: https://www.w3schools.com/cssref/pr_font_font-family.asp
Poppins font ๐
We use the "Poppins" font on our website.
Add a Poppins font.
Now, let's add the Poppins font by clicking on the URL below.
https://fonts.google.com/specimen/Poppins
In the meantime, please add 300`, 400`, 500`, and 700` fonts.
*Note: Different weights and italics can be added later, as needed and appropriate. In my case, I first add 300
, 400
, 500
, and 700
and adjust as needed.
Now, copy and paste the embed link into the head tag.
Next, let's change the font-family
of the body to the Poppins font.
Q. Why do I need to choose font-weight
and italic
?
A. This is to improve the loading speed of the site by reducing the size of the font file to be loaded.
See: https://www.smashingmagazine.com/2019/06/optimizing-google-fonts-performance/
Final Code
The final code looks like this
Refs.
font-family
:
- https://www.w3schools.com/cssref/pr_font_font-family.asp
- https://developer.mozilla.org/en-US/docs/Web/CSS/font-family
Google fonts: