π Introduction
The font-family property in CSS is used to define the typeface of text on a webpage. It allows you to choose between built-in system fonts, web-safe fonts, and custom fonts. A beautiful, readable font improves UX and adds personality to your UI. β¨
π€ What is font-family?
It's a CSS property that tells the browser which font to use for rendering text. You can define a single font or a fallback list.
Basic Usage
p {
font-family: 'Arial';
}π§ Font Family Stack (Fallbacks)
A font family stack includes multiple fonts. If the first one isnβt available, the browser tries the next one. This ensures consistency across devices. π±π»
Example: Font Stack
body {
font-family: "Roboto", "Segoe UI", Helvetica, Arial, sans-serif;
}Note
π― Types of Font Families
- π¨ Serif β Fonts with decorative strokes (e.g., Times New Roman).
- β¨ Sans-serif β Clean fonts without strokes (e.g., Arial, Inter).
- π§± Monospace β Equal-width fonts (e.g., Courier New).
- βοΈ Cursive β Script-like fonts (e.g., Brush Script).
- γ°οΈ Fantasy β Decorative fonts used rarely.
π Generic Font Families Table
| Generic Family | Description | Example |
|---|---|---|
| serif | Fonts with small strokes | Times New Roman |
| sans-serif | Clean, modern fonts | Arial |
| monospace | Fixed width fonts | Courier New |
| cursive | Script handwritten fonts | Brush Script |
| fantasy | Decorative creative fonts | Impact |
π₯ Using Google Fonts
One of the easiest ways to use custom fonts is via Google Fonts. Just import the font and use the name in your CSS.
Import Google Font
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">Apply the Font
body {
font-family: "Poppins", sans-serif;
}Note
π Applying Multiple Font Families
Hereβs a practical example using different sections with different fonts:
Multiple Font Examples
h1 {
font-family: "Poppins", sans-serif;
}
p {
font-family: "Georgia", serif;
}
code {
font-family: "Fira Code", monospace;
}πΈ Visual Example
Further details can be found at: https://dummyimage.com/800x300/efefef/000&text=Font+Family+Demo
π‘ Tips for Choosing Fonts
- βοΈ Use sans-serif fonts for modern UIs.
- βοΈ Ensure good readability for body text.
- βοΈ Limit to 2β3 font families per site.
- βοΈ Test on multiple devices and OS.
π Conclusion
The font-family property is essential for styling text and ensuring a great reading experience. Mastering font stacks, generic families, and custom font usage will elevate your UI/UX design. π