Skip to content

Beginner · CSS basics · topic 17 of 64 · 25 min

Colours, backgrounds and units

Explanation

CSS colours can be written as names, hex codes like #1d4ed8, rgb() or hsl(), with optional transparency. Sizes use units: px for fixed sizes, rem for text that respects the user's settings, % and vw for sizes relative to the parent or the screen.

Ways to write a colour

FormatExampleNote
Nametomato, navyAbout 140 names; quick for testing
Hex#1d4ed8, #fffRed, green, blue in base 16; most common in design files
rgb()rgb(29 78 216)Red, green, blue from 0 to 255
rgb with alphargb(0 0 0 / 50%)Alpha means transparency: 0% clear, 100% solid
hsl()hsl(220 80% 50%)Hue (0-360), saturation, lightness: easy to make lighter or darker shades

Modern browsers also support oklch(), a newer format where equal steps look equally bright to the eye. You will see it in design systems; hex and hsl are enough to start.

Think of it like this: Think of mixing paint at a hardware shop. Hex and rgb tell the machine exactly how much red, green and blue to add. hsl is like asking for "blue, quite strong, medium light", which is easier for a human to adjust.

Backgrounds, borders and shadows

css
.banner {
  background-color: #fff7ed;
  background-image: linear-gradient(90deg, #f97316, #facc15);
  border: 1px solid #fdba74;
  border-radius: 12px;               /* rounded corners */
  box-shadow: 0 2px 8px rgb(0 0 0 / 15%);
  color: #1f2937;
}
A festive banner with a gradient, rounded corners and a soft shadow

Units for size

UnitRelative toGood for
pxNothing (fixed)Borders, small fixed details
remThe root font size (usually 16px)Font sizes, spacing
emThe current element's font sizePadding that grows with the text of a button
%The parentWidths inside a container
vw / vh1% of the viewport width / heightFull-screen sections
dvh1% of the dynamic viewport heightFull-height sections on phones where the address bar hides and shows

Why rem for text? Many people, especially older users, increase the default text size in their phone or browser settings. Sizes in rem grow with that setting; sizes in px do not.

css
html { font-size: 100%; } /* respect the user setting (usually 16px) */
h1 { font-size: 2rem; }    /* 32px by default */
p  { font-size: 1rem; }
.hero { min-height: 100dvh; }
rem for text, dvh for a full-height section on phones
Tip: To turn px into rem, divide by 16: 24px is 1.5rem.
Careful: Light grey text on white looks stylish but is hard to read, especially on a cheap phone in sunlight. Keep enough contrast between text and background (you will learn the exact ratio in the accessibility topic).

Watch the lesson

Colours, backgrounds and units · 1/6

navy #1d4ed8 rgb(29 78 216) hsl(220 80% 50%)

Colours can be names, hex codes, rgb or hsl.

Prefer a video by a teacher? Find free videos on “css colors units rem em px explained” on YouTube ↗

In real life: A UPI payment success screen

A payment success screen uses a green background in hsl so the designer can make a lighter shade for the card just by raising the lightness. The amount is 2.5rem so it grows when the user has big-text settings, and the screen uses min-height: 100dvh so the "Done" button is never hidden behind the phone's address bar.

Practice (0/2 done)

Task 1 · Code · HTML

Style the .badge: background-color #16a34a, text colour white, border-radius 8px, and font-size 0.875rem.

Task 2 · Code · Javascript

Write a function pxToRem(px) that converts pixels to rem, assuming 1rem = 16px, and returns a string like "1.5rem". pxToRem(24) returns "1.5rem"; pxToRem(16) returns "1rem".

Quick quiz

Question 1

1.What does the alpha part of rgb(0 0 0 / 50%) control?

Question 2

2.Which unit grows when a user raises the default text size in their browser?

Question 3

3.With the default 16px root size, what is 2rem?

Question 4

4.Which property makes rounded corners?

Question 5

5.Why use dvh instead of vh for a full-height section on phones?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: The box model