The Wayback Machine - https://web.archive.org/web/20250114213334/https://www.geeksforgeeks.org/how-to-add-font-in-css/
Open In App

How To Add Font In CSS?

Last Updated : 09 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Adding fonts to a website enhances its design and readability. In CSS, custom fonts can be applied using several techniques that allow web designers to include specific fonts that aren’t system defaults.

Prerequisites

These are the approaches to add a font in CSS:

Using web-safe fonts

In this approach we will use the web-safe fonts. They are predefined fonts that are universally available across most systems and browsers, such as Arial, Times New Roman, and Courier. These fonts do not require any external resource links.

Example: In below example we are adding the fonts in CSS using web-safe fonts. The font-family which will be used here is Georgia.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
     content="width=device-width,
      initial-scale=1.0">
    <title>Web-Safe Fonts Example

    </title>
    <style>
        body {
            font-family: 'Georgia',
             serif;
        }

        h1 {
            font-family: 'Arial',
             sans-serif;
            color: green;
        }
    </style>
</head>

<body>
    <h1>Welcome to GeeksForGeeks</h1>
    <p>This paragraph uses the Georgia
        font, and the heading uses Arial.</p>
</body>

</html>

Output:

a1
Output using web-safe fonts

Importing fonts from Google Fonts or other CDN-based font providers

In this approach we will use Google Fonts which is a free library that provides access to various fonts which can be easily included in your website.

Example: In this example we are importing the google fonts to add font in CSS.The font family of the same is Roboto.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport"
     content="width=device-width,
      initial-scale=1.0" />
    <title>Google Fonts Example</title>
    <style>
        @import
        url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap");

        body {
            font-family: "Roboto", sans-serif;
        }

        h1 {
            font-family: "Lobster", cursive;
            color: green;
        }
    </style>
</head>

<body>
    <h1>Welcome to GeeksForGeeks</h1>
    <p>
        This paragraph uses Roboto, and the
         heading uses Lobster from Google
        Fonts.
    </p>
</body>

</html>

Output:

a1
Output using Implementing fonts from google forms


Dive deep into the world of JavaScript with our JavaScript Full Course Online, offering everything from the basics to advanced techniques. Learn how to build interactive websites, optimize your code, and earn a certification to showcase your skills.

Take the Three 90 Challenge! Complete 90% of the course in 90 days, and earn a 90% refund. Push your limits and stay motivated as you work towards mastering JavaScript. Join now and get started on your JavaScript learning journey!


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg