The font-family property in CSS specifies the typeface or group of typefaces to be used for displaying text. It allows developers to control the appearance and readability of textual content across different devices and platforms.
What is the purpose of the font-family property?
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
Overview
The font-family property determines which typeface is used for rendering text on a webpage. It can list multiple font names in a prioritized order, known as a font stack, to ensure consistent display even if some fonts are unavailable on a user’s device.
Term | Description | Example |
|---|---|---|
Primary Font | The preferred typeface to use. | font-family: 'Roboto', sans-serif; |
Fallback Fonts | Alternatives that load if the primary font isn’t available. | 'Helvetica', 'Arial' |
Generic Family | Broad fallback like serif, sans-serif, or monospace. | sans-serif |
Example: Applying Font Family to Text
body {
font-family: 'Open Sans', 'Helvetica Neue', Arial, sans-serif;
}
This rule specifies several fallback fonts — the browser uses the first available option in the stack.
Generic Font Families
- serif: Fonts with decorative strokes (e.g., Times New Roman).
- sans-serif: Clean, modern fonts without strokes (e.g., Arial, Roboto).
- monospace: Fixed-width fonts where all characters align evenly (e.g., Courier New).
- cursive: Script-style fonts resembling handwriting.
- fantasy: Stylized, decorative display fonts used sparingly.
Best Practices
- Always include fallback fonts for better cross-platform compatibility.
- Web-safe fonts (like Arial or Verdana) ensure consistent rendering across browsers.
- Match font styles to content purpose — readability for paragraphs, distinction for headers.
<li>For custom web fonts, use the @font-face rule or Google Fonts.Example: Using a Google Font
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
<style>
h1 {
font-family: 'Poppins', sans-serif;
}
</style>
This snippet loads the 'Poppins' font from Google Fonts and applies it to all <h1> elements.
Think of font-family as your webpage’s voice — it sets the tone and personality for how your content communicates.
font-familycontrols the text’s visual style.- Use multiple font names for fallbacks.
- Generic families ensure graceful degradation.
- Choose readable fonts for accessibility and brand consistency.