CSS chapters

Chapter 5 of 17

Lengths, percentages and colour

Try this in our CSS Compiler →

The idea

CSS values have specific units and accepted types. px is a CSS pixel, rem is relative to the root font size, and percentages depend on the property and containing block. Not every property accepts every unit. Zero can often omit a unit, but a nonzero width normally cannot.

Worked example

body { font-size: 16px; }
button { padding: 0.5rem 1rem; border: 2px solid teal; }
h1 { color: rgb(0, 100, 100); }

What to check

If the root size is 16px, 1rem equals 16 CSS pixels. Changing a parent's font size does not change rem, unlike em-based sizing.

Practice task

Compare padding in rem and px while changing the root font size. Explain which value changes and why.

Check your understanding

Explain the result in your own words before changing the example. Change one thing at a time, compare the result with your prediction, and keep the version that demonstrates the concept clearly.

Try it yourself

Output
Ctrl/Cmd+Enter runs · Ctrl/Cmd+S downloads the project · Local auto-save is always on