HTML semantics & forms

Landmarks, headings, native form controls and validation, when a button is not a div. Semantics are free accessibility and free SEO.

About 27 min read3-question knowledge check

The mental model

HTML is not a styling language and it is not a template language. It is a contract you sign with every user agent that will ever read your page — browsers, screen readers, search crawlers, Safari Reader, password managers, translation engines, and the "share to" preview in a chat app. You write one document; a dozen machines interpret it. Semantics are the vocabulary of that contract.

The analogy that sticks: think of a building. A div is a blank wall panel — it holds things up, it means nothing. A nav, a main, a button, a label are the signage: EXIT, RECEPTION, PULL, PUSH. A sighted mouse user navigates by looking at the furniture and never reads the signs. Everyone else — a screen-reader user jumping between landmarks, a keyboard user tabbing, a crawler building an index, a password manager deciding which field is the username — reads only the signs. Build the whole building out of blank panels and it still looks fine in the photograph, and is unusable in the dark.

What breaks without semantics is specific, not vague. A div styled as a button is not focusable, does not fire on Enter or Space, is not announced as a button, cannot be found by a screen reader's "list all buttons" command, and does not submit a form. Recreating those five behaviours takes tabindex="0", role="button", a keydown handler for two keys, an aria-disabled story, and a click handler that also has to handle the space-key scroll — roughly twenty lines to reproduce, imperfectly, what the four characters button give away free. Semantics are the cheapest accessibility, the cheapest SEO, and the cheapest resilience you will ever buy.

The second half of the contract is forms. Forms are the only part of the web platform where the browser ships a complete, free, offline-capable application: validation, error messaging in the user's own language, autofill, keyboard behaviour, mobile keyboard selection, and password-manager integration. Every one of those features is keyed off attributes most people never set.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision