Typography
fonts, sizes, weight, line-height
Skills taught: css.typography, css.fonts, css.text-properties
Font family and fallbacks
body {
/* List fonts in preference order; end with a generic family */
font-family: 'Inter', 'Segoe UI', Arial, sans-serif;
}
code, pre {
font-family: 'Fira Code', 'Consolas', 'Courier New', monospace;
}
/* Load a web font from Google Fonts */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap');
Size, weight, and style
h1 {
font-size: 2.5rem; /* relative to root font size (usually 16px) */
font-weight: 700; /* 100–900; 400 = normal, 700 = bold */
font-style: normal; /* normal | italic | oblique */
letter-spacing: -0.02em; /* tighten display headings */
text-transform: uppercase; /* DISPLAY HEADINGS */
}
p {
font-size: 1rem; /* 16px at default browser settings */
font-weight: 400;
line-height: 1.6; /* unitless — 1.6 × font-size — scales correctly */
}
Tip: Use unitless line-height (e.g. 1.5) not 24px. A unitless value multiplies the element's own font-size, so it scales correctly when the font-size changes.
Text alignment and decoration
.hero { text-align: center; }
.article { text-align: left; } /* default — most readable for body text */
a {
text-decoration: none; /* remove underline */
text-decoration: underline; /* explicit underline */
}
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; /* shows '...' when text overflows */
max-width: 200px;
}
Type scale — consistent hierarchy
:root {
--text-xs: 0.75rem; /* 12px */
--text-sm: 0.875rem; /* 14px */
--text-base: 1rem; /* 16px */
--text-lg: 1.125rem; /* 18px */
--text-xl: 1.25rem; /* 20px */
--text-2xl: 1.5rem; /* 24px */
--text-3xl: 1.875rem; /* 30px */
--text-4xl: 2.25rem; /* 36px */
}
h1 { font-size: var(--text-4xl); font-weight: 800; }
h2 { font-size: var(--text-2xl); font-weight: 700; }
p { font-size: var(--text-base); line-height: 1.6; }
Frequently Asked Questions
What is the difference between em and rem for font sizes?
em is relative to the parent element's font-size. rem is relative to the root (<html>) font-size (usually 16px). rem is easier to reason about because it does not compound through nested elements.
How do I load custom fonts?
Use Google Fonts: add <link href="https://fonts.googleapis.com/css2?family=Inter..." rel="stylesheet"> to your <head>, then set font-family: 'Inter', sans-serif in CSS. For self-hosted fonts, use @font-face with the font file URLs.
How many fonts should I use on a page?
One to two font families is usually enough. One for body text, optionally one for headings or code. Every additional font family is an extra network request.