Colors, Units and Typography
Specify colors in hex, RGB and HSL with enough contrast, choose between px, rem, % and viewport units, and set readable fonts and line spacing.
Welcome back!
Step 1 of 14Learn
Four ways to write a color
CSS gives you several ways to describe the same color:
color: rebeccapurple; /* named color */
color: #663399; /* hex */
color: rgb(102 51 153); /* red, green, blue, 0–255 each */
color: hsl(270 50% 40%); /* hue, saturation, lightness */Named colors (about 140 of them, from red to papayawhip) are easy to read but limited.
Hex codes are the most common in design tools. Each pair of hexadecimal digits is red, green and blue from 00 to FF (0 to 255): #FF0000 is red, #000000 is black, #FFFFFF is white. Three-digit shorthand doubles each digit: #F60 equals #FF6600.
RGB writes the same three channels in decimal.
HSL is the most intuitive for adjusting colors: hue is a position on the color wheel in degrees (0 red, 120 green, 240 blue), saturation is how vivid it is, and lightness goes from 0% (black) to 100% (white). To make a lighter version of a brand color, keep the hue and saturation and raise the lightness.
Add transparency with a fourth value: rgb(0 0 0 / 50%) is half-transparent black, great for overlays and shadows.
Colors apply to text (color), backgrounds (background-color) and borders (border-color).
Step 2 of 14Practice · Match the pairs
Match each color value with the color it produces.
Tap an answer, then tap the place where it belongs (or drag it there).
- #FFFFFF
- #000000
- rgb(255 0 0)
- hsl(120 100% 50%)
- rgb(0 0 0 / 50%)
Step 3 of 14Learn
Contrast: can everyone read it?
Light gray text on a white background looks elegant in a design mockup and is a struggle to read for many people, especially older readers, people with low vision and anyone using a phone in sunlight.
The Web Content Accessibility Guidelines (WCAG) define a contrast ratio between text and its background, from 1:1 (identical colors) to 21:1 (black on white). The common targets:
- 4.5:1 for normal body text.
- 3:1 for large text (roughly 24 px regular or about 19 px bold) and for important parts of icons and form controls.
Examples: black on white is 21:1. Medium gray #777777 on white is about 4.5:1, right at the limit. Light gray #AAAAAA on white is only about 2.3:1, too low for text.
You don't need to calculate ratios by hand. Browser developer tools show the contrast ratio when you inspect text, and free online checkers do too.
Two more color rules: never use color alone to convey information ("required fields are in red" fails for colorblind users, so add an asterisk or the word "required"), and check that link text stands out from surrounding text, usually with an underline.
Step 4 of 14Practice · Choose one
A designer wants body text in light gray #AAAAAA on a white background, which has a contrast ratio of about 2.3:1. What should you recommend?
Step 5 of 14Practice · Chart
Contrast ratio of gray text on white
WCAG level AA asks for at least 4.5:1 for normal body text and 3:1 for large text.
Tap the lightest gray that still meets 4.5:1 for body text.
Tap the bar that answers the question, then press Check.
Step 6 of 14Learn
Units: px, rem, % and viewport units
CSS lengths use units, and choosing the right one makes pages flexible and accessible.
px (pixels) is a fixed size. It's fine for borders and small details, but fixed font sizes ignore a user's browser font setting.
rem is relative to the root (html element) font size, which is 16 px by default. So 1rem = 16 px, 1.5rem = 24 px and 2rem = 32 px. If a user sets their browser to larger text, everything in rem grows proportionally. Use rem for font sizes and often for spacing.
em is relative to the current element's font size. It's useful for spacing that should scale with the text, like padding inside a button, but it compounds when nested: 1.2em inside 1.2em is 1.44 times the base.
% is relative to the parent, usually its width. width: 50% takes half the parent's width.
vw and vh are 1% of the viewport's width and height. height: 100vh fills the screen's height, handy for full-screen hero sections.
ch is about the width of the "0" character, perfect for limiting line length: max-width: 65ch keeps paragraphs at a comfortable 60 to 75 characters per line.
Step 7 of 14Note
Step 8 of 14Try the calculator
rem to pixels
With the default root size of 16 px, 1rem = 16 px. Change the root size to see how rem-based text responds to a user's larger font setting.
- Size in pixels
- 24 px
- Same size if a user sets the root to 20 px
- 30 px
Step 9 of 14Practice · Work it out
With the default root font size of 16 px, how many pixels is 2.25rem?
Step 10 of 14Compare
Which CSS unit when?
| Aspect | px | rem | % | vw / vh |
|---|---|---|---|---|
| Relative to | Nothing: fixed | The root font size (usually 16 px) | The parent element | 1% of the viewport width / height |
| Respects user text size | No | Yes | Depends on the parent | No |
| Typical use | Borders, fine details | Font sizes, spacing | Widths of columns and images | Full-screen sections, fluid headings |
Step 11 of 14Learn
Typography that people can read
Most of the web is text, so good typography is the biggest single improvement you can make to a plain page.
Font family. Specify a font stack: a list of choices, ending with a generic family, in case the first isn't available:
body {
font-family: "Inter", Arial, sans-serif;
}Generic families include serif, sans-serif and monospace. Using the visitor's system fonts (font-family: system-ui, sans-serif;) costs nothing to download. Web fonts from font services look distinctive but add download time; limit yourself to one or two families and a few weights.
Size. Body text around 1rem to 1.125rem (16 to 18 px) reads well on most screens. Create clear contrast between levels: an h1 might be 2.5rem, an h2 1.75rem.
Line height. Set line-height around 1.5 to 1.7 for body text. Use a number without units, so it scales with each element's font size. Headings look better tighter, around 1.2.
Line length. Long lines are tiring; aim for about 60 to 75 characters with max-width: 65ch on text containers.
Weight and style. font-weight: 700 is bold; 400 is normal. Avoid long passages in italics or all capitals, which are harder to read.
Step 12 of 14Note
Step 13 of 14Practice · Select all that apply
Which settings help make body text comfortable to read? Select all that apply.
Select all that apply.
Step 14 of 14Practice · What would you do?
What would you do?
Leo sets every font size in his stylesheet in px. A visitor with low vision has set her browser's default text size to "Very large," but Leo's site still shows small text. What should Leo change?
Lesson complete!