Skip to the content.

Lesson 5: Style It and Make It Accessible

Course home Previous: Lesson 4 Next: Lesson 6

Time: about 60 minutes.

Goals

By the end of this lesson you will be able to:

Concepts

CSS in five ideas

  1. Selectors pick elements: button, .habit, #habit-name, input[type="text"].
  2. Properties set how they look: color, padding, border-radius.
  3. The box model: every element is a box with content, padding, border and margin. box-sizing: border-box makes widths include padding and border, which is easier to reason about.
  4. Layout: display: flex lines things up in a row or column; display: grid makes rows and columns.
  5. Custom properties: --accent: #2f6f4f; defined once on :root and used as var(--accent). Change one value, change the whole theme.

Mobile first

Write styles for a narrow phone screen first, then add @media (min-width: ...) rules for bigger screens if needed. A single centred column with a max-width often works everywhere with no media queries at all, which is what Tiny Habits does.

Dark mode

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --text: #f1f1ec;
  }
}

If every colour is a custom property, dark mode is just a second set of values.

Accessibility basics for every page

The W3C introduction to web accessibility and MDN’s accessibility section explain why each of these matters.

AI CSS smells

Smell Why it hurts Ask instead
The same colour hex code repeated 20 times Changing the theme means editing everywhere “Use custom properties on :root for all colours.”
!important everywhere Makes later changes fight each other “Avoid !important.”
Fixed widths like width: 800px Breaks on phones “Use max-width and flexible units.”
outline: none on buttons Keyboard users get lost “Keep a visible :focus-visible style.”
Pulling in a CSS framework from a CDN Extra dependency, harder to review “Plain CSS in styles.css only.”
Inline style="..." attributes in HTML Scattered styles; blocked by a strict CSP later “All styles in styles.css.”

Walkthrough

Step 1: Plan the look

Read index.html. I want to style Tiny Habits with a calm, minimal look.
Plan the CSS before writing it:
- all colours and the border radius as custom properties on :root
- a dark-mode version using prefers-color-scheme
- a single centred column, max width about 36rem, comfortable padding
- the input and Add button on one row; input stretches, button stays
  readable
- clear visible focus outlines
- a style for an error message (red-ish, readable in both themes)
- each habit as a card (we will add habits in the next lesson)
Constraints: plain CSS in a new styles.css linked from index.html, no
frameworks, no inline styles, no !important.
Describe the plan and the colour palette first, including whether the
text colours meet 4.5:1 contrast on their backgrounds.

Check the plan mentions every bullet. Then approve.

Step 2: Build and look

After the agent creates styles.css and adds the <link rel="stylesheet" href="styles.css">, start your server:

python3 -m http.server 8000

Open http://localhost:8000 and check:

Step 3: Test with your keyboard only

Put the mouse away.

  1. Press Tab. Can you see where focus is?
  2. Tab to the input, type, Tab to the button, press Enter.
  3. Press Shift+Tab to go back.

If focus is hard to see:

The keyboard focus is hard to see on the Add button. Add a clear
:focus-visible outline using the accent colour, with a small offset.
Change only styles.css.

Step 4: Zoom and contrast

Step 5: Prepare styles for the habit cards

The list is empty now, so add a temporary example to check the card style. Ask:

Temporarily add one example <li class="habit"> to index.html with a
checkbox, a name, a Delete button and a stats line, so I can check the card
styling. Mark it clearly with an HTML comment "TEMPORARY". Style the
checkbox with accent-color and make the Delete button look secondary.

Look at it in both themes. Then ask the agent to remove the temporary example (or delete it yourself) before you commit:

git diff            # check only styles.css and the link tag changed
git add index.html styles.css
git commit -m "Add responsive styles with dark mode and focus outlines"

Step 6: Compare with the reference

Open project/styles.css. Notice:

What good output looks like

Checklist

Mini-quiz

  1. Why define colours as custom properties?
  2. What does box-sizing: border-box change?
  3. What is the minimum contrast ratio for normal body text under WCAG AA?
  4. The AI wrote button:focus { outline: none; }. What is the problem?
  5. Why does it help that the reference project has no inline style attributes?
Answers 1. You define each colour once and reuse it, so theming (like dark mode) and changes happen in one place. 2. Width and height include padding and border, so a box set to 100% width does not overflow when you add padding. 3. 4.5:1. 4. Keyboard users can no longer see which element has focus. Use `:focus-visible` with a clear outline instead. 5. Styles stay in one reviewable file, and the page can use a strict Content Security Policy that blocks inline styles (Lesson 11).

Assignment

  1. Add a third theme colour for “done” days and use it in the seven-day strip style (you will see it in Lesson 6).
  2. Make the Add button at least 44px tall on all screens. Verify with DevTools.
  3. Ask the agent: “List anything in styles.css that a beginner might find confusing, and explain each in one sentence.” Add a short comment above the two most confusing rules.
  4. Commit each change separately with clear messages.

Next: Lesson 6: Add interactivity with JavaScript