How to Change the Font with font-family in CSS
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
<selector> {
font-family: <font>, <fallback font>, <generic family>;
}
| Part | What 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:

<!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 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
| Mistake | What happens | Do 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 default | Write generic families without quotes |
| Listing only one font | If it isn't installed, the browser uses its default font: a list holding only a font that didn't exist came out in Times | End the list with a generic family |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The page shows another font than the first in the list | That font isn't installed: with NoSuchFont, Georgia, serif, Chromium used Georgia | List fallbacks you're happy with |
| The text is in Times | No font in the list matched, or the generic family is quoted | Remove 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
- font-family, on MDN.
- generic-family, on MDN.
