×

How to use google fonts in CSS

Fonts: -

Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in HTML and CSS; however, if we don't like the normal HTML/CSS fonts and want something new, fresh, and current, we can utilize Google Fonts. 

Google Fonts: -

Google Fonts is a Google online library that allows users to use several font families via a CDN (content delivery network) that is open to everyone.

The typeface we choose is an important aspect of any design. Google Fonts is a free online fonts service that lets users utilize a broad array of typefaces in our CSS file.

In 2010, Google Fonts was started as an engineering endeavour to advance the web and make it quicker.

Personal and commercial use of Google Fonts is both free. Thanks to the Google Fonts website, anyone may instantly select and use multiple fonts for their personal design needs.

Everyone uses Google Font, but mostly it is used by Graphic designers, Ui/UX designers, developers, web designers, bloggers, etc. Google Fonts can be used on wedding invitation cards, websites, posters, and books, these are a few, but its usage is a lot more.

Font creators, type foundries, and the design community from all over the world contribute with Google Fonts. These people and organizations create the fonts that we see on Google Fonts.

We should use Google Fonts because 

  • They are easy to implement.
  • There are more than 900 fonts from which we can choose to use for the developing website or anything.
  • The overall level of the fonts is improving.
  • Google Fonts can also be downloaded for usage in print.
  • The Google Fonts API enables using web-fonts to be easier and faster for everyone. The Google fonts have already been tested in a variety of browser settings.

We only need to add custom cascading style sheets (CSS) linked to our HTML document, which then references the font family of the chosen in the CSS style.

How to use Google Fonts?

We can use Google Fonts while developing the website or webpage in two ways.

The first way is by adding it to the <link> tag in the HTML file, and the second way is by using the @import rule. We will study both ways with the help of an example.

1)Adding to <link>tag in HTML document:-

How to use google fonts in CSS?

First, go to Google font's official website and select the font of our choice.

Then click on "select this style" and copy the link of the style we have selected.

And now paste that link in the <link> tag of the HTML document.

Syntax: -

<link rel = "stylesheet" href = "https://fonts.googleapis.com/css?family=Font+Name>  

Then we can use an external stylesheet or an inline style to apply the specified web font to an element.

Selector(id,class,tag)
{
	Font-family: ‘Name_of_Font’,’Name_of_font_family’;
}

Example: -

<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css2?family=Caveat&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2 family=Cantarell&family=Caveat&family=Great+Vibes&family=Luckiest+Guy&family=Tapestry&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cantarell&family=Caveat&family=Creepster&family=Great+Vibes&family=Luckiest+Guy&family=Rock+Salt&family=Tapestry&display=swap" rel="stylesheet">
<style>
h1{
  font-family: 'Caveat', cursive;
  font-size: 40px;
}
p
{
	font-family: 'Cantarell', sans-serif;
}
h2
{
	font-family: 'Rock Salt', cursive;
}
</style>
</head>
<body>
<h1>Using Google Fonts-Here we have used Caveat Font of Cursive Font-Family</h1>
<p>Here we have used Cantarell Font of sans-serif Font-Family</p>
<h2>Here we have used Rock Salt Font--6287287w92</h2>
</body>
</html>

Output: -

How to use google fonts in CSS?

So, in the above example, we have used multiple Google fonts by using it in the <link> tag and have applied to each selector or tag different Google Fonts to make it look different and understand how to use them.

2) Using import rule: -

We use the same ways here as we did for the <link> tag. Also, we can do, but instead of using the <link>tag here, we will be using the @import rule.

So firstly, go to Google Fonts, select the font of choice, and import it in the <style> tag.

Syntax: -

