Skip to main content

How to Change the Font with font-family in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

font-family sets the font. Give it a list: your first choice, fallbacks in case it isn't installed, and a generic family such as sans-serif at the end, which always works.

Syntax​

Syntax
<selector> {
font-family: <font>, <fallback font>, <generic family>;
}
PartWhat it does
<font>The font you want, such as Arial. Quote names that contain spaces, such as "Times New Roman".
<fallback font>Used if the font before it isn't installed. List as many as you like.
<generic family>A keyword such as serif, sans-serif or monospace, written without quotes. The browser picks a font of that kind.

MDN's font-family page says generic family names are keywords and must not be quoted, and that a generic family should be the last item in the list.

The Example​

The example is a page of opening hours. The page's text uses Arial, and the heading uses Georgia.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding a rule for body with the font-family Arial, Helvetica, sans-serif and a rule for h1 with the font-family Georgia, serif, and a body with the heading Opening hours and one paragraph.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Font family</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}

h1 {
font-family: Georgia, serif;
}
</style>
</head>
<body>
<h1>Opening hours</h1>
<p>Monday to Friday, 9:00 to 17:00.</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: the heading Opening hours in Georgia, a serif font, and the paragraph Monday to Friday, 9:00 to 17:00. in Arial, a sans-serif font.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The paragraph is in Arial. It got the font list from body, and Arial, the first choice, is installed.
  • The heading is in Georgia. Its own rule replaced the list it would get from body.

Chromium reported the fonts it really drew with: Georgia for the heading and Arial for the paragraph.

Common Mistakes​

MistakeWhat happensDo this instead
Quoting a generic family: "sans-serif"It's read as a font named sans-serif, which doesn't exist, so the text came out in Times, Chromium's defaultWrite generic families without quotes
Listing only one fontIf it isn't installed, the browser uses its default font: a list holding only a font that didn't exist came out in TimesEnd the list with a generic family

Troubleshooting​

SymptomLikely causeFix
The page shows another font than the first in the listThat font isn't installed: with NoSuchFont, Georgia, serif, Chromium used GeorgiaList fallbacks you're happy with
The text is in TimesNo font in the list matched, or the generic family is quotedRemove the quotes, and end with a generic family

FAQ​

Do font names with spaces need quotes?​

Times New Roman worked without quotes, but MDN calls it good practice to quote font names that contain spaces, digits or punctuation other than hyphens, so write "Times New Roman".

Which font does sans-serif give?​

Whatever the browser chooses. Chromium on macOS used Helvetica for sans-serif, and Times for a page with no font-family at all.

Conclusion​

Give font-family a list that ends with an unquoted generic family. The browser uses the first font it has, so the page always gets a font of the kind you asked for.

Additional Resources​