Colour, size and typography decide how a page feels and how easy it is to read. CSS lets you write colours as names, hex codes, RGB or HSL, and sizes in fixed units like px or flexible ones like rem, % and vw. This lesson shows each format in a live example and explains which units to choose for text and layout.
In this lesson you will learn
- Named, hex, RGB, RGBA and HSL colours, and gradients
- Absolute and relative units
- The difference between
emandrem - Font families, fallbacks and common text properties
- Readable contrast and line height
Colour Formats
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Colours</title>
<style>
div { padding: 6px; margin: 4px 0; color: white; font-family: sans-serif; }
.name { background: teal; }
.hex { background: #6a1b9a; }
.rgb { background: rgb(230, 81, 0); }
.rgba { background: rgba(230, 81, 0, 0.5); color: black; }
.hsl { background: hsl(210, 70%, 45%); }
.grad { background: linear-gradient(90deg, #00695c, #26a69a); }
</style>
</head>
<body>
<div class="name">Named colour: teal</div>
<div class="hex">Hex: #6a1b9a</div>
<div class="rgb">rgb(230, 81, 0)</div>
<div class="rgba">rgba(230, 81, 0, 0.5) is 50% transparent</div>
<div class="hsl">hsl(210, 70%, 45%)</div>
<div class="grad">linear-gradient</div>
</body>
</html>
Result
- Named colours (
teal,crimson) are easy to read but limited. - Hex
#RRGGBBgives red, green and blue in base 16:#ff0000is pure red. rgb()uses numbers from 0 to 255;rgba()adds an alpha value from 0 (invisible) to 1 (solid).hsl()is hue (0–360° around the colour wheel), saturation and lightness, which makes it easy to create lighter or darker shades.linear-gradient()blends colours and is used as a background image.
Units and Fonts
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Units and fonts</title>
<style>
html { font-size: 16px; }
body { font-family: Georgia, "Times New Roman", serif; line-height: 1.5; }
.px { font-size: 14px; }
.rem { font-size: 1.25rem; } /* 1.25 x 16px = 20px */
.em { font-size: 1.5em; font-weight: bold; }
.pct { width: 50%; background: #e0f2f1; }
.vw { font-size: 4vw; }
.sans { font-family: Arial, Helvetica, sans-serif; text-transform: uppercase; letter-spacing: 2px; }
</style>
</head>
<body>
<p class="px">14px text</p>
<p class="rem">1.25rem text (20px)</p>
<p class="em">1.5em bold text</p>
<div class="pct">50% of the page width</div>
<p class="vw">4vw grows with the window</p>
<p class="sans">Sans-serif, uppercase, spaced</p>
</body>
</html>
Result
| Unit | Relative to | Good for |
|---|---|---|
px | Fixed | Borders and small details |
rem | The root (html) font size | Font sizes and spacing; respects the user’s browser setting |
em | The element’s own font size (or its parent’s, for font-size) | Padding that scales with the text |
% | The parent element | Widths |
vw / vh | 1% of the viewport width / height | Full-screen sections, fluid headings |
font-familytakes a list of fallbacks ending in a generic family (serif,sans-serif,monospace). The browser uses the first font it has.line-height: 1.5(no unit) spaces lines at 1.5 times the font size, comfortable for body text.text-transform,letter-spacing,font-weightandtext-alignfine-tune the text.- Resize your window: the
4vwline changes size, the others do not.
Readable Text
Text needs enough contrast with its background. The WCAG accessibility guidelines ask for a contrast ratio of at least 4.5:1 for normal text. Browser developer tools show the ratio when you inspect a colour. Very light grey on white is a common failure.
Common Mistakes
- Setting all font sizes in
px, which ignores users who enlarge their default text size. - Nesting
emfont sizes, which multiply: 1.2em inside 1.2em is 1.44 times the original. - No fallback font, so a missing web font drops to the browser default.
- Low contrast colour choices that look stylish but are hard to read.
Practice Exercises
Exercise 1. Make a semi-transparent black overlay at 60% opacity.
Show answer
.overlay { background: rgba(0, 0, 0, 0.6); }
Exercise 2. If the root font size is 16px, what is 2.5rem in pixels?
Show answer
2.5 × 16 = 40px.
Frequently Asked Questions
Should I use rem or px?
Use rem for font sizes and most spacing so the page scales with the user’s settings, and px for fine details like 1px borders.
How do I use a Google Font?
Add the font’s <link> tag to your page’s head, then use its name in font-family with a fallback, such as font-family: "Hind Siliguri", sans-serif;.
Summary
- Colours can be names, hex, RGB(A) or HSL; gradients blend them.
remis best for text,%andvwfor fluid layout,pxfor details.- Always give font fallbacks and a comfortable line height.
- Check contrast: at least 4.5:1 for body text.
Next: CSS Flexbox.
