The Wayback Machine - https://web.archive.org/web/20250116095211/https://www.geeksforgeeks.org/css-font-family-property/
Open In App

CSS font-family Property

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

The CSS font-family property specifies the font for an element. It can list multiple fonts as fallbacks. If the browser doesn’t support the first font, it tries the next. It includes specific font names like “Arial” and generic families like “serif” or “sans-serif”.

Types of Font Family

  • family-name: Specific names of font families, such as “Times”, “Courier”, “Arial”, etc.
  • generic-family: Names of generic font families, such as “serif”, “sans-serif”, “cursive”, “fantasy”, “monospace”

Syntax

element_selector {
font-family: family-name|generic-family|initial|inherit;
}

Property values

PropertyDescription
fonts-nameSpecifies the name of the font in quotes, separated by commas.
generic-familySets the font of text in an HTML document from the list of available fonts from the font pool.
initialSets an element’s CSS property to its default value.
inheritInherits a property to an element from its parent element’s property value.

Notes :

  • The family-name should be enclosed in quotes when it contains white space or special characters.
  • When using the style attribute in HTML, the font name should be enclosed in single quotes.

We will understand the usage of the font-family property by implementing it.

Example 1: Basic Usage of font-family

In this example we demonstrates the use of the font-family CSS property to style paragraphs (<p>). It applies different fonts such as “Impact”, “Arial”, with fallbacks for cross-platform consistency in web typography.

HTML
<!DOCTYPE html>
<html>

<head>
    <title> CSS font-family Property </title>
    <style>
        .para1 {
            font-family: "Impact", Times, serif;
        }

        .para2 {
            font-family: Arial, Helvetica, sans-serif;
        }
    </style>
</head>

<body>
    <h1>Font-family Property</h1>
    <p class="para1">
        GeeksforGeeks in Impact font
    </p>

    <p class="para2">
        GeeksforGeeks in Arial font.
    </p>

</body>

</html>

Output:

Example 2: Multiple Font-Family Values

In this example we demonstrates the use of the font-family property to style text with different fonts: Times New Roman, Arial, and Courier New.

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Font Family Example</title>
    <style>
        .example1 {
            font-family: "Times New Roman", Times, serif;
        }

        .example2 {
            font-family: "Arial", Helvetica, sans-serif;
        }

        .example3 {
            font-family: "Courier New", Courier, monospace;
        }
    </style>
</head>

<body>
    <h1 class="example1">
        This is a heading with Times New Roman
    </h1>
    <p class="example1">
        This is a paragraph with Times New Roman.
    </p>

    <h1 class="example2">
        This is a heading with Arial
    </h1>
    <p class="example2">
        This is a paragraph with Arial.
    </p>

    <h1 class="example3">
        This is a heading with Courier New
    </h1>
    <p class="example3">
        This is a paragraph with Courier New.
    </p>
</body>

</html>

Output:

CSS-font-family-Property

CSS font-family Property Example Output

Conclusion

The font-family property is a powerful CSS feature for specifying fonts in web design. It allows for a fallback system, ensuring that if one font is not available, another can be used. By understanding and utilizing this property, you can enhance the typography and overall appearance of your web pages.

Supported Browsers

The browsers that support the font-family property, are listed below:

CSS font-family Property – FAQs

What does the font-family property do in CSS?

The font-family property in CSS specifies a prioritized list of font family names for an element. It defines which fonts should be used to render text, allowing fallback fonts if the primary font isn’t available.

How do I specify multiple font families in CSS?

You can specify multiple font families by listing them in order of preference, separated by commas. For example: font-family: “Open Sans”, Arial, sans-serif;. The browser will use the first available font.

What is a generic font family in CSS?

A generic font family is a broad category of fonts that includes serif, sans-serif, monospace, cursive, fantasy, and system-ui. These generic families are used as fallbacks if none of the specified fonts are available.

Should I include both specific and generic font families?

Yes, it’s a good practice to include a generic font family as the last option in your font-family list. For example: font-family: “Roboto”, “Helvetica”, sans-serif; ensures a sans-serif font is used if the specific fonts aren’t available.

Can I use custom fonts with font-family?

Yes, you can use custom fonts by first importing them using @font-face or @import, then specifying them in the font-family property. Make sure to include a fallback font for better compatibility.


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

Similar Reads

three90RightbarBannerImg