@import url(‘https://fonts.googleapis.com/css?family=Font_Family_name’)

Example: -

<html>
<head>
<style>
@import url('https://fonts.googleapis.com/css2?family=Lobster+Two&display=swap');
h2{
color: blue;
}
 h1{
color: green;}
#Ex{
font-family: 'Lobster Two', cursive;
font-size: 30px;}
p{
color: purple;}
</style></head>
<body>
<center>
<h1> Example of Google Font using import rule </h1>
<div id ="Ex">
<h2> Here font-family is Lobster Two </h2>
<p> Hello This is an example of Google font using import here we have used lobster font of Cursive font family. </p>
</div>
</center>
</body>
</html>

Output: -

How to use google fonts in CSS?

In the above example, we have used only one font using @import, but as we did in the first example, likewise for import, also we can use multiple @import for styling purposes.


Related Topics

CSS Pagination

CSS Pagination The pagination in CSS is an advantageous approach for indexing of a website’s distinct pages over the homepage. When our site contains multiple pages, we must insert a few...

13 minutes read.

CSS Text Effects

Text Effects in CSS We can use distinct text effects used inside the HTML document. Few attributes can be applied to insert the text effects. We can also style various web documents...

5 minutes read.

CSS Math Functions

The CSS math functions permit numerical articulations. These CSS math functions can particularly be utilized in maybe unforeseen ways, for example, inside angles and shading the functions and in the...

3 minutes read.

CSS calc()

CSS calc(): The CSS calc() function is a CSS function of inbuilt type, which permits us to implement calculations. It is used for calculating the angle, integer frequency, number, time,...

3 minutes read.

CSS Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 minutes read.

CSS Browser Support

The desk underneath lists of all CSS properties and the way each belongs to it is supported in the generally unique browsers: - The quantity to the right of the browser...

2 minutes read.

CSS Position

CSS Position: The position property in CSS is applied to set the position of the elements. It can be used to position the backside of an element. It is also...

3 minutes read.

CSS Transition

CSS Transition The transitions in CSS are influenced, included to modify the component from one design to another, without the use of the JavaScript or Flash. We should describe two of the...

2 minutes read.

CSS Functions

CSS functions are used for different CSS properties. Every front-end developer should have knowledge of all CSS functions. It makes the coding easier. FunctionsExplanationattr().It returns the value of an attribute of...

5 minutes read.

CSS z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

CSS Superscript and Subscript

Superscript and Subscript To specify the superscript and the subscript text, HTML provides us two tags that are <sup> and <sub>. The superscript content looks in the shorter font and half...

2 minutes read.

How to use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 minutes read.

CSS Word Wrap

Word Wrap in CSS: The word-wrap property in CSS is referred to break any lengthy word and wrap towards the next or following line. It is employed for preventing overflow...

1 minute read.

CSS Form

You can create attractive HTML form by using CSS. Style input Fields The Input Field width properties are used to determine the width of the input field. Let us see an example of CSS style...

2 minutes read.

CSS Page-break-before Attribute

Page-break-before Attribute As its name implies, the page-break-before property is applied to include page break before any element, at the time of printing any document. It adds the page break before...

5 minutes read.

How to Set Space between the Flexbox

For managing layout on the Web, CSS Flexbox and CSS Grid are two excellent tools. Flexbox handles one-dimensional layouts quite well, whereas CSS Grid manages two-dimensional layouts with columns and...

4 minutes read.

CSS Order

CSS Order CSS order property in CSS describes the flex item’s order inside the flex or any grid container. It is applied to order any flex item. Note: If any component is...

3 minutes read.

CSS :root selector

CSS :root selector This CSS pseudo-class matches any document’s root element. It chooses the parent of the highest-level inside the document DOM or tree. An element of <html> is always a...

2 minutes read.

CSS Gradient

CSS Gradient: The CSS gradient property is applied to show the smooth transition inside two specified colors or more than two specified colors. Need of CSS Gradient The following are a few...

4 minutes read.

CSS Introduction

CSS stands for Cascading Style Sheet. It is a design language which is used to design the web pages. It was developed by Håkon Wium Lie on October 10, 1994. It provides...

1 minute read.