Document structure
Every HTML document follows the same skeleton. The doctype must come first, followed by a single root html element containing exactly one head and one body.
lang on the html element — it drives screen reader pronunciation and browser translation prompts.Metadata & head
Tags inside <head> describe the document rather than render visible content.
| Tag / attribute | Purpose |
|---|---|
meta charset="utf-8" | Character encoding — should be the first tag in head. |
meta name="viewport" | width=device-width, initial-scale=1 for responsive layouts. |
meta name="description" | Search engine snippet text, ~150–160 characters. |
link rel="canonical" | Points search engines to the preferred URL for duplicate content. |
meta property="og:*" | Open Graph tags controlling social share previews (title, image, description). |
link rel="icon" | Favicon reference — see the Favicon Package Generator for a full set. |
Text content
Use the most specific element available rather than styling a generic div or span.
| Element | Use for |
|---|---|
h1–h6 | Document outline. One h1 per page; never skip levels for styling reasons. |
p | Paragraphs of text. |
ul / ol / li | Unordered and ordered lists. |
strong / em | Semantic importance and emphasis (not just bold/italic styling). |
blockquote / cite | Quoted content and its source. |
code / pre | Inline code and preformatted code blocks. |
abbr | Abbreviations, with a title attribute for the expansion. |
Semantic elements
Semantic landmarks give assistive technology and search engines a reliable outline of the page.
| Element | Represents |
|---|---|
header | Introductory content for a page or section (often a logo + nav). |
nav | A block of primary navigation links. |
main | The dominant content of the document. Exactly one per page. |
article | Self-contained, independently distributable content (a post, a card). |
section | A thematic grouping of content, usually with its own heading. |
aside | Content tangentially related to the main content (sidebars, pull quotes). |
footer | Footer for its nearest sectioning ancestor. |
Forms & inputs
Every form control needs a programmatic label. Prefer wrapping or a matching for/id pair over placeholder-only labeling.
| Input type / element | Use for |
|---|---|
input type="email" | Email addresses — brings up an email keyboard on mobile. |
input type="tel" | Phone numbers — numeric keypad on mobile, no format enforcement. |
input type="number" | Numeric values with spinners. Use inputmode for numeric-only text. |
select / option | Choosing one value from a short, known list. |
textarea | Multi-line free text. |
fieldset / legend | Groups related controls (e.g. a radio button set) with a shared label. |
required, pattern, minlength | Built-in client-side validation attributes. |
Media & embeds
| Element / attribute | Use for |
|---|---|
img alt="" | Images. alt is required — use an empty string for purely decorative images. |
picture / source | Responsive images: different files per viewport or format. |
video / audio | Native media playback with controls, autoplay, loop, muted. |
track kind="captions" | Captions/subtitles for a video element. |
iframe | Embedding another document. Always set a descriptive title. |
Global attributes
These work on virtually every HTML element, regardless of tag.
| Attribute | Purpose |
|---|---|
id | Unique identifier, used by CSS, JS, and anchor links. |
class | One or more style/behavior hooks, space-separated. |
data-* | Custom data attributes for scripting, e.g. data-state. |
hidden | Removes the element from rendering and the accessibility tree. |
tabindex | 0 adds an element to natural tab order; -1 makes it focusable via script only. |
contenteditable | Makes an element's content directly editable by the user. |
Accessibility basics
Reach for semantic HTML first — it comes with accessibility built in. Use ARIA only to patch gaps semantic elements can't cover.
- Every interactive control needs an accessible name (visible label,
aria-label, oraria-labelledby). - Use
alton images, notaria-label—altis the correct, universally supported mechanism. aria-live="polite"announces dynamic content updates (toasts, form errors) without stealing focus.- Never remove focus outlines without providing a visible replacement.
- Check color contrast: 4.5:1 for normal text, 3:1 for large text, per WCAG AA.
button already has the right role, state, and keyboard behavior — a styled div with role="button" does not, unless you reimplement all of it yourself.