T2 CSS · Lesson 2
Colours and units
Text and background colours; colours as numbers in rgb and hex; hsl for shades, and see-through colours with an alpha and opacity; and the units px, em, rem, %, vw and vh, with which to use for each job.
- set a text colour and a background colour, by name or by number
- read and write colours as hex codes and as rgb(), and say how red, green and blue light mix
- write colours as hsl() to make lighter and darker shades, and make colours see-through
- choose between px, em, rem and % for a size, and say what each is measured against
- size things by the screen with vw and vh, and pick units that respect the reader's settings
The back cover
A painter mixes colours by eye, but a screen mixes them by numbers: three small lamps of red, green and blue light in every dot. Sizes work the same way, from a pixel to a share of the screen. Choose the right number and the page looks right on every phone.
These pages show colour names and why exact colours need numbers, in rgb and in hex. They show hsl for shades, see-through colours, and the units px, em, rem, percent, vw and vh, with a rule for which to use for each job.
Before you start
The story: Three reds and a tiny notice
Meher's dye vats steam along the back wall of the Tailors' Hall. Corin stands in front of three banners for the Lamplighters' Fair, each a different red: one like a tomato, one like a brick, one almost pink. "I asked the dyers for red," he says. "All three of them gave me red."
At the same time a courier brings back your notice from the harbour. On the giant harbour glass its letters are fine. On the courier's palm-sized panel they are so small that nobody can read them.
Meher folds her arms. "Red is a word," she says. "A dyer needs a number. And a size that is right on one glass is wrong on another. Fix both."
Lesson 1 wrote rules with named colours such as teal and maroon, and sizes in pixels. Names run out fast, though, and pixels are not always the right measure. This lesson gives every colour an exact number and every size the right kind of unit. It builds on these ideas; each has a tiny example.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| A rule: selector, property, value. | p { color: teal; }. | Every colour and size here is a value in a rule. |
| The browser reports colours as rgb(). | teal comes back as rgb(0, 128, 128). | This lesson explains those three numbers. |
| A screen is made of tiny dots. | A phone's screen has millions of pixels. | The px unit is named after them. |
| Some properties are inherited. | A div's colour and font size pass to the p inside it. | Units such as em are worked out from the size a child inherits. |
| Contrast must be at least 4.5 to 1. | #767676 on white just passes, from the HTML step's accessibility lesson. | Every colour you choose must still pass it. |
| The reader may enlarge the text. | Zoom to 200 percent, or a bigger default font in the browser's settings. | Some units follow the reader's choice, and some ignore it. |
| Percent means "out of a hundred". | 50% of ₹400 is ₹200. | A % size is a share of something else's size. |
| Mixing paint. | Yellow and blue paint make green. | Screens mix light, which mixes differently. |
| The viewport tag. | <meta name="viewport" content="width=device-width, initial-scale=1">. | Units such as vw are shares of that visible width. |
By the end of this lesson you will be able to:
- set a text colour and a background colour, by name or by number;
- read and write colours as hex codes and as rgb(), and say how red, green and blue light mix;
- write colours as hsl() to make lighter and darker shades, and make colours see-through;
- choose between px, em, rem and % for a size, and say what each is measured against;
- size things by the screen with vw and vh, and pick units that respect the reader's settings.
In one line
This lesson gives colours exact numbers and sizes the right units; you need lesson 1's rules and inheritance, and the HTML step's contrast rule.
Text colour and background colour
At a glance
Every element has two colours of its own: the colour of its text, set by color, and the colour of the box behind it, set by background-color. CSS also knows 148 colour names, from red to rebeccapurple, which are handy for a quick try.
But a name gives you only the shades someone else chose, and a design needs exact colours, which are written as numbers. Whatever colours you pick, the text and its background must pass the contrast rule, so that everyone can read the page.
Take a guess: Can 148 colour names give a designer every colour they need?
A shop's signboard has two colours that matter most: the letters, and the board behind them. Yellow letters on white are useless, however pretty the yellow. CSS has a property for each of the two.
| Property | Colours | Example |
|---|---|---|
color | the text (and, by default, its underline and border) | color: navy; |
background-color | the box behind the text, out to its edges | background-color: lightyellow; |
<style>
.board {
color: white;
background-color: maroon;
}
</style>
<p class="board">Asha Stores: open 8 am to 9 pm</p>
Checked in Chrome
- white letters
- on a maroon board
- and the pair passes the contrast rule of 4.5 to 1
The background fills the paragraph's whole box, across the full width of the page, because a paragraph is a block. Remember lesson 1: color is inherited, so the letters of everything inside take it; background-color is not inherited, but a child's background is see-through by default, so the parent's colour shows behind it.
Named colours
CSS knows 148 colours by name: the plain ones such as red, blue and white, and fancier ones such as tomato, gold, teal, lavender and rebeccapurple. Names are handy for a quick try. Capitals do not matter: Teal and teal are the same.
<style>
.a { color: tomato; }
.b { color: Gold; background-color: black; }
.c { color: rebeccapurple; }
</style>
<p class="a">tomato</p>
<p class="b">gold on black</p>
<p class="c">rebeccapurple</p>
Checked in Chrome
- tomato is rgb(255, 99, 71)
- Gold, with a capital, still works: rgb(255, 215, 0)
- rebeccapurple is rgb(102, 51, 153)
But a name gives you only that one exact shade. A shop's brand red, a school's exact blue, the saffron of the flag: none of these has a name. For them you need the colour's numbers, which the next section explains.
Contrast still rules
Whatever colours you pick, the pair of text and background must pass the contrast rule from the HTML step: 4.5 to 1 for ordinary text, 3 to 1 for large text. A beautiful colour nobody can read is a failed colour.
Worked example 1
A school's results notice should have dark text on a pale background, readable by everyone. Choose a pair of named colours and check the contrast.
What we have: the wish for dark on pale, and the contrast rule
What we need: two named colours and a passing contrast
The plan: pick a dark text colour and a pale background, then let the browser measure the pair
<style>
.notice {
color: darkblue;
background-color: aliceblue;
}
</style>
<p class="notice">Class 10 results: Friday, 10 am</p>
Checked in Chrome
- darkblue is rgb(0, 0, 139)
- the pair passes 4.5 to 1
darkblue on aliceblue: dark text on a pale background, passing the contrast rule.
Check: the ratio is computed in the browser, from the colours it drew, not guessed.
When you are stuck: text and background colours
color is the letters; background-color is the box behind them. Set both, and check the pair's contrast.
If a name does nothing, check its spelling: the browser skips names it does not know.
Pale text on white, or dark on dark, fails the contrast rule; DevTools' colour picker shows the ratio.
Your turn
Which property colours the box behind the text?Tap to reveal
- It has background in its name.
background-color.
Do capitals matter in colour names?Tap to reveal
- Look at the Gold check.
No: Gold and gold are the same.
Why can a name not give a shop's exact brand red?Tap to reveal
- How many reds have names?
Because a name is one fixed shade; an exact colour needs its numbers.
Did you know?
A colour named for a girl
The colour rebeccapurple, #663399, is named in memory of Rebecca Meyer, the daughter of Eric Meyer, a well-known author on CSS. She died in 2014, on her sixth birthday. When the web community proposed beccapurple, her father asked that the name be rebeccapurple: just before her birthday she had said that Becca was a baby name. The CSS Working Group approved it that June.
Why it fits here: The 148 names of this topic include this one, chosen to remember a person.
Sources: Eric Meyer: rebeccapurple (meyerweb.com); Wikipedia: Eric A. Meyer; W3C: CSS Color Module Level 4, named colors
Names that do not add up
The CSS Color standard itself admits that its colour names are not consistent: darkgray is lighter than gray, and lightpink is darker than pink. The reason is history. Gray came from HTML, while dark gray came from X11, an older list of names. The standard also notes that indianred was originally named after a red pigment from India, and it does not encourage using the names at all.
Why it fits here: This is why this topic sends you to numbers for any colour that matters.
Sources: W3C: CSS Color Module Level 4, named colors; Wikipedia: X11 color names
In one line
color sets the text and background-color the box behind it; 148 named colours are handy, but an exact colour needs numbers, and every pair must pass the contrast rule.
Colours as numbers: rgb and hex
At a glance
Look closely at a screen and every dot is three tiny lamps: red, green and blue. Each lamp has a brightness from 0 to 255, and the three together make one colour. So rgb(255 153 51) means full red, a little more than half green, and a little blue: a saffron orange.
Hex writes the same three numbers in sixteens, as two digits each: #FF9933. The letters A to F stand for the digits ten to fifteen. When each pair repeats its digit, a short form works too: #F93 is #FF9933.
Take a guess: Three lamps, each with 256 levels: how many colours can they make together?
A screen has no paint. Every dot on it is three tiny lamps, one red, one green and one blue, each turned up or down. Look at a phone screen through a magnifying glass and you can see them. Every colour you have ever seen on a screen is a mix of those three lights.
Mixing light
Light mixes the opposite way to paint. Paint gets darker as you mix more in; light gets brighter. All three lamps fully on make white; all three off make black.
| Red lamp | Green lamp | Blue lamp | You see |
|---|---|---|---|
| full | off | off | red |
| full | full | off | yellow (red and green light make yellow) |
| full | full | full | white |
| off | off | off | black |
| half | half | half | grey |
rgb(): three numbers
CSS writes a colour as the strength of each lamp, from 0 (off) to 255 (full on): rgb(red green blue). Commas between the numbers work too, and are what the browser reports.
<style>
.red { color: rgb(255 0 0); }
.yellow { background-color: rgb(255 255 0); }
.grey { color: rgb(128, 128, 128); }
</style>
<p class="red">Full red light</p>
<p class="yellow">Red and green light together</p>
<p class="grey">Half of every light</p>
Checked in Chrome
- red: 255, 0, 0
- red and green full: yellow
- half of all three: grey
Why 255? Each lamp's strength is stored in one byte, a group of 8 binary digits, which holds 256 values: 0 to 255. Three bytes give 256 × 256 × 256, about 1.68 crore colours.
Hex codes: the same three numbers, written shorter
Designers usually write colours as a hex code: a hash, then six characters, such as #FF9933. It is the same three numbers in hexadecimal, a way of counting in sixteens that uses the digits 0 to 9 and then the letters A to F for ten to fifteen. Each pair of characters is one lamp: the first pair red, the second green, the third blue.
| Pair | Read it as | Worked out | Lamp strength |
|---|---|---|---|
FF | fifteen sixteens and fifteen | 15 × 16 + 15 | 255, full |
99 | nine sixteens and nine | 9 × 16 + 9 | 153 |
33 | three sixteens and three | 3 × 16 + 3 | 51 |
00 | nothing | 0 | 0, off |
So #FF9933 is red 255, green 153, blue 51: the saffron of the national flag. The browser agrees:
<style>
.saffron { background-color: #FF9933; }
.green { background-color: #138808; }
.navy { color: #000080; }
</style>
<p class="saffron">Saffron</p>
<p class="navy">The chakra's navy blue</p>
<p class="green">India green</p>
Checked in Chrome
- #FF9933 is rgb(255, 153, 51)
- #138808 is rgb(19, 136, 8)
- #000080 is rgb(0, 0, 128)
Capitals do not matter in hex: #ff9933 and #FF9933 are the same. And when each pair is a doubled character, there is a short form of three: #F93 means #FF9933, and #FFF means white.
<style>
.short { color: #F93; }
.long { color: #FF9933; }
</style>
<p class="short">#F93</p>
<p class="long">#FF9933</p>
Checked in Chrome
- #F93 and #FF9933 are the same colour
A memory aid
R, G, B, in that order, always. In rgb() and in a hex code alike, red comes first, then green, then blue: the three lamps in the same order every time.
Worked example 2
A shop's brand colour is given as #1E8449. Work out its red, green and blue by hand, and say roughly what colour it is.
What we have: a hex code
What we need: the three lamp strengths, and the colour they make
The plan: split it into three pairs; turn each into a number with sixteens and ones
- Red: 1E. 1 sixteen, and E is fourteen: 16 + 14 = 30.
- Green: 84. 8 sixteens and 4: 128 + 4 = 132.
- Blue: 49. 4 sixteens and 9: 64 + 9 = 73.
- Little red, a lot of green, some blue: a deep green.
<style>
.brand { color: #1E8449; }
</style>
<p class="brand">Asha Stores</p>
Checked in Chrome
- the browser agrees: rgb(30, 132, 73)
rgb(30, 132, 73), a deep green.
Check: the browser turned #1E8449 into exactly the three numbers worked out by hand.
A path that fails: Reading 1E as one and E side by side, "1 and 14", giving 114. The first character counts sixteens, so 1E is 16 + 14 = 30.
When you are stuck: rgb and hex
Split a hex code into three pairs: red, green, blue.
For each pair: the first character times 16, plus the second. A is 10, B 11, C 12, D 13, E 14, F 15.
Or let DevTools do it: click a colour square in the Styles pane, and it shows the same colour in every format.
Your turn
What colour is rgb(0, 0, 0)?Tap to reveal
- Every lamp is off.
Black.
Work out the green of #3366CC.Tap to reveal
- The middle pair, 66: 6 sixteens and 6.
6 × 16 + 6 = 102.
What is the long form of #0A3?Tap to reveal
- Double each character.
#00AA33.
Did you know?
Nearly 1.7 crore colours
Each lamp's level is stored in eight bits, so a whole colour takes 24 bits. That gives 256 × 256 × 256 = 1,67,77,216 colours, nearly 1.7 crore. The CSS Color standard reads each hex pair as one lamp, where 00 is the minimum and ff, 255 in decimal, is the maximum.
Why it fits here: The rgb() and hex codes of this topic can name every one of those colours.
Sources: Wikipedia: Web colors; W3C: CSS Color Module Level 4, the hex notation
Why two hex digits fit one lamp
Hex suits colours because of how computers store them. Each lamp's level takes eight bits, and two hexadecimal digits describe exactly eight bits, from 00 to ff. So six hex digits name a whole colour, one pair for red, then green, then blue, with nothing left over.
Why it fits here: The six digits of a hex code in this topic are those three pairs, in the order red, green, blue.
Sources: Wikipedia: Web colors; W3C: CSS Color Module Level 4, the hex notation
In one line
A screen colour is red, green and blue light, each 0 to 255: rgb(255 153 51). In hex it is #FF9933, where each pair is a lamp counted in sixteens, and #F93 is the short form.
Shades with hsl, and see-through colours
At a glance
Nobody thinks of a colour as so much red, green and blue. We say a darker blue, or a paler green. The form hsl() names a colour that way: a hue, an angle round the colour wheel; a saturation, how strong it is; and a lightness, how light it is.
So the shades of one colour share a hue and change only the lightness. An alpha value after a slash makes a single colour see-through, while opacity fades a whole box with everything inside it.
Take a guess: To make a button's blue a little darker, which of the three numbers would you change?
rgb tells the lamps what to do, which suits a screen. It does not suit people. Ask "what is a lighter version of rgb(19, 136, 8)?" and you cannot answer without a calculator. A tailor thinks differently: which colour, how strong, how light. CSS can be written that way too.
A colour given as hue, saturation and lightness: hsl(hue saturation lightness). The browser turns it into red, green and blue for the screen.
| Part | Means | Written as | Picture it as |
|---|---|---|---|
| hue | which colour | an angle, 0 to 360 | a point on a colour wheel: 0 red, 120 green, 240 blue, and back to red at 360 |
| saturation | how strong the colour is | 0% to 100% | 0% is grey; 100% is the colour at its boldest |
| lightness | how light it is | 0% to 100% | 0% is black, 100% is white, 50% is the plain colour |
<style>
.saffron { background-color: hsl(30 100% 60%); }
.teal { color: hsl(180 100% 25%); }
</style>
<p class="saffron">hsl(30 100% 60%)</p>
<p class="teal">hsl(180 100% 25%)</p>
Checked in Chrome
- hue 30 (orange), full strength, fairly light: the flag's saffron, rgb(255, 153, 51)
- hue 180, full strength, dark: teal
Shades from one colour
Here is why hsl is worth learning. To make a family of shades of one colour, keep the hue and the saturation, and change only the lightness. A shop can have its brand colour, a pale version for backgrounds and a dark one for text, all plainly related.
<style>
.pale { background-color: hsl(30 100% 90%); }
.plain { background-color: hsl(30 100% 60%); }
.dark { color: hsl(30 100% 25%); }
</style>
<p class="pale">Pale saffron, for a background</p>
<p class="plain">Saffron itself</p>
<p class="dark">Dark saffron, for text</p>
Checked in Chrome
- lightness 90%: a pale cream
- lightness 25%: a dark brown-orange, readable as text
See-through colours: alpha
A colour can also be partly see-through, like tinted glass. A fourth number, the alpha, says how solid it is: 1 (or 100%) is solid, 0 is invisible, 0.5 is half see-through. It goes after a slash in rgb() or hsl().
<style>
.card { background-color: navy; }
.glass { background-color: rgb(255 255 255 / 50%); color: white; }
</style>
<div class="card"><p class="glass">Half see-through white on navy</p></div>
Checked in Chrome
- white at half strength: the browser reports rgba(255, 255, 255, 0.5)
The browser reports such colours as rgba(), rgb with an a for alpha. You will also see rgba() written in older CSS, with commas: rgba(255, 255, 255, 0.5) is the same colour. The colour transparent is fully see-through: rgba(0, 0, 0, 0).
opacity: fading a whole box
The property opacity fades an element and everything inside it at once: its text, its background, its pictures. An alpha fades only the one colour it is written in.
<style>
.faded { opacity: 0.5; background-color: maroon; color: white; }
</style>
<div class="faded"><p>Sold out</p></div>
Checked in Chrome
- the box is drawn at half strength
- the paragraph's own opacity is still 1, yet it is drawn faded with its box
Fading text you need to read
opacity: 0.5 on a box of text halves the contrast of everything in it. A "Sold out" card faded to half may fall below 4.5 to 1. Fade only what may be hard to read, and check the contrast of what must be read.
Worked example 3
A school's site uses the blue hsl(220 80% 45%) for its buttons. Make a pale version of it for a notice's background, and a dark version for the notice's text, keeping the same blue.
What we have: one blue, and the need for a pale and a dark version
What we need: two more colours in the same family
The plan: keep the hue (220) and the saturation (80%); raise the lightness for pale, lower it for dark
<style>
.notice {
background-color: hsl(220 80% 94%);
color: hsl(220 80% 22%);
}
</style>
<p class="notice">Admissions close on 15 June.</p>
Checked in Chrome
- lightness 94%: a pale blue
- lightness 22%: a deep blue
hsl(220 80% 94%) for the background and hsl(220 80% 22%) for the text.
Check: the browser turned both into rgb, and the pale one is far lighter than the dark one, as the lightness said.
When you are stuck: hsl and alpha
hsl: which colour (an angle), how strong (a percent), how light (a percent).
For shades of one colour, change only the lightness.
Alpha after a slash fades one colour; opacity fades the whole box and everything in it.
Your turn
In hsl(120 100% 50%), which colour is it?Tap to reveal
- Hue 120 on the colour wheel.
Green, at full strength.
What does an alpha of 0 mean?Tap to reveal
- 1 is solid.
Fully see-through: invisible.
What is the difference between an alpha and opacity?Tap to reveal
- One fades a colour; one fades a box.
An alpha fades one colour; opacity fades the whole element, children included.
Did you know?
hsl came to CSS in 2011
CSS Color Level 3, a W3C Recommendation of 7 June 2011, added hsl() as a complement to rgb(). It gave its reason plainly: the advantage of HSL over RGB is that it is far more intuitive, so you can guess at the colours you want, and then tweak. It also makes matching sets of colours easier, by keeping the hue and varying the lightness.
Why it fits here: The shades of this topic, one hue with several lightnesses, are the matching sets that standard had in mind.
Source: W3C: CSS Color Module Level 3
Why see-through is called alpha
The idea of an alpha channel, a fourth number that says how see-through a colour is, came from Alvy Ray Smith and Ed Catmull in the late 1970s, at the New York Institute of Technology's Computer Graphics Lab. A 1984 paper by Thomas Porter and Tom Duff developed it further. Smith said they called it alpha because of the classic formula for blending two images, which uses the Greek letter alpha.
Why it fits here: The alpha after the slash in this topic is that fourth number.
Sources: Wikipedia: Alpha compositing; Alvy Ray Smith: Alpha and the History of Digital Compositing
In one line
The form hsl(hue saturation lightness) names a colour the way people think, so shades of one colour change only the lightness. An alpha after a slash makes one colour see-through, and opacity fades a whole box.
Units: px, em, rem and %
At a glance
A size needs a unit, the way a recipe needs grams or cups. A px is a fixed size. An em is measured by the text: for font-size it is the parent's font size, and em inside em multiplies, so nested boxes grow or shrink.
A rem is measured by the root element, 16px by default, and it follows the reader's own font setting, which makes it kind to people who need larger text. A percent is a share of the parent. Every number except zero needs its unit.
Take a guess: If a reader sets their phone's text larger, which sizes on a page should grow with it?
"Make the heading twice as big" means nothing until you say twice as big as what. A tailor measures some things in fixed centimetres (a button is 2 cm across) and others by the person (a sleeve is the length of the arm). CSS has both kinds of measure, called units, and choosing the right one is half of good CSS.
| Unit | Kind | Measured against | Example |
|---|---|---|---|
px | absolute (fixed) | nothing else: a CSS pixel | border: 1px solid black; |
em | relative | the font size of the element (for font-size: its parent's) | padding: 1em; |
rem | relative | the font size of the page's root, the html element (16px unless changed) | font-size: 1.25rem; |
% | relative | the same property of the parent (for width: the parent's width) | width: 50%; |
px: a fixed size
A CSS pixel, px, is a fixed small length, about the size of one dot on an ordinary computer screen. On a phone with a sharp screen, one CSS pixel is drawn with several of the phone's tiny dots, so things stay a sensible size. The px unit is right for things that should not grow with the text, such as a thin border.
em: measured by the text
An em is one times the font size. For font-size itself, it is measured against the parent's font size; for every other property, against the element's own font size.
<!DOCTYPE html>
<style>
.card { font-size: 20px; }
.card p { font-size: 1.5em; padding: 1em; }
</style>
<div class="card"><p>Diwali offers</p></div>
Checked in Chrome
- font-size 1.5em: 1.5 × the card's 20px = 30px
- padding 1em: 1 × the paragraph's own 30px = 30px
That is why em is good for spacing: padding in em grows with the text, so big text gets roomy padding and small text tight padding, by itself.
em that keeps growing
em multiplies at every level. Give list items font-size: 1.2em and nest lists three deep: each level is 1.2 × the one above, so 16px becomes 19.2px, then 23.04px, then 27.648px. When sizes creep up in nested parts, rem is usually the fix.
<!DOCTYPE html>
<style>
li { font-size: 1.2em; }
</style>
<ul><li>Spices<ul><li>Whole<ul><li>Jeera</li></ul></li></ul></li></ul>
Checked in Chrome
- (the slip: each level grows by 1.2 again: 19.2px, 23.04px, 27.648px)
rem: measured by the root
A rem (root em) is measured against the font size of the html element, the root of the page, whatever the parent. Unless a sheet changes it, that is 16px. So 2rem is 32px everywhere, at any depth.
<!DOCTYPE html>
<style>
.card { font-size: 20px; }
.card h2 { font-size: 2rem; }
</style>
<div class="card"><h2>Fresh today</h2></div>
Checked in Chrome
- the root's font size: 16px by default
- 2rem is 2 × 16px = 32px, whatever the card's 20px
Why rem respects the reader
A reader who sees poorly can set a bigger default font in the browser's settings, say 20px instead of 16px. Sizes in rem follow that choice: 2rem becomes 40px. Sizes in px ignore it. So set font sizes in rem, and the page grows for the reader who asked.
%: a share of the parent
A percentage is a share of something on the parent: for font-size, the parent's font size; for width, the parent's width. A box with width: 50% inside a 400px box is 200px wide.
<!DOCTYPE html>
<style>
.shelf { width: 400px; }
.half { width: 50%; background-color: lightyellow; }
</style>
<div class="shelf"><div class="half">Half the shelf</div></div>
Checked in Chrome
- 50% of the shelf's 400px is 200px
A number needs its unit
Write padding: 10 and the browser does not know: 10 pixels? 10 em? It skips the declaration. The one exception is zero, since zero of anything is zero: margin: 0 is fine.
<!DOCTYPE html>
<style>
p { margin: 0; padding: 10; }
</style>
<p>Open till 9.</p>
Checked in Chrome
- margin: 0 works without a unit
- (the slip: padding: 10 has no unit, so it is skipped)
Worked example 4
A product card has font-size 18px. Its price should be 1.5 times the card's text, and the card's padding should be one line of its own text. Write the rule with em, and work out each size in pixels.
What we have: a card at 18px, a price at 1.5 times, padding of one em
What we need: the rule, and the pixel sizes it gives
The plan: font-size 1.5em on the price (measured against the card); padding 1em on the card (against its own 18px)
- The price: 1.5 × 18px = 27px.
- The card's padding: 1 × its own 18px = 18px.
<!DOCTYPE html>
<style>
.card { font-size: 18px; padding: 1em; }
.card .price { font-size: 1.5em; }
</style>
<div class="card">Toor dal <span class="price">₹180</span></div>
Checked in Chrome
- the price is 27px
- the padding is 18px
.price { font-size: 1.5em; } gives 27px; .card { padding: 1em; } gives 18px.
Check: the browser drew both at the sizes worked out by hand.
Worked example 5
A reader with weak eyes sets the browser's default font to 20px instead of 16px. A page has two headings: one at 1.5rem and one at 24px. Which grows, and to what?
What we have: the reader's 20px root, a heading in rem and a heading in px
What we need: each heading's size after the change
The plan: rem is measured by the root, which now follows the reader; px is fixed
- Before: 1.5rem is 1.5 × 16 = 24px, the same as the 24px heading.
- After: the root is 20px, so 1.5rem is 1.5 × 20 = 30px. The px heading stays at 24px.
<!DOCTYPE html>
<style>
/* the reader's setting, shown here as a rule on the root */
html { font-size: 20px; }
.in-rem { font-size: 1.5rem; }
.in-px { font-size: 24px; }
</style>
<h2 class="in-rem">Grows with the reader</h2>
<h2 class="in-px">Stays the same</h2>
Checked in Chrome
- 1.5rem follows the 20px root: 30px
- 24px ignores it: still 24px
The rem heading grows to 30px; the px heading stays at 24px.
Check: the block sets the root to 20px, as the reader's setting does, and the browser gives exactly those sizes.
A path that fails: Believing browser zoom makes the two the same, so the unit does not matter. Zoom enlarges both, but the default-font setting, which many readers use, moves only rem and em.
When you are stuck: units
Ask "as what?": px is fixed; em is the text's size; rem is the root's size; % is a share of the parent.
Font sizes creeping up in nested parts: switch em to rem.
Every number except zero needs its unit, with no space: 10px, not 10 px.
Your turn
With no changes to the root, how many pixels is 1.5rem?Tap to reveal
- The root is 16px.
1.5 × 16 = 24px.
A box is 600px wide. How wide is a child with width: 25%?Tap to reveal
- A share of the parent's width.
150px.
Why are font sizes better in rem than in px?Tap to reveal
- Think of a reader who sets a bigger default font.
Because rem follows the reader's chosen font size; px ignores it.
Did you know?
A CSS pixel is an angle
A CSS pixel is not one dot of your screen. The CSS standard defines a reference pixel as the visual angle of one pixel on a 96 dpi screen seen from an arm's length. For an arm of 28 inches, that is about 0.0213 degrees. It matches the rule that 1px is one ninety-sixth of an inch.
Why it fits here: The px of this topic is that agreed size, meant to look about the same on a phone held close and a laptop further away.
Source: W3C: CSS Values and Units Module Level 4, absolute lengths
The em began as a square of metal
The em is older than computers. In metal type, the letter m was once cast on a square body, so a square space as wide as a line was tall came to be called an em-quad. CSS kept the name for a size measured by the font: 1em equals the computed font size of the element it is used on.
Why it fits here: The em of this topic still means what the printers meant: a size that follows the type.
Sources: Wikipedia: Em (typography); W3C: CSS Values and Units Module Level 4, font-relative lengths
In one line
A px is a fixed size, and an em is measured by the text (its parent's for font-size) and multiplies when nested. A rem is measured by the root, 16px by default, and follows the reader's settings. A % is a share of the parent, and every number but zero needs its unit.
Sizing by the screen, and choosing a unit
At a glance
Some sizes should follow the screen itself. The unit vw is 1% of the viewport's width and vh is 1% of its height, so 100vh is one full screen tall. On a phone, though, the address bar slides in and out, and the screen's height changes as you scroll.
The unit dvh follows that moving height. A rule of thumb ends the guessing: size text in rem, spacing in em or rem, hairlines in px, layout widths in percent, and full screens in dvh.
Take a guess: On a phone, does the screen stay the same height while you scroll?
A cinema's screen and a phone's screen both show the same film, filling each screen edge to edge. The film is not sized in centimetres; it is sized as "the whole width of whatever screen this is". CSS has units like that too.
| Unit | Means | Example |
|---|---|---|
vw | 1% of the viewport's width (the visible part of the page) | width: 50vw;: half the screen's width |
vh | 1% of the viewport's height | height: 100vh;: the full screen's height |
The viewport is the part of the page you can see at once: the window, or on a phone, the screen, as set by the viewport tag of the HTML step.
<!DOCTYPE html>
<style>
.banner { width: 50vw; height: 10vh; background-color: gold; }
</style>
<div class="banner">Diwali sale</div>
Checked in Chrome
- 50vw is half the window's width
- 10vh is a tenth of the window's height
The checks compare the banner with the window's own size, so they hold on any screen. Resize the window, and a vw-sized box resizes with it. A full-screen welcome section is often min-height: 100vh: at least as tall as the screen.
On phones: dvh
On a phone, the browser's address bar slides away as you scroll, so the visible height changes. 100vh is measured with the bar hidden, so a 100vh section can be cut off at the bottom while the bar shows. The unit dvh (dynamic viewport height) follows the bar: min-height: 100dvh always fits what is visible.
Text sized in vw
font-size: 3vw looks good on a laptop, becomes tiny on a phone, and cannot be enlarged by the browser's zoom in the usual way, which fails readers who need bigger text. Size text in rem; the responsive design lesson of this step shows how to let it grow gently with the screen.
Which unit for which job
| Job | Best unit | Why |
|---|---|---|
| font sizes | rem | follows the reader's chosen font size; no creeping when nested |
| padding and margins around text | em or rem | space that grows with the text it surrounds |
| borders and hairlines | px | a thin line should stay thin at any text size |
| widths inside a layout | % | a share of the box it sits in |
| full-screen sections | vh or dvh | measured by the screen itself |
| zero | no unit | margin: 0 |
Worked example 6
A shop's welcome section should fill at least the whole screen on any phone, its heading should be 2.5 times the reader's normal text, and it should have a 1 pixel border at the bottom. Choose the units and write the rule.
What we have: three sizes, each with a different job
What we need: a unit for each, and the rule
The plan: full screen: dvh; a heading by the reader's text: rem; a hairline: px
<!DOCTYPE html>
<style>
.welcome {
min-height: 100dvh;
border-bottom: 1px solid gray;
}
.welcome h1 { font-size: 2.5rem; }
</style>
<section class="welcome"><h1>Asha Stores</h1></section>
Checked in Chrome
- the section is at least the screen's height
- 2.5rem is 2.5 × 16px = 40px, and it grows if the reader enlarges the text
- a 1px line at the bottom
min-height: 100dvh; a heading of 2.5rem; border-bottom: 1px solid gray.
Check: each size is measured against the thing it should follow: the screen, the reader's text, nothing at all.
When you are stuck: choosing units
Ask what the size should follow: the screen (vw, vh, dvh), the reader's text (rem, em), the parent (%), or nothing (px).
Text: rem. Hairlines: px. Full-screen sections: dvh.
Never size body text in vw: it shrinks on phones and fights zoom.
Your turn
What is 25vw on a screen 1200px wide?Tap to reveal
- 1vw is 1% of the width.
25 × 12 = 300px.
Why can 100vh be cut off on a phone?Tap to reveal
- Think of the address bar.
Because it is measured with the address bar hidden; dvh follows the bar.
Which unit suits a 1-pixel divider line?Tap to reveal
- It should stay thin whatever the text size.
px.
Did you know?
Why 100vh was too tall on phones
On a phone, toolbars such as the address bar appear and disappear, so the viewport changes size, but vw and vh do not. As a result, an element 100vh tall was too tall when the page loaded, and part of it hid behind the bar. The dynamic units, such as dvh, fix this, and they shipped in Chrome 108, joining Safari and Firefox, which already had them.
Why it fits here: The dvh of this topic was made for exactly this problem.
Source: web.dev: The large, small, and dynamic viewport units
Three heights, not one
A phone screen has three heights in CSS. Units with the sv prefix, such as svh, measure the small viewport, and units with the lv prefix, such as lvh, measure the large one; both stay fixed unless the viewport itself is resized. The dv units sit between them: 100dvh becomes either the large or the small size as the toolbars come and go.
Why it fits here: The dvh of this topic is the one of the three that moves, which is why it suits a full-screen section.
Source: web.dev: The large, small, and dynamic viewport units
In one line
The units vw and vh are 1% of the viewport's width and height, and dvh follows a phone's moving address bar. Size text in rem, spacing in em or rem, hairlines in px, layout widths in %, and full screens in dvh.
Where it is used
Every colour and size on every site you use was chosen in one of these ways.
| What you learned | Where it is used | A real example |
|---|---|---|
| hex codes | brand colours everywhere | a payments app's exact shade of purple, written once as a hex code and used on every screen |
| rgb() with an alpha | overlays and shadows | the dark see-through layer behind a pop-up, so the page shows dimly behind it |
| hsl() shades | colour families in design systems | a bank's blue in ten lightness steps, from a pale background to a dark heading |
| opacity | disabled or sold-out items | a sold-out product card drawn faded |
| rem | font sizes on accessible sites | a government site whose text grows when a reader sets a bigger default font |
| em | padding inside buttons | a button whose padding grows when its text is made bigger |
| % | widths inside layouts | two columns of 50% each on a product page |
| vh and dvh | full-screen sections | a film streaming site's first screen, exactly the height of the phone |
Mistakes and debugging
As in lesson 1, a broken value is skipped without a word. These are the slips that cause it, each checked in the browser.
1. A hex code without its hash, or with the wrong length
<!DOCTYPE html>
<style>
.a { color: FF9933; }
.b { color: #FF993; }
</style>
<p class="a">No hash</p>
<p class="b">Five characters</p>
Checked in Chrome
- (the slip: both are skipped, so the text stays black)
The fix: a hash, then 3 or 6 characters (or 4 or 8 with an alpha): #FF9933. Beware a page with no doctype: in the old quirks mode, the browser accepts a hex code without its hash, and a size without its unit, so the slip hides until the doctype is added. Every page should start with the doctype anyway.
2. A space between a number and its unit
<!DOCTYPE html>
<style>
p { padding: 10 px; font-size: 1.5 rem; }
</style>
<p>Fresh today.</p>
Checked in Chrome
- (the slip: 10 px and 1.5 rem, with spaces, are skipped)
The fix: 10px, 1.5rem, written as one word.
3. A number with no unit
padding: 10 is skipped, as the units section showed. The fix: padding: 10px. Only zero may go without.
4. rgb numbers over 255
<style>
.x { color: rgb(300 0 0); }
</style>
<p class="x">Too red?</p>
Checked in Chrome
- (not an error: the browser cuts 300 down to 255, the most a lamp can do)
Not skipped, but not what you wrote either: the lamp is at full. The fix: keep each number from 0 to 255.
5. em that grows at every level
Nested lists or cards with font sizes in em get bigger at each level. The fix: rem, measured by the root every time.
6. Fading away the contrast
opacity, or a pale alpha, on text that must be read can drop it below 4.5 to 1. The fix: check the contrast of the colours as drawn, in DevTools' colour picker.
The debugging corner: the colour picker and the Computed tab
In DevTools' Styles pane, click the small square beside any colour. The picker opens: it converts the colour between hex, rgb and hsl (click the format's label to switch) and shows the text's contrast ratio with a tick when it passes. The Computed tab shows each size as the pixels it became, which settles any "how big is 1.5em here?" question.
Quick card
The lesson on one card.
Every idea of the lesson
<!DOCTYPE html>
<style>
.brand {
color: #FFFFFF;
background-color: hsl(30 100% 25%);
font-size: 1.25rem;
padding: 1em;
border-bottom: 1px solid rgb(0 0 0 / 50%);
width: 50%;
}
.hero { min-height: 50vh; background-color: #FF9933; }
</style>
<div class="hero"><p class="brand">Asha Stores</p></div>
Checked in Chrome
- hex white on an hsl dark saffron
- 1.25rem is 20px, and 1em of padding is the same 20px
- a half see-through black line
- the hero is half the screen's height
- #FF9933, the flag's saffron
| Idea | In one line |
|---|---|
color, background-color | the text, and the box behind it; check their contrast |
| named colours | 148 of them, such as teal and tomato; capitals do not matter |
rgb(255 153 51) | red, green and blue light, each 0 to 255 |
#FF9933, #F93 | the same in hex, each pair counted in sixteens; the short form doubles each character |
hsl(30 100% 60%) | hue (an angle), saturation, lightness; shades change only the lightness |
rgb(0 0 0 / 50%), opacity: 0.5 | an alpha fades one colour; opacity fades the whole box |
px, em, rem, % | fixed; by the text; by the root (16px); a share of the parent |
vw, vh, dvh | 1% of the screen's width or height; dvh follows a phone's address bar |
The story: The guild's red, in numbers
You write the guild's red as numbers, so much red light, so much green, so much blue, and send the same three numbers to every dyer. All three vats come out the same. From that one colour you make a lighter and a darker shade by changing a single number.
Then you set the notice's sizes by the reader's own type instead of fixed measures. On the courier's panel the letters grow to fit; on the harbour glass they stay in proportion. Old Mr Hollis turns up the type on his own panel, and everything on the notice grows with it.
Meher holds the three new banners side by side. "One red," she says. "At last."
Reagents gathered: hex, rgb, hsl, alpha, rem, viewport.
Sources
- MDN Web Docs (Prose CC BY-SA 2.5 or later; code samples added since 2010-08-20 CC0 (its licensing page, read 2026-10-26); reused with credit)
- HTML Living Standard (WHATWG) (CC BY 4.0: "Copyright WHATWG (Apple, Google, Mozilla, Microsoft). This work is licensed under a Creative Commons Attribution 4.0 International License" (its acknowledgements page, read 2026-10-26); reused with credit)