Font family lets you choose how text looks by picking different styles of letters. It helps make your website easy to read and nice to look at.
Font family in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
font-family: fontName, fallbackFont, genericFamily;Use commas to separate multiple font names.
Always end with a generic family like serif, sans-serif, or monospace as a backup.
font-family: Arial, Helvetica, sans-serif;font-family: 'Times New Roman', Times, serif;
font-family: 'Courier New', Courier, monospace;
font-family: 'Comic Sans MS', cursive, sans-serif;
This page shows how different font families change text style. The heading uses a serif font called Georgia, paragraphs use monospace fonts, and the body uses a clean sans-serif font.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Font Family Example</title> <style> body { font-family: Arial, Helvetica, sans-serif; margin: 2rem; line-height: 1.5; } h1 { font-family: Georgia, serif; color: #2a2a2a; } p { font-family: 'Courier New', Courier, monospace; background-color: #f0f0f0; padding: 1rem; border-radius: 0.5rem; } </style> </head> <body> <h1>Welcome to Font Family Demo</h1> <p>This paragraph uses a monospace font to look like code.</p> <p>The rest of the page uses Arial or similar sans-serif fonts for easy reading.</p> </body> </html>
Put font names with spaces inside quotes, like 'Times New Roman'.
Always list multiple fonts to make sure text looks good if the first font is missing.
Generic families help browsers pick a similar font if none of your listed fonts are available.
Font family controls the style of text by choosing fonts.
List fonts separated by commas, ending with a generic family.
Use quotes for font names with spaces.
Practice
font-family property control on a webpage?Solution
Step 1: Understand the role of
Thefont-familyfont-familyproperty sets which font or fonts the browser uses to display text.Step 2: Differentiate from other text properties
Size, color, and spacing are controlled by other CSS properties likefont-size,color, andletter-spacing.Final Answer:
The style and type of text fonts displayed -> Option AQuick Check:
font-family controls font style = C [OK]
- Confusing font-family with font-size
- Thinking font-family changes text color
- Mixing font-family with letter spacing
Solution
Step 1: Recognize font names with spaces need quotes
Font names with spaces like "Arial Black" must be wrapped in quotes to be read correctly.Step 2: Check syntax correctness
font-family: 'Arial Black', sans-serif; uses quotes and separates fonts with commas, which is correct syntax.Final Answer:
font-family: 'Arial Black', sans-serif; -> Option DQuick Check:
Quotes needed for multi-word fonts = A [OK]
- Omitting quotes for multi-word font names
- Missing commas between font names
- Combining font names without spaces or commas
p { font-family: 'Times New Roman', Georgia, serif; }What font will the browser use if 'Times New Roman' is not available but Georgia is?
Solution
Step 1: Understand font-family fallback order
The browser tries fonts in order: first 'Times New Roman', then Georgia, then generic serif.Step 2: Apply fallback logic
If 'Times New Roman' is missing, the browser uses the next available font, which is Georgia.Final Answer:
Georgia -> Option BQuick Check:
Fallback font used if first missing = B [OK]
- Assuming browser uses generic serif first
- Ignoring font order in the list
- Thinking browser picks random font
h1 { font-family: Arial, 'Helvetica Neue' sans-serif; }Solution
Step 1: Check font list syntax
Font names must be separated by commas. Here, there's no comma between 'Helvetica Neue' and sans-serif.Step 2: Confirm quotes and other syntax
Quotes around 'Helvetica Neue' are correct; Arial does not need quotes; multiple fonts are allowed.Final Answer:
Missing comma between 'Helvetica Neue' and sans-serif -> Option AQuick Check:
Font names must be comma-separated = A [OK]
- Forgetting commas between font names
- Misusing quotes around single-word fonts
- Thinking font-family limits number of fonts
Solution
Step 1: Use quotes for multi-word font names
'Open Sans' has a space, so it must be in quotes.Step 2: Separate fonts with commas and use generic family without quotes
Fonts must be comma-separated. Arial is a single word and does not need quotes. The generic familysans-serifshould not be quoted.Final Answer:
font-family: 'Open Sans', Arial, sans-serif; -> Option CQuick Check:
Quotes for multi-word + commas + generic unquoted = D [OK]
- Not quoting multi-word font names
- Missing commas between fonts
- Quoting generic font families
