9 / 9 · Basic
Style a small page
The bakery page from the HTML course gets its colours, spacing and layout from about 40 lines of CSS, and the HTML does not change at all.
Jump to the 9 practice questionsYou will learn
- Turn design decisions into custom properties
- Style a whole page with the pieces of every unit
- Check focus, contrast and narrow screens
Before this: Responsive design with media queries
1Explain
Start with decisions
Before writing rules, make a handful of decisions. Choose one accent colour, a text colour and a background with enough contrast, one font stack, a base size, and a spacing step that repeats everywhere (for example 8, 16 and 24 pixels). A page that reuses a few values looks calm and deliberate; a page where every box has its own slightly different margin looks messy even when nobody can say why.
:root {
--accent: #1a5fb4; /* 6.29:1 on white */
--ink: #222;
--paper: #fffdf8;
--space: 16px;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
line-height: 1.6;
color: var(--ink);
background: var(--paper);
}
header { display: flex; justify-content: space-between; align-items: center;
flex-wrap: wrap; gap: var(--space); padding: var(--space); }
nav ul { display: flex; gap: var(--space); list-style: none; margin: 0; padding: 0; }
main { max-width: 60rem; margin: 0 auto; padding: var(--space); }
.cards { display: grid; gap: var(--space);
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
.card { padding: var(--space); border: 1px solid #ddd; border-radius: 8px; }
img { max-width: 100%; height: auto; }
.btn { background: var(--accent); color: white; border: 0;
padding: 8px var(--space); border-radius: 6px; font: inherit; }
.btn:hover { background: #154c90; }
.btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }A complete small stylesheet for the bakery page. #154c90 is a slightly darker shade of the accent.
2Explore
Custom properties
A custom property is a name you invent, starting with two hyphens, that holds a value: --accent: #1a5fb4;. Define your decisions once on :root, which is the html element, and use them anywhere with var(): background: var(--accent);. Custom properties inherit like text properties, so every element can read the ones defined on the root.
Change a value once on :root and everything that reads it with var() follows.
Tap a choice: the code and the preview change together.
<style>
:root { --accent: #1a5fb4; --space: 16px; }
.card { border: 2px solid var(--accent); padding: var(--space); margin-bottom: var(--space); }
.btn { background: var(--accent); color: white; border: 0; padding: 8px 12px; }
</style>
<div class="card">Sourdough</div>
<div class="card">Rye</div>
<button class="btn">Order</button>Nothing is drawn: there is no visible content.
3Problem
The start of a custom property
How does a custom property name start?
Hint
Two characters, the same one twice.
Solution
Answer: Two hyphens
The two leading hyphens distinguish custom property names from ordinary CSS properties; --accent is a name you define yourself.
4Problem
Using --accent
How do you use the value of --accent?
Hint
A function reads the value.
Solution
Answer: var(--accent)
var() looks up a stored custom-property value and inserts it into the declaration. That gives the declaration the reusable value defined earlier.
5Problem
calc with a custom property
--space is 16px. How many pixels is calc(var(--space) * 1.5)?
px
Hint
16 × 1.5
Solution
Answer: 24 px
16 times 1.5 = 24, the next step of an 8, 16, 24 scale.
6Problem
Cards without a media query
Testing CSS means looking. Open the page in a narrow window (320 pixels for the WCAG reflow check) and a wide one, and look for sideways scrolling and text that touches the edges. Zoom to 200 percent. Press Tab through the page and make sure you can always see where the focus is.
Which layout lets the cards go from one column to many with no media query?
Hint
Think of auto-fill and minmax from the grid unit.
Solution
Answer: repeat(auto-fill, minmax(180px, 1fr))
auto-fill makes as many columns of at least 180px as fit.
7Practice
Style the bakery page
Follow the eight comments. Use the custom property for the button colour.
- body margin 0
- font stack
- menu is flex
- no bullets
- menu gap 16px
- cards grid
- cards gap 16px
- card padding
- card radius 8px
- button accent
- button text white
- uses var(--accent)
- focus style
- media query
<style>
/* 1. :root: --accent: #1a5fb4 */
/* 2. body: margin 0, font-family system-ui, sans-serif */
/* 3. nav ul: a flex row, gap 16px, no bullets */
/* 4. .cards: a grid with a 16px gap */
/* 5. .card: padding 16px, border-radius 8px */
/* 6. .btn: the accent colour from step 1 as background (use the custom property), white text */
/* 7. .btn: a visible outline when it has keyboard focus */
/* 8. a media query (for example min-width: 300px) for .cards */
</style>
<header>
<strong>Corner Bakery</strong>
<nav><ul><li><a href="#">Bread</a></li><li><a href="#">Cakes</a></li><li><a href="#">Visit</a></li></ul></nav>
</header>
<main>
<div class="cards">
<div class="card"><h2>Sourdough</h2><p>Baked at 7 am.</p></div>
<div class="card"><h2>Rye</h2><p>Dark and dense.</p></div>
</div>
<button class="btn" type="button">Order for tomorrow</button>
</main>Hint
Work through the eight comments one by one and look at the preview after each.
A solution
<style>
:root { --accent: #1a5fb4; }
body { margin: 0; font-family: system-ui, sans-serif; }
nav ul { display: flex; gap: 16px; list-style: none; padding: 0; }
.cards { display: grid; gap: 16px; }
.card { padding: 16px; border-radius: 8px; background: #fff4d6; }
.btn { background: var(--accent); color: white; border: 0; padding: 8px 12px; }
.btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
@media (min-width: 300px) {
.cards { grid-template-columns: 1fr 1fr; }
}
</style>
<header>
<strong>Corner Bakery</strong>
<nav><ul><li><a href="#">Bread</a></li><li><a href="#">Cakes</a></li><li><a href="#">Visit</a></li></ul></nav>
</header>
<main>
<div class="cards">
<div class="card"><h2>Sourdough</h2><p>Baked at 7 am.</p></div>
<div class="card"><h2>Rye</h2><p>Dark and dense.</p></div>
</div>
<button class="btn" type="button">Order for tomorrow</button>
</main>8Explain
Common mistakes
<style>
.btn { background: #1a5fb4; color: white; border: 0; padding: 8px 12px; }
.btn:focus { outline: none; }
</style>
<button class="btn">Order</button><style>
.btn { background: #1a5fb4; color: white; border: 0; padding: 8px 12px; }
.btn:focus-visible { outline: 3px solid #1a5fb4; outline-offset: 2px; }
</style>
<button class="btn">Order</button>outline: none hides where keyboard focus is. Style :focus-visible instead of removing the ring.
9
Summary
- Decide colours, fonts and a spacing scale first; store them as custom properties on :root.
- body sets text styles once; flex for the header, grid for cards, fluid images.
- Test at 320px, at 200% zoom, with Tab, and in the developer tools.