Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / CSS Tutorial / Chapter 2: Styling Text, Colors & Backgrounds
CSS ⏱ 10 min read

Typography, Web Fonts, and Text Formatting

Over 90% of web design is typography. Great typography makes reading comfortable and effortless.

What is it?

CSS typography properties govern font families, sizes, weights, line heights, letter spacing, text alignment, and text transforms.

Why is it used?

Consistent typography creates clear information architecture, brand distinction, and prevents reader eye fatigue.

Detailed Explanation

Key Typography Properties: • `font-family`: Comma-separated font stack with fallback systems (e.g. `'Inter', -apple-system, sans-serif`). • `font-size`: Size in `px`, `rem`, or `em`. Modern web uses `rem` (relative to root html font size, 1rem = 16px by default). • `font-weight`: 400 (normal), 600 (semi-bold), 700 (bold), 800 (extra-bold). • `line-height`: Vertical distance between lines of text (1.5 to 1.7 is ideal for prose). • `letter-spacing`: Horizontal spacing between letters (negative spacing e.g. `-0.5px` creates a crisp editorial look on large headings).

Syntax Structure

Syntax Blueprint
font-family: 'Inter', system-ui, sans-serif;
font-size: 1.125rem;
line-height: 1.6;
letter-spacing: -0.02em;

Code Example

CSS Code
/* Editorial Typography Stack */
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 1rem;
  line-height: 1.65;
  color: #1c1c1c;
}

h1, h2, h3 {
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.2;
  color: #1c1c1c;
}

h1 { font-size: 2.25rem; }
h2 { font-size: 1.75rem; }
h3 { font-size: 1.25rem; }

.article-lead {
  font-size: 1.125rem;
  color: #5f5f5d;
  line-height: 1.6;
}

Explanation of the Code

• Uses relative `rem` units for font sizes so text scales cleanly if users change their browser font settings. • Applies `line-height: 1.65` for optimal readability and negative letter-spacing for sharp modern headings.

Expected Output

High-contrast, professional typography that looks like a published magazine or clean technical documentation.

Real-World Application

Technical docs like Stripe, MDN, and CodeWithHarry optimize typography first before adding decorative graphics.

Common Mistakes to Avoid

• Using tiny fixed `px` fonts that don't respect user accessibility settings. • Setting `line-height: 1` on body paragraphs, causing lines of text to collide awkwardly.

Quick Practice Challenge

Style an article heading with 800 weight, -0.5px letter spacing, and a paragraph with 1.6 line height.

Key Takeaways

• Use `rem` units for accessible, scalable font sizing. • Maintain `line-height` between 1.5 and 1.7 for readable body text. • Use negative letter spacing on headings for modern aesthetic polish.