Sub topic 7
Set typography and color
This chapter assumes you can link a stylesheet and write rules with selectors. It also assumes you know the cascade and the box model at a basic level. You will set the fonts, sizes, spacing between lines, and colors for the personal site. You will also check that text stays readable.
What typography covers on a one-page site
Most of a personal site is text, so a handful of properties decide how the page looks. Set them once on body and let inheritance carry them down. Font properties such as font-family, font-size, line-height and color are inherited. Every paragraph, list and link picks them up without its own rule.
Box properties such as margin and padding are not inherited. That difference lets you style the whole page with one rule and override only where you need to.
The last node matters. Browsers give links their own color, so inheriting color from body does not change links. You must style a yourself, as the stylesheet from the earlier chapter did with --color-accent.
Choose a font stack
font-family takes a list called a font stack. The browser uses the first font it finds on the visitor's device and falls through to the next. End every stack with a generic family such as sans-serif, serif or monospace, so there is always a fallback.
body {
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1,
h2 {
font-family: Georgia, "Times New Roman", serif;
}
code {
font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
}Names that contain spaces need quotes. system-ui is a keyword, not a font name, so it takes none.
A system font stack costs nothing to download and loads instantly. The page looks slightly different on each operating system, and for a personal site that is usually acceptable. Use a web font only if a specific typeface is part of your identity.
Loading a web font
If you do use one, declare it with @font-face and keep the system stack as a fallback.
@font-face {
font-family: "Inter";
src: url("fonts/inter-regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
body {
font-family: "Inter", system-ui, sans-serif;
}font-display: swap shows text in the fallback font right away and switches when the file arrives. Without it, some browsers hide text until the font loads. Use .woff2 files because they are small and supported by all current browsers. Check the font's license before you host it yourself.
Size text in rem
Pixel sizes ignore a visitor who raised the default text size in browser settings. Relative units respect it. rem is relative to the root element's font size, which is 16px in most browsers unless the user changes it.
| Unit | Relative to | Good for |
|---|---|---|
| `rem` | Root font size | Body text, headings, spacing |
| `em` | The element's own font size | Spacing that should scale with the text, such as button padding |
| `px` | Nothing (fixed) | Hairline borders, small shadows |
| `%` | The parent's font size | Rarely needed for fonts |
Leave the root size alone, or set it to 100%. Setting html { font-size: 62.5%; } to make 1rem equal 10px is a common trick, but it shrinks every size you forget to set.
A simple heading scale:
h1 { font-size: 2.5rem; }
h2 { font-size: 1.75rem; }
h3 { font-size: 1.25rem; }
h1,
h2,
h3 {
line-height: 1.2;
margin-block: 0 0.5em;
}Fluid sizes with clamp()
clamp(min, preferred, max) lets a size grow with the viewport but never leave a range. Use it for the main heading so it fits a phone and a wide monitor without a media query.
h1 {
font-size: clamp(2rem, 1.25rem + 3vw, 3.5rem);
}Put a rem term in the preferred value, as above. A value made only of vw does not respond to the user's text-size setting.
Make long text easy to read
Three properties do most of the work.
line-height: use a unitless number such as1.6for body text. A unitless value is multiplied by each element's own font size. A value like24pxis inherited as a fixed length, so a large heading inside it can end up with cramped lines.- Line length: 45 to 75 characters per line reads comfortably. The
chunit is about the width of the digit 0, somax-width: 65chcaps a paragraph near that range. font-weight: use400for body text and600or700for emphasis. Avoid weights your font does not include, because the browser fakes them and the result looks blurry.
p {
max-width: 65ch;
margin-block: 0 1em;
}
strong {
font-weight: 600;
}
.lead {
font-size: 1.25rem;
color: #3e4c59;
}The main element from the earlier chapter is 60rem wide, which is far more than 65 characters. The max-width on p keeps paragraphs narrow even when the column is wide. Headings can still span the full width.
Work with color
CSS accepts several color formats. They are interchangeable, so pick the one you can read most easily.
| Format | Example | Notes |
|---|---|---|
| Keyword | `rebeccapurple` | About 140 names, handy for quick tests |
| Hex | `#0b5cad` or `#0bd` | Compact. Common in design tools |
| `rgb()` | `rgb(11 92 173)` | Red, green and blue from 0 to 255 |
| `hsl()` | `hsl(209 88% 36%)` | Hue (0 to 360), saturation, lightness |
HSL is useful for building a palette. To make a darker hover color, lower the lightness and keep hue and saturation.
a {
color: hsl(209 88% 36%);
}
a:hover {
color: hsl(209 88% 26%);
}Three properties take colors: color for text, background-color for the box fill, and border-color. Keep the palette small. One text color, one background, one accent and one muted shade are enough for a single page.
Check contrast
Contrast ratio compares the brightness of text and its background. The Web Content Accessibility Guidelines (WCAG) level AA asks for at least 4.5:1 for normal text and 3:1 for large text, which means 24px and up, or 18.66px and up when bold.
Using the colors from the earlier stylesheet:
- Text
#1f2933on white is about 14.7:1. - Link
#0b5cadon white is about 6.7:1. - Light gray
#999999on white is about 2.85:1, which fails. - Gray
#767676on white is about 4.54:1, the lightest neutral gray that passes on white.
You can measure any pair with the contrast checker in your browser's developer tools. Select an element, open the color picker in the Styles pane, and it shows the ratio. Check pairs in every state, including :hover, and check text on top of images, which no stylesheet can measure for you.
Put it together
This block pulls the chapter's decisions into one stylesheet section. It defines the colors as custom properties, which the next chapter covers in detail. Replace or merge it with the matching rules from your existing styles.css.
:root {
--color-text: #1f2933;
--color-muted: #52606d;
--color-bg: #ffffff;
--color-accent: #0b5cad;
}
body {
margin: 0;
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
font-size: 1rem;
line-height: 1.6;
color: var(--color-text);
background: var(--color-bg);
}
h1,
h2,
h3 {
line-height: 1.2;
margin-block: 0 0.5em;
}
h1 { font-size: clamp(2rem, 1.25rem + 3vw, 3.5rem); }
h2 { font-size: 1.75rem; }
h3 { font-size: 1.25rem; }
p {
max-width: 65ch;
margin-block: 0 1em;
}
.muted {
color: var(--color-muted);
}
a {
color: var(--color-accent);
text-underline-offset: 0.15em;
}--color-muted (#52606d) has a ratio of about 6.4:1 on white, so secondary text stays readable.
Common errors
- Setting
line-height: 24px. Headings that inherit it get the same fixed height. Use a unitless number. - Sizing everything in
px. Text no longer follows the visitor's browser settings. - A font stack with no generic family. If the named font is missing, the browser falls back to its default serif.
- Unquoted font names with spaces.
font-family: Times New Romancan work, but quote such names so the list parses unambiguously. - Pale gray text for "elegance". Light gray on white often falls below 4.5:1. Measure it.
- Expecting
body { color }to recolor links. The browser's link style wins. Selectadirectly. - Too many fonts and weights. Each extra file slows the page. One or two families are plenty.
Exercise
A paragraph has font-size: 1.125rem and inherits line-height: 1.6 from body. A heading inside the same body has font-size: 2rem. What line height, in rem, does each get, and why does line-height: 1.6 work better here than line-height: 1.6rem on body?
Show answer
The paragraph's line height is 1.125 × 1.6 = 1.8rem. The heading's is 2 × 1.6 = 3.2rem. A unitless number is inherited as a multiplier, so each element multiplies it by its own font size. With 1.6rem on body, every element would inherit the fixed length 1.6rem. The 2rem heading would then have lines shorter than its own font size, and wrapped lines would overlap.
Exercise
Your muted text uses #999999 on a white background (ratio about 2.85:1). Name the WCAG AA target for normal text and describe one way to fix the color without changing its hue family (gray).
Show answer
The AA target for normal text is 4.5:1. Darken the gray, for example to #767676 (about 4.54:1) or #52606d (about 6.4:1), then confirm the ratio with the browser's developer tools.