Lesson 13: Mastering Typeface & Font Size
Learn how to create beautiful typography with CSS. Understand font families, sizing techniques, weights, and responsive text strategies for modern web design.
Start LearningUnderstanding Font Families
Font families define the visual style of your text. CSS provides several generic font families and allows you to use custom web fonts for more design flexibility.
Font Family Comparison
The quick brown fox jumps over the lazy dog.
1234567890
Merriweather - Ideal for long-form content and editorial designs.
The quick brown fox jumps over the lazy dog.
1234567890
Inter - Excellent for UI elements and digital interfaces.
The quick brown fox jumps over the lazy dog.
1234567890
Roboto Mono - Perfect for code snippets and technical content.
The quick brown fox jumps over the lazy dog.
1234567890
Playfair Display - Elegant choice for headings and titles.
CSS Font Family Syntax
/* Specify a prioritized list of font families */ body { font-family: "Helvetica Neue", Arial, sans-serif; } /* Using generic family names */ h1 { font-family: serif; } p { font-family: sans-serif; } code { font-family: monospace; } /* Using web fonts */ @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap'); .heading { font-family: 'Roboto', sans-serif; } /* Using variable fonts */ @font-face { font-family: 'VariableFont'; src: url('font.woff2') format('woff2-variations'); font-weight: 100 900; font-stretch: 75% 125%; }
Practical Font Pairing Examples
Serif & Sans-serif
This combination creates a classic, professional look. Use serif for headings and sans-serif for body text to create clear visual hierarchy.
Sans-serif & Monospace
Perfect for technical documentation. Use sans-serif for headings and monospace for code snippets and technical details.
Display & Sans-serif
Ideal for editorial designs. Use a display font for headlines and a clean sans-serif for body text to balance elegance with readability.
Font Sizing Techniques
Choosing the right font size unit is crucial for responsive design. CSS offers several units for sizing text:
Absolute unit - fixed size regardless of context
Relative to parent element's font size
Relative to root (html) element's font size
Relative to parent element's font size
Relative to viewport width (1vw = 1% of viewport width)
Interactive Font Sizing Demo
Adjust the sliders to see how different units affect font sizes:
Recommended Base Sizes
Font Size Accessibility
Good Contrast
Text size: 18px on #4caf50
Contrast ratio: 4.8:1
Poor Contrast
Text size: 14px on #9e9e9e
Contrast ratio: 2.1:1
Excellent Contrast
Text size: 16px on #f44336
Contrast ratio: 5.9:1
Font Weight & Style
Beyond size and family, you can control the weight (boldness) and style (italic/oblique) of your fonts.
Font Weight
Font weight ranges from 100 (thin) to 900 (heavy), with 400 as normal and 700 as bold:
/* Font weight examples */ .light-text { font-weight: 300; } .normal-text { font-weight: 400; /* same as 'normal' */ } .bold-text { font-weight: 700; /* same as 'bold' */ } .heavy-text { font-weight: 900; } /* Using variable font weights */ .variable-text { font-variation-settings: "wght" 450; }
Font Style & Variable Fonts
Practical Applications
Apply typography principles to create readable, accessible, and visually appealing designs.
Line Height & Spacing
Proper line height improves readability. Aim for 1.5-1.7 times the font size for body text.
Tight Spacing (1.2)
This paragraph has tight line spacing which can make longer text difficult to read, especially on smaller screens or for users with visual impairments. The lines feel cramped and crowded together.
Optimal Spacing (1.6)
This paragraph has ideal line spacing that enhances readability. The text breathes naturally, creating a comfortable reading rhythm that doesn't tire the eyes. This is especially important for longer articles and content.
Loose Spacing (2.0)
This paragraph has very loose line spacing which can disconnect related lines of text. While it might work for short poetic verses, it becomes inefficient for longer content as it requires excessive scrolling.
Responsive Typography
Create fluid typography that adapts to different screen sizes using relative units and media queries.
Responsive Heading
This text scales smoothly between different viewport sizes using the clamp() function. Resize this container to see how the text adapts. Clamp() provides a minimum, preferred, and maximum value for fluid sizing.
/* Responsive typography with clamp() */ h1 { font-size: clamp(1.5rem, 5vw, 2.5rem); } p { font-size: clamp(1rem, 2.5vw, 1.2rem); line-height: 1.6; } /* Fluid typography with calc() */ .subheading { font-size: calc(1rem + 0.5vw); } /* Alternative with media queries */ @media (min-width: 1200px) { body { font-size: 18px; } } @media (max-width: 768px) { body { font-size: 16px; } }
Typography Scale
Create visual hierarchy with a consistent typography scale based on ratios like 1.333 (Perfect Fourth).
Caption
0.8rem / 12.8pxBody
1rem / 16pxSubheading
1.333rem / 21.33pxHeading
1.777rem / 28.43pxTitle
2.369rem / 37.9pxTypography Utility Classes
Create reusable utility classes for consistent typography across your project:
Extra Small Text (.text-xs)
For captions and fine printSmall Text (.text-sm)
For secondary informationMedium Text (.text-md)
Standard body text sizeLarge Text (.text-lg)
For lead paragraphs and emphasisExtra Large Text (.text-xl)
For subheadings and section titlesXXL Text (.text-xxl)
For main headings and page titles/* Typography utility classes */ .text-xs { font-size: 0.75rem; } /* 12px */ .text-sm { font-size: 0.875rem; } /* 14px */ .text-md { font-size: 1rem; } /* 16px */ .text-lg { font-size: 1.125rem; } /* 18px */ .text-xl { font-size: 1.25rem; } /* 20px */ .text-xxl { font-size: 1.5rem; } /* 24px */ /* Responsive utilities */ @media (min-width: 768px) { .text-xxl { font-size: 2rem; } /* 32px on larger screens */ } /* Font weight utilities */ .font-light { font-weight: 300; } .font-regular { font-weight: 400; } .font-medium { font-weight: 500; } .font-bold { font-weight: 700; }
Best Practices
Follow these guidelines for effective typography in your projects:
Font Units Comparison
Next Steps
Now that you understand typography fundamentals, continue to the next lesson to learn about text transformations and decorations.