Font families, size, and weight
Typography gives a page hierarchy and personality. A small, consistent set of font sizes and weights is easier to scan than a different treatment for every heading.
✓ What you will learn in this topic
- Explain the key idea: Use fallback stacks, relative sizing, and a consistent type scale while preserving semantic headings.
- Apply the concept: The demo uses a system sans-serif face at the root-relative body size; the card heading scales within a readable range.
- Avoid this common mistake:
Concept explanation
font-family accepts a prioritized list, so include generic fallbacks such as sans-serif. font-size can use relative units like rem, which respect the userâs root text setting. font-weight controls emphasis; choose weights the selected font actually provides.
Use semantic headings in HTML and style their appearance with CSS. This keeps the document outline useful even if the visual design changes.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
.demo {
font-family: system-ui, sans-serif;
font-size: 1rem;
font-weight: 400;
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>.demo {
font-family: system-ui, sans-serif;
font-size: 1rem;
font-weight: 400;
}Expected result: The demo uses a system sans-serif face at the root-relative body size; the card heading scales within a readable range.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>.card h2 {
font-size: clamp(1.25rem, 1rem + 1vw, 1.75rem);
font-weight: 700;
line-height: 1.2;
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
Very small text, long full-width paragraphs, and fixed line heights can make reading uncomfortable, especially when users zoom.
Best practice
Use a sensible system-font fallback stack, relative sizing, comfortable line height, and a readable maximum text width.
Practice exercise
Style a short article with a heading scale, body text, a readable line length, and a link that is identifiable without color alone. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- Use fallback stacks, relative sizing, and a consistent type scale while preserving semantic headings.
- Use a sensible system-font fallback stack, relative sizing, comfortable line height, and a readable maximum text width.
Knowledge check
Why include a generic family at the end of font-family?
Reveal the explanation
The browser uses the next available family in the list, including the generic fallback.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
css.demo {
font-family: system-ui, sans-serif;
font-size: 1rem;
font-weight: 400;
}
Expected result:
The demo uses a system sans-serif face at the root-relative body size; the card heading scales within a readable range.
Hands-on practice task
Style a short article with a heading scale, body text, a readable line length, and a link that is identifiable without color alone.
In this section
Other lessons in Text and Typography
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.