Reference

HTML, CSS & browser APIs

Look up semantic HTML, forms, CSS layout, events, DOM operations and common browser APIs.

Scope and conventions

Modern browser platform examples, independent of a framework. HTML/CSS excerpts belong in the appropriate document/style context; DOM snippets assume a browser and named elements/data; TypeScript annotations clarify DOM types and need compilation or removal in plain JavaScript. Check target-browser support for newer features.

HTML & semantics

Declare document language and viewport

Web platform

The document language helps assistive technology and language processing. A viewport declaration allows responsive CSS to work with the device viewport.

Fragment

Web platform · Declare document language and viewport
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Example</title>
</head>
<body><main><h1>Example</h1></main></body>
</html>

Use native elements for their actual job

Web platform

Headings describe structure, landmarks identify regions, anchors navigate and buttons perform actions. A generic div does not acquire native keyboard behavior by looking like a button.

Fragment

Web platform · Use native elements for their actual job
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<nav aria-label="Main"><a href="/items">Items</a></nav>
<main>
  <h1>Items</h1>
  <button type="button">Refresh</button>
</main>

Describe meaningful images

Web platform

alt supplies an image’s text alternative. An empty alt marks a decorative image; a missing alt and an intentionally empty alt have different meanings.

Fragment

Web platform · Describe meaningful images
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<img src="chart.png" alt="Orders rose from 20 to 35">
<img src="decoration.svg" alt="">

Describe the image’s purpose in context, not merely its filename.

Insert text without parsing it as HTML

Web platform

textContent creates text. innerHTML parses markup and can introduce injection when content is untrusted. HTML escaping, URL encoding and JavaScript string escaping are different operations.

Fragment

Web platform · Insert text without parsing it as HTML
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const output = document.querySelector("#output");
if (output) output.textContent = message;

Choose classic defer or module loading

Web platform

Deferred classic scripts and non-async module scripts run after parsing, in document order. Module scripts are deferred by default and resolve their import graph first. async scripts run as soon as they are ready, without ordering.

Fragment

Web platform · Choose classic defer or module loading
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<script defer src="classic.js"></script>
<script type="module" src="app.js"></script>

Top-level await lets a module finish later than scripts that start after it: the order of starting is not the order of completion.