Units, backgrounds and CSS variables
Choose the right CSS unit (px, rem, %, vw, ch), add background images, gradients and shadows, and keep your colours and spacing consistent with CSS custom properties.
🎯 By the end of this lesson you will be able to:
- Pick sensible units for text, spacing and widths
- Use background images, gradients and shadows
- Store colours and sizes once in CSS custom properties (variables)
Outline
What you'll learn and why it matters
Professional sites look consistent: the same green everywhere, the same spacing between sections, text that grows when a reader needs it. Two tools make that easy: choosing the right units, and storing design choices once in CSS variables. You will also add finishing touches like gradients and shadows.
Which unit when?
| Unit | Means | Good for |
|---|---|---|
px | A pixel on the screen (a fixed size) | Borders, small details, shadows |
rem | The root font size (usually 16px; follows the user's setting) | Font sizes and most spacing |
em | The current element's font size | Padding that should grow with a button's text |
% | A percentage of the parent's size | Widths inside a container |
vw / vh | 1% of the browser window's width / height | Full-screen sections, very large headings |
ch | About the width of one character | Comfortable line lengths, such as max-width: 65ch |
body {
font-family: Arial, sans-serif;
}
.box {
width: 80%;
max-width: 40rem;
padding: 1rem;
border: 2px solid #64748b;
}
.box p {
max-width: 30ch;
}Show the HTML this CSS styles
<div class="box">
<p>This box is 80% of the page width, but never wider than 40rem. This paragraph stops at about 30 characters per line, so it is easy to read.</p>
</div>Browser preview
Backgrounds, gradients and shadows
background-image can show a picture or a gradient, a smooth blend between colours. box-shadow adds depth behind a box.
.hero {
background-image: linear-gradient(135deg, #166534, #4ade80);
color: white;
padding: 3rem 1.5rem;
border-radius: 1rem;
text-align: center;
}
.photo-card {
background-image: url("images/cafe.svg");
background-size: cover;
background-position: center;
min-height: 12rem;
border-radius: 1rem;
margin-top: 1.5rem;
box-shadow: 0 8px 24px rgb(0 0 0 / 0.2);
}Show the HTML this CSS styles
<div class="hero">
<h1>Green Leaf Café</h1>
<p>Fresh snacks and the best chai in town.</p>
</div>
<div class="photo-card" role="img" aria-label="A cup of tea on a café table"></div>Browser preview
| Code | What it does |
|---|---|
linear-gradient(135deg, A, B) | Blends colour A into colour B along a diagonal |
url("images/cafe.svg") | Uses an image file as the background (path relative to the CSS file) |
background-size: cover | Scales the image to fill the box, cropping the edges if needed |
box-shadow: 0 8px 24px rgb(0 0 0 / 0.2) | Shadow moved 0 sideways and 8px down, blurred 24px, black at 20% strength |
CSS variables (custom properties)
A custom property stores a value under a name starting with two dashes. Define them on :root (the <html> element) so the whole page can use them, then read them with var().
:root {
--brand: #166534;
--brand-light: #dcfce7;
--radius: 0.75rem;
--space: 1rem;
}
h1 {
color: var(--brand);
}
.card {
background-color: var(--brand-light);
border: 2px solid var(--brand);
border-radius: var(--radius);
padding: var(--space);
margin-bottom: var(--space);
}Show the HTML this CSS styles
<h1>Today's specials</h1>
<div class="card">Banana bread</div>
<div class="card">Ginger lemon tea</div>Browser preview
Now try the idea in your head: change --brand to #7c3aed and the heading and both borders turn purple at once. On a real site with 50 rules using the brand colour, that is one change instead of 50.
Practice
Guided exercise
Theme a profile card
Create variables for a main colour (#1d4ed8), a light colour (#dbeafe) and a radius (1rem). Use them to style a profile card with a gradient background from the light colour to white, a coloured border, rounded corners and a soft shadow.
- Add a
:rootrule with--main,--lightand--radius. - In
.profile, uselinear-gradient(var(--light), white)as the background image. - Add
border,border-radius,paddingandbox-shadow.
Show the solution
:root {
--main: #1d4ed8;
--light: #dbeafe;
--radius: 1rem;
}
.profile {
max-width: 20rem;
padding: 1.5rem;
background-image: linear-gradient(var(--light), white);
border: 2px solid var(--main);
border-radius: var(--radius);
box-shadow: 0 4px 12px rgb(0 0 0 / 0.15);
font-family: Arial, sans-serif;
}
.profile h2 {
color: var(--main);
margin-top: 0;
}Show the HTML this CSS styles
<div class="profile">
<h2>Kofi Mensah</h2>
<p>Front-end developer in Accra. I build fast, accessible websites.</p>
</div>Browser preview
How to get there: The variables hold the design choices, and the rules only refer to them. A gradient is a background image, so it uses background-image. margin-top: 0 removes the heading's default space at the top of the card.
Your turn
Readable line length
Make the article readable on any screen: limit it to 65 characters per line, centre it, use a 1.125rem font size and 1.7 line height.
Show the solution
article {
max-width: 65ch;
margin: 0 auto;
font-size: 1.125rem;
line-height: 1.7;
}Show the HTML this CSS styles
<article>
<h1>Why short lines are easier to read</h1>
<p>When lines are very long, your eyes have to travel a long way back to find the start of the next line, and it is easy to lose your place. Around 50 to 75 characters per line is comfortable for most readers.</p>
</article>Browser preview
How to get there: ch ties the width to the text size, so the line length stays comfortable even if the reader enlarges the text. margin: 0 auto centres the article.
Common mistakes
Writing var(brand) or var(-brand)
You see: The value is ignored
Fix: Custom property names need two dashes everywhere: --brand and var(--brand).
Using background-color for a gradient
You see: Nothing appears
Fix: Gradients are images: use background-image.
A background image path that is relative to the HTML instead of the CSS file
You see: The image does not load when the CSS is in a separate folder
Fix: url() paths start from the CSS file's folder.
Using px for all font sizes
You see: Text ignores the reader's preferred text size
Fix: Use rem for font sizes.
Recap
- Use rem for text and spacing, % or max-width for widths, px for borders, ch for line length.
- Gradients and pictures go in
background-image;background-size: coverfills a box. - Background images are decorative: meaningful images belong in
<img>with alt text. - Define custom properties on
:root(--brand: …) and use them withvar(--brand).
Key terms
- Custom property
- A CSS variable whose name starts with --, read with var().
- Gradient
- A smooth blend between two or more colours, used as a background image.
- :root
- A selector for the top element of the page (html), where site-wide variables are usually defined.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Which unit follows the reader's browser text-size setting?
2. How do you use a custom property called --brand?
3. Which property shows a gradient?
4. A photo shows the product a page is selling. How should you add it?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.