Fonts in CSS - Choose font-family and font-size Like a Pro
Your font choice gives a site its personality: formal, modern, minimal, or classic. Choosing the right font with a comfortable size greatly improves readability.
If you are looking for font-family in CSS, best font-size for websites, or what fallback fonts are, this lesson covers it step by step.
What Are Font Families in CSS?
In CSS, fonts are grouped into general families. The most common are:
- Serif: letters with small strokes; classic and formal.
- Sans-serif: no strokes; cleaner and easier on screens.
- Monospace: each character has equal width (great for code).
Simple definition: font-family in CSS sets the font used to display text.
Result in the browser: Serif
Example Text
Result in the browser: Sans-serif
Example Text
Result in the browser: Monospace
Example Text
font-family in CSS with Fallback Fonts
Best practice is to provide a fallback list. If the first font is not available, the browser uses the next one.
p {
font-family: "Arial", "Helvetica", sans-serif;
}
What does this code do? It requests Arial first, then Helvetica, then any available sans-serif font.
Line by line:
"Arial": first choice."Helvetica": second fallback.sans-serif: generic fallback if the others are not available.
Expected result: a consistent font appearance across devices.
Common mistake: setting only one font without a fallback.
Result in the browser:
This text uses Arial, then Helvetica, then sans-serif.
font-size in CSS (Text Size)
The font-size property controls text size.
You can use different units:
px: fixed value (clearest for beginners).em: relative to the parent element.rem: relative to thehtmlfont size.%: percentage-based.
h1 {
font-size: 32px;
}
p {
font-size: 18px;
}
What does this code do? It makes headings larger than paragraphs to create a clear visual hierarchy.
Expected result: easier reading and a logical size scale.
Common mistake: using very small sizes like 12px for body text on mobile.
Result in the browser:
Small size (12px)
Comfortable reading size (18px)
Large heading size (32px)
When to Use serif vs sans-serif in CSS?
- sans-serif: usually best for long web text.
- serif: sometimes good for headings or a formal tone.
- monospace: perfect for code and technical data.
Typography Best Practices in CSS
- Keep body text between
16pxand20px. - Pair font-size with a good line-height (like 1.5 to 1.8).
- Always use fallback fonts.
- Do not use too many font families on one page.
16px.
Common Mistakes with font-family and font-size
1) Relying on an unavailable font: no fallback provided.
2) Extreme sizes: headings too large or body text too small.
3) Too many fonts: weakens visual consistency.
FAQ - Common Search Questions
How do I change the font in CSS?
Use font-family like: body { font-family: Arial, sans-serif; }.
What is the best font-size for body text in CSS?
Usually between 16px and 18px depending on the font and audience.
What is the difference between serif and sans-serif in CSS?
Serif fonts have small strokes and feel traditional; sans-serif fonts are cleaner for modern screens.
Why do we use fallback fonts in CSS?
To ensure a suitable replacement if the main font is not available on the user's device.
Should I use px or rem for font-size in CSS?
For beginners, px is easier. For advanced responsive projects, rem is more flexible.