20. Typography, Web Fonts & @font-face Integration
🟢 Beginner
1. Definition
CSS typography properties style text: font-family fallback stacks, web safe fonts, Google Fonts integration, custom font files via @font-face, font-size, font-weight, line-height, letter-spacing, text-align, and text-transform.
2. Why It Is Used
Typography controls readability, user comprehension, and visual hierarchy. Integrating custom web fonts builds brand identity across all user operating systems.
3. How It Works
@font-face downloads custom font files (.woff2) from a web server and maps them to a custom font-family name usable throughout your CSS.
4. Syntax
@font-face {
font-family: 'CustomFont';
src: url('fonts/custom-font.woff2') format('woff2');
font-display: swap;
}
body {
font-family: 'CustomFont', system-ui, sans-serif;
}
5. Typography Properties Reference Table
| Property | Function | Example Values |
|---|---|---|
font-family | Prioritized font stack fallback list | 'Inter', system-ui, sans-serif |
font-weight | Stroke boldness weight | 400 (Normal), 700 (Bold) |
line-height | Line spacing multiplier | 1.5 (Unitless multiplier) |
font-display | Font loading fallback behavior | swap (Prevents invisible text FOUT) |
6. Beginner Example
p {
font-family: Arial, sans-serif;
font-size: 1rem;
line-height: 1.6;
}
7. Intermediate Example
h1 {
font-size: 2.5rem;
font-weight: 800;
letter-spacing: -0.025em;
text-transform: uppercase;
}
8. Advanced Custom @font-face Loading Example
@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 400 700; /* Variable font weight range */
src: url('fonts/inter-variable.woff2') format('woff2');
font-display: swap; /* Flash of unstyled text prevention */
}
9. Line-by-Line Code Explanation
font-display: swap;: Immediately displays fallback system fonts while custom.woff2files load, preventing invisible text during web page load.
10. Expected Terminal / Browser Output
Clean, readable typography utilizing modern custom web fonts without page load rendering delays.
11. Common Mistakes & Pitfalls
- Forgetting fallback generic font families: Omitting
sans-seriforserifat the end offont-familystacks breaks formatting if custom fonts fail to load.
12. Important Rules & Limitations
- Always use modern compressed
.woff2font formats for fastest loading speed.
13. Multilingual Explanation: English
CSS typography properties format text readability. Use @font-face with font-display: swap for fast loading custom web fonts.
14. Multilingual Explanation: Hindi (Roman Script)
Typography se text ki readability aur style decide hota hai. Custom fonts ke liye @font-face aur fallback font family ka istemaal karein.
15. Multilingual Explanation: Marathi (Roman Script)
Typography mule text vachnyasathi sopaa hoto. Custom fonts jodnyasathi @font-face vapara.
16. Practice Questions
- Why is unitless
line-height: 1.5preferred over pixel line height (line-height: 24px)?
17. Practical Coding Challenge
Import a Google Font (e.g. `'Poppins'`) and apply it to a .heading-title class with font-weight: 600.