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:
- Get the AI to write clean, maintainable CSS using custom properties (variables).
- Make a layout that works on phones first and scales up.
- Add dark mode that follows the user’s system setting.
- Check accessibility yourself with a keyboard, zoom and a contrast checker.
- Spot the CSS habits that make AI-generated styles hard to maintain.
Concepts
CSS in five ideas
- Selectors pick elements:
button,.habit,#habit-name,input[type="text"]. - Properties set how they look:
color,padding,border-radius. - The box model: every element is a box with content, padding, border and margin.
box-sizing: border-boxmakes widths include padding and border, which is easier to reason about. - Layout:
display: flexlines things up in a row or column;display: gridmakes rows and columns. - Custom properties:
--accent: #2f6f4f;defined once on:rootand used asvar(--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
- Contrast: text must stand out from its background. WCAG asks for a contrast ratio of at least 4.5:1 for normal text.
- Focus visible: keyboard users must see where they are. Never remove outlines without replacing them.
- Labels: every input has a visible label.
- Touch targets: buttons big enough to tap, roughly 44 by 44 CSS pixels.
- Do not rely on colour alone: a green “done” box should also have text or a tick.
- Respect motion preferences: wrap animations in
@media (prefers-reduced-motion: no-preference). - Zoom: the page must still work at 200% zoom.
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:
- Light theme looks clean.
- Switch your system to dark mode (System Settings on macOS, Settings, Personalisation, Colours on Windows). The page should switch too.
- Make the window very narrow (about phone width). Nothing should overflow sideways.
Step 3: Test with your keyboard only
Put the mouse away.
- Press Tab. Can you see where focus is?
- Tab to the input, type, Tab to the button, press Enter.
- 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
- Zoom to 200% (Ctrl or Cmd and +). Everything should still fit and be usable.
- Check contrast: in Chrome DevTools, inspect a text element and look at the colour picker, which shows the contrast ratio. Or ask the agent to calculate it, then verify one pair with DevTools yourself.
- Try the WAVE checker on your deployed site later in Lesson 12.
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:
- Every colour is a custom property and redefined for dark mode.
:focus-visibleuses a thick outline with an offset.- The seven-day strip uses
grid-template-columns: repeat(7, 1fr). - The only transition sits inside
prefers-reduced-motion: no-preference. overflow-wrap: anywherestops a very long habit name from breaking the layout.
What good output looks like
- One
styles.css, linked once fromindex.html, no inline styles. - Colours defined once as custom properties, with a dark-mode block.
- Layout works from phone width to desktop without horizontal scrolling.
- Visible focus on every interactive element.
- No
!important, no CSS frameworks, no fixed pixel widths on containers.
Checklist
- Styles live in
styles.cssand use custom properties. - Dark mode follows the system setting.
- Narrow window: no sideways scroll.
- Keyboard only: I can reach and use everything and always see focus.
- 200% zoom still works.
- I checked at least one contrast ratio myself.
- Temporary example removed; work committed.
Mini-quiz
- Why define colours as custom properties?
- What does
box-sizing: border-boxchange? - What is the minimum contrast ratio for normal body text under WCAG AA?
- The AI wrote
button:focus { outline: none; }. What is the problem? - Why does it help that the reference project has no inline
styleattributes?
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
- Add a third theme colour for “done” days and use it in the seven-day strip style (you will see it in Lesson 6).
- Make the Add button at least 44px tall on all screens. Verify with DevTools.
- 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.
- Commit each change separately with clear messages.