add google fonts to css
Code snippet for how to add google fonts to css with sample and detail explanation
Title: Adding Google Fonts to CSS
In this quick guide, we will discuss how to integrate Google Fonts into your CSS code. This handy feature can help web designers optimize the aesthetics of their sites by providing them with a vast array of downloadable font styles.
Importing Google Fonts to CSS
Firstly, you need to locate the exact font style you need from Google Fonts. Once you have found your desired design, the following line of code will allow you to import the Google Font straight into your CSS:
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
Please replace ‘Roboto’ with your selected font.
Then, use it within your CSS like this:
body {
font-family: 'Roboto', sans-serif;
}
Again, replace ‘Roboto’ with your chosen font.
Code Explanation for Importing Google Fonts
The @import
rule is used to import style rules from other style sheets. In this case, it is being used to pull in the styles directly from Google Fonts. The url
segment of the code represents the specific location from whence the styles are being imported.
We are importing a font named “Roboto”, although you can replace “Roboto” with the specific style you have chosen from Google Fonts.
Following the Google Fonts URL 'https://fonts.googleapis.com/css2?family=Roboto&display=swap'
, the &display=swap
part is a feature that ensures text remains visible to the user while the custom font is loading. This helps in minimizing flashes of unstyled text.
In the second part of the code, we apply the font to the body element of the webpage. The font-family
property is used to specify the font of the text. ‘Roboto’ is the name of the font, and sans-serif is the generic font family being used as a fallback in case ‘Roboto’ can’t be loaded.
So, by incorporating these simple lines of code, you can make your website more attractive and visually appealing by utilizing the range of font styles offered by Google Fonts.