Skip to content
Codewilds

Lesson 14 of 29 · CSS Basics

CSS Colors and Units: hex, rgb(), px, em, rem and %

easy10 XP2 min read

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, tomato or rebeccapurple. Easy to read, but limited.
  • Hex — a # and three pairs of hexadecimal digits for red, green and blue, each from 00 (none) to ff (full). #ff6347 means 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. In font-size, 2em means twice the parent's font size. Nested em sizes multiply, which can surprise you.
  • rem — relative to the root element, <html>, whose font size is 16px by default. 1.5rem is 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: ff6347 is ignored; it has to be #ff6347.
  • A space before the unit, or no unit at all. 16 px and width: 300 are both invalid — the number and its unit belong together: 16px. Only 0 may go without a unit.
  • Compounding em. A list with font-size: 1.2em inside 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:

  1. body: the background colour #fdf6e3.
  2. h1: the text colour #b5432c and the font size 2.5rem.
  3. .panel: the background colour white, a width of 50%, the text colour rgb(60, 60, 60) and the font size 18px.

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
Play the 7 free lessons

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 .

Write code ↓
HTML & CSS+10 XP

Lesson 14 is part of the full course. The first 7 lessons are free.

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
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.