edubun
English
1 / 9

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 questions

You 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.

--accent
--space
<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>
Preview

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.

Checks
  • 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
Your code
<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>
Preview
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

Mistake
<style>
  .btn { background: #1a5fb4; color: white; border: 0; padding: 8px 12px; }
  .btn:focus { outline: none; }
</style>
<button class="btn">Order</button>
Fixed
<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.

Back to the courseCSS
Full text · The whole lesson as one text

Start with decisions, not rules

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. CSS has a feature made for this, custom properties.

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. Changing the accent colour of the whole site now means editing one line. You can also redefine a property inside a media query, for example to swap colours for prefers-color-scheme: dark.

: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.

Reading the stylesheet from top to bottom

The stylesheet above uses something from almost every unit. The root holds the decisions. body sets the font, line height and colours once, and inheritance carries them into every paragraph. The header is a wrapping flex row, so the menu drops below the name on a narrow phone instead of being squeezed. main has a max-width and margin: 0 auto, which keeps lines readable and centres the content on wide screens. The cards use the auto-fill grid pattern, so they reflow from one column to several with no media query, and images are fluid. The button gets a hover colour and, just as important, a visible focus outline for keyboard users.

Check it on real screens

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. Check the contrast of text and buttons in the developer tools. Turn on the dark mode or reduced motion setting of your system if you styled for them. Browser developer tools let you change any value live and see the box model of any element, which is the fastest way to answer the question of why something is where it is.

Choose two cards that should use the same spacing value. Find the declarations that read their shared custom property, then change that value once and compare both cards. If only one changes, check whether the other uses a fixed value or a more local definition. That difference may be intentional, but it should be explainable. Restore the value and repeat the check in a narrow window before adding more decorative rules.

From here, the same pieces scale up to any site. Bigger projects split the stylesheet into files, name classes by what a component is (card, button, site header), and keep a short list of shared custom properties. But the rules underneath are the ones in these nine units.

Key points
  • 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.

Practice questions from Style a small page on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (5)
  • W3C CSS Working Group specifications: CSS Snapshot 2025, Selectors Level 4, CSS Cascading and Inheritance Level 5, CSS Color Level 4, CSS Values and Units Level 4, CSS Box Model Level 3, CSS Box Sizing Level 3, CSS Fonts Level 4, CSS Flexible Box Layout Level 1, CSS Grid Layout Level 2, Media Queries Level 4 and Level 5 (w3.org/TR/, checked 2026-10-17)
  • MDN Web Docs, Learn web development: CSS styling basics and CSS layout, and the CSS reference (developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics, .../Core/CSS_layout, developer.mozilla.org/en-US/docs/Web/CSS, checked 2026-10-17)
  • W3C, CSS history and CSS1 Recommendation of 17 December 1996 (w3.org/Style/CSS20/history.html, w3.org/press-releases/1996/css1-rec, checked 2026-10-17)
  • W3C WCAG 2.2, success criteria 1.4.3 Contrast (Minimum)
  • 1.4.8 Visual Presentation, 1.4.10 Reflow, 1.4.12 Text Spacing (w3.org/TR/WCAG22/, checked 2026-10-17).

Checked: 2026-10-17