The web and SQL databases · Part 19 of 36

HTML and CSS: structure and style

Intermediate 16 min

Semantic HTML elements, the DOM, CSS selectors, the box model, flexbox/grid and accessibility basics.

What you will learn

  • You can write a semantic HTML page with headings, lists, links and a form.
  • You can style it with CSS selectors, the box model and flexbox.
  • You can name basic accessibility rules from WCAG 2.2.

Before this: JSON, modules and your first small program

Three languages, three jobs

A web page is built from HTML (structure and meaning), CSS (presentation) and JavaScript (behaviour). Keeping the three separate keeps each simple.

A bare house frame next to the same house painted and decorated, like HTML structure and CSS style

HTML: meaning first

HTML marks up content with elements: an opening tag, content, a closing tag, and optional attributes.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My garden</title>
  <link rel="stylesheet" href="garden.css">
</head>
<body>
  <header><h1>My garden</h1></header>
  <main>
    <h2>Beds</h2>
    <ul>
      <li><a href="/beds/north">North bed</a></li>
    </ul>
    <form action="/beds" method="post">
      <label for="name">Bed name</label>
      <input id="name" name="name" required>
      <button>Add bed</button>
    </form>
  </main>
</body>
</html>

Use elements for what they mean: <h1>–<h6> for headings in order, <button> for actions, <a> for navigation, <nav>, <main>, <article> for page regions. Screen readers, search engines and browser features (keyboard focus, reader mode) rely on that meaning. A <div> that looks like a button is not one: it can't be focused or pressed with the keyboard unless you rebuild all of that.

The browser parses HTML into a tree of objects, the DOM (Document Object Model). JavaScript reads and changes the page through the DOM: document.querySelector("h1").textContent = "Hi".

CSS: rules for presentation

A CSS rule is a selector plus declarations:

main { max-width: 60rem; margin: 0 auto; padding: 1rem; }
.bed-list { display: flex; flex-wrap: wrap; gap: 1rem; }
a:hover, a:focus-visible { text-decoration: underline; }
@media (prefers-color-scheme: dark) { body { background: #1d1f21; color: #eee; } }
  • Selectors pick elements: by tag (main), class (.bed-list), id (#name), state (:hover), or relationship (nav a).
  • When rules conflict, the cascade decides: more specific selectors win, then later rules.
  • Box model: every element is a box of content + padding + border + margin. box-sizing: border-box makes width include padding and border, which is easier to reason about.
  • Layout: flexbox arranges items in one direction (a row of cards), grid in two (a page layout). Media queries and container queries adapt the layout to the screen or the component's size.

Accessibility is part of the job

The W3C's WCAG 2.2 (October 2023) is the reference for accessible web content, and laws such as the European Accessibility Act (applying since 28 June 2025) build on it. Basics you can do from day one:

  • Text alternatives: alt text for meaningful images.
  • Every form field has a <label>.
  • Colour contrast of at least 4.5:1 for normal text.
  • Everything works with the keyboard, with a visible focus indicator.
  • Headings in a logical order; lang set on <html>.

Key points

  • HTML gives structure and meaning, CSS presentation, JavaScript behaviour.
  • Use semantic elements (button, a, nav, main, headings in order); the browser builds the DOM from them.
  • CSS rules = selector + declarations; the cascade and specificity resolve conflicts.
  • Box model: content, padding, border, margin; flexbox for one dimension, grid for two.
  • WCAG 2.2: labels, alt text, 4.5:1 contrast, keyboard access, visible focus.

Flashcards

Question: Which element should you use for a clickable action?
Answer: <button> — it is focusable and keyboard-operable by default.
Question: What is the DOM?
Answer: The tree of objects the browser builds from HTML, which JavaScript can read and change.
Question: What does box-sizing: border-box change?
Answer: Width and height include padding and border.
Question: What is the WCAG minimum contrast for normal text?
Answer: 4.5:1.

Check yourself

Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.

  1. Which element is right for a link to another page?

    • <a href="…">
    • <button>
    • <div onclick>
    • <span>
    Show the answer

    <a href="…">

    Navigation uses anchors; buttons trigger actions.

    Source: HTML Living Standard

  2. Match the CSS layout tool with its job.

    Show the answer

    Flexbox = Arrange items along one direction; Grid = Arrange items in rows and columns; Media query = Change styles for a screen size or preference

    Flexbox is one-dimensional, grid two-dimensional; media queries adapt to the environment.

    Source: MDN, CSS layout

  3. Which help accessibility according to WCAG 2.2?

    • A <label> for every input
    • Visible keyboard focus
    • Light grey text on white for elegance
    • alt text for meaningful images
    Show the answer

    A <label> for every input; Visible keyboard focus; alt text for meaningful images

    Low-contrast text fails the 4.5:1 contrast requirement.

    Source: WCAG 2.2

  4. An element has width 200px, padding 10px on each side and a 1px border, with box-sizing: content-box. How wide is it on screen?

    Show the answer

    222 px

    200 + 2 × 10 + 2 × 1 = 222 px. With border-box it would be 200 px.

    Source: MDN, The box model

Sources

Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.