Welcome to Day 27 of your frontend development journey! Today we're diving deep into the art of text styling—one of the most crucial skills for creating professional, readable, and visually appealing websites. By the end of this lesson, you'll be able to craft typographic systems that enhance both aesthetics and usability.
Why Typography Matters More Than You Think
Before we jump into code, consider this: 95% of web content is text. That means your typography choices directly impact:
- Readability and user experience
- Accessibility for all users
- Brand personality and perception
- Conversion rates and engagement
Let's transform your type styling from basic to beautiful.
1. Font Family: Your Design's Voice
The Professional Approach to Font Stacks
body {
font-family:
/* Modern system fonts first */
-apple-system, BlinkMacSystemFont,
/* Robust fallbacks */
"Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell,
/* Universal fallback */
sans-serif;
}
Key Principles:
- System fonts first for performance
- Logical fallbacks that match proportions
- Generic family always last
Custom Web Fonts Done Right
@font-face {
font-family: 'CustomFont';
src: url('fonts/custom-regular.woff2') format('woff2'),
url('fonts/custom-regular.woff') format('woff');
font-weight: 400;
font-style: normal;
font-display: swap; /* Critical for performance */
}
.heading {
font-family: 'CustomFont', Georgia, serif;
}
Pro Tip: Always use WOFF2 (with WOFF fallback) for best compression.
2. Font Size: Hierarchy & Readability
Relative Units for Responsive Design
:root {
/* Base size for 1rem */
font-size: 16px;
}
h1 {
font-size: 2.5rem; /* 40px */
line-height: 1.2;
}
p {
font-size: 1.125rem; /* 18px */
}
.small-text {
font-size: 0.875rem; /* 14px */
}
Why This Matters:
-
rem
units scale with browser settings - Maintains proportional relationships
- Accessible to users who zoom
Advanced Fluid Typography
h2 {
font-size: clamp(
1.5rem, /* Minimum */
4vw, /* Fluid value */
2.5rem /* Maximum */
);
}
Perfect for: Headings that need to adapt across devices.
3. Font Weight: Creating Visual Hierarchy
Standard Weight Scale
.light-text {
font-weight: 300; /* Often for decorative text */
}
.normal-text {
font-weight: 400; /* Default body text */
}
.semibold {
font-weight: 600; /* For emphasis */
}
.bold-heading {
font-weight: 700; /* Strong visual impact */
}
Important Note: Always check if your font family supports the weights you're using!
Variable Fonts (Modern Approach)
@font-face {
font-family: 'VariableFont';
src: url('font.woff2') format('woff2-variations');
font-weight: 100 900; /* Range available */
}
.dynamic-weight {
font-family: 'VariableFont';
font-weight: 387; /* Any value in range! */
}
4. Line Height: The Secret to Readability
Golden Ratio Approach
body {
line-height: 1.6; /* Optimal for paragraphs */
}
h1, h2, h3 {
line-height: 1.2; /* Tighter for headings */
}
.code-block {
line-height: 1.8; /* Extra space for code */
}
Professional Tip: Unitless values are preferred—they act as multipliers.
Vertical Rhythm System
:root {
--baseline: 1.5rem;
}
p {
margin-bottom: var(--baseline);
line-height: var(--baseline);
}
This creates consistent vertical spacing throughout your design.
5. Putting It All Together: A Typography System
Complete Type Scale Example
:root {
/* Base sizing */
--text-base-size: 1rem;
--text-scale-ratio: 1.25;
/* Type scale */
--text-xs: calc(var(--text-base-size) / var(--text-scale-ratio));
--text-sm: var(--text-base-size);
--text-md: calc(var(--text-sm) * var(--text-scale-ratio));
--text-lg: calc(var(--text-md) * var(--text-scale-ratio));
--text-xl: calc(var(--text-lg) * var(--text-scale-ratio));
/* Line heights */
--heading-line-height: 1.2;
--body-line-height: 1.6;
}
body {
font-size: var(--text-sm);
line-height: var(--body-line-height);
}
h1 {
font-size: var(--text-xl);
line-height: var(--heading-line-height);
}
h2 {
font-size: var(--text-lg);
line-height: var(--heading-line-height);
}
.caption {
font-size: var(--text-xs);
}
6. Advanced Text Effects
Text Stroke (Border)
.stroked-text {
-webkit-text-stroke: 1px black;
text-stroke: 1px black;
color: transparent;
}
Gradient Text
.gradient-text {
background: linear-gradient(45deg, #ff8a00, #e52e71);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
Text Shadow
.drop-shadow {
text-shadow: 1px 1px 3px rgba(0,0,0,0.3);
}
.neon-effect {
text-shadow:
0 0 5px #fff,
0 0 10px #fff,
0 0 20px #0ff;
}
7. Exercises to Build Your Skills
-
Create a Type Scale
- Implement a modular scale using CSS variables
- Apply it to headings and paragraphs
- Test readability at different screen sizes
-
Design a Pull Quote
- Style a standout quote with:
- Larger font size
- Different font family
- Special borders or background
- Appropriate line height
- Style a standout quote with:
-
Fix Poor Typography
- Improve this problematic CSS:
body { font-family: "Comic Sans MS"; font-size: 12px; line-height: 12px; }
What's Next?
Tomorrow (Day 28) we'll explore CSS Layout with Flexbox—the modern way to create flexible, responsive page structures. For advanced typography techniques including variable fonts and fluid type systems, check out Chapter 22 in the Learn Frontend Development in 180 Days ebook.
Pro Tip: Test your typography with these tools:
Top comments (0)