Lesson 14 of 29 · CSS Basics
CSS Colors and Units: hex, rgb(), px, em, rem and %
Nearly every CSS declaration you write sets a colour or a size. CSS gives you several ways to write both, and choosing the right one — especially the right unit — makes your pages easier to read and easier to change.
Three ways to write a colour
h1 { color: tomato; } /* a named colour */
h1 { color: #ff6347; } /* hex */
h1 { color: rgb(255, 99, 71); } /* rgb() */
All three lines produce exactly the same colour:
- Named colours — about 140 words like
red,teal,tomatoorrebeccapurple. Easy to read, but limited. - Hex — a
#and three pairs of hexadecimal digits for red, green and blue, each from00(none) toff(full).#ff6347means lots of red, some green and a little blue. Design tools usually hand you colours in hex. rgb()— the same three channels as numbers from 0 to 255. Modern CSS also accepts them without commas, plus a slash for transparency:rgb(255 99 71 / 50%)is half see-through.
Text and background
color sets the colour of the text; background-color paints the element's box behind it:
body {
background-color: #fdf6e3;
color: rgb(60, 60, 60);
}
Keep enough contrast between the two. Light grey text on a white background looks elegant to its designer and is hard to read for everyone else.
Units for sizes
px— pixels, a fixed size. Predictable, and great for borders and small details.%— a percentage of the parent element.width: 50%makes an element half as wide as its parent.em— relative to a font size. Infont-size,2emmeans twice the parent's font size. Nestedemsizes multiply, which can surprise you.rem— relative to the root element,<html>, whose font size is 16px by default.1.5remis 24px, wherever it's used.
h1 { font-size: 2.5rem; } /* 2.5 × 16px = 40px */
.note { font-size: 0.875rem; } /* 14px */
.panel { width: 50%; } /* half as wide as its parent */
rem is the favourite for font sizes: it's predictable, and it grows when visitors choose a bigger default text size in their browser settings — sizes in px ignore that choice.
Common mistakes
- Forgetting the
#.color: ff6347is ignored; it has to be#ff6347. - A space before the unit, or no unit at all.
16 pxandwidth: 300are both invalid — the number and its unit belong together:16px. Only0may go without a unit. - Compounding
em. A list withfont-size: 1.2eminside another one is 1.44 times the size. - Low contrast. If you have to squint at your own page, so does everyone else.
Assignment
Write three rules:
body: the background colour#fdf6e3.h1: the text colour#b5432cand the font size2.5rem..panel: the background colourwhite, a width of50%, the text colourrgb(60, 60, 60)and the font size18px.
Premium lesson
Unlock the full Learn HTML & CSS course
- All 29 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .