HTML
The structure of the web — document anatomy, semantic elements, text and links, forms and validation, media, metadata and SEO, and how the browser turns HTML into a DOM.
HTML (HyperText Markup Language) describes the structure and meaning of a web page. CSS handles presentation and JavaScript handles behaviour; HTML is the foundation both build on. The browser parses it into the DOM tree (DOM and events).
Document anatomy
<!DOCTYPE html> <!-- standards mode -->
<html lang="en"> <!-- language: screen readers, translation, hyphenation -->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <!-- responsive on mobile -->
<title>Orders · Acme</title> <!-- tab title, bookmarks, search results -->
<meta name="description" content="Track and manage your Acme orders.">
<link rel="stylesheet" href="/styles.css">
<script src="/app.js" defer></script> <!-- defer: download in parallel, run after parsing -->
</head>
<body>
<!-- visible content -->
</body>
</html>An element is a start tag, content and end tag: <p class="lead">Hello</p>. Attributes configure elements. Void elements have no content or end tag: <img>, <input>, <br>, <meta>, <link>.
Semantic HTML
Use elements for what content is, not how it should look. Semantics drive accessibility (screen readers navigate by landmarks and headings), SEO, browser features (reader mode) and maintainability.
<body>
<header>
<nav aria-label="Main">
<a href="/">Home</a> <a href="https://example.com/orders">Orders</a>
</nav>
</header>
<main> <!-- one per page: the primary content -->
<article> <!-- self-contained: a post, a card, a product -->
<h1>Shipping update</h1>
<p>Posted <time datetime="2026-12-01">1 December</time></p>
<section>
<h2>What changed</h2>
<p>…</p>
</section>
<figure>
<img src="map.png" alt="Map of new delivery zones covering the north of the city">
<figcaption>New delivery zones</figcaption>
</figure>
</article>
<aside>Related posts</aside> <!-- tangential content -->
</main>
<footer>© 2026 Acme</footer>
</body>| Instead of | Use | Why |
|---|---|---|
<div onclick> | <button> | Keyboard focus, Enter/Space activation, role announced — for free |
<span onclick> that navigates | <a href> | Middle-click, open in new tab, crawlable |
<div class="header"> | <header>, <nav>, <main>, <footer> | Landmarks for assistive tech |
<b>, <i> for meaning | <strong> (importance), <em> (emphasis) | Conveys meaning, not just style |
| Bold text as a heading | <h1>–<h6> in order | Document outline; screen-reader navigation |
Text, links and lists
<h2>Steps</h2>
<ol> <!-- ordered; <ul> for unordered -->
<li>Install with <code>npm i</code></li>
<li>Read the <a href="https://example.com/docs/setup">setup guide</a></li>
</ol>
<a href="https://example.com" target="_blank" rel="noopener noreferrer">External</a>
<a href="#pricing">Jump to pricing</a> <!-- fragment link to id="pricing" -->
<a href="mailto:help@example.com">Email us</a>
<dl><dt>Latency</dt><dd>Time for one request</dd></dl>
<blockquote cite="…">Quoted text</blockquote>
<pre><code>preformatted code</code></pre>Forms
Forms are how users send data. Native elements give validation, keyboard support, autofill and mobile keyboards without JavaScript:
<form action="/signup" method="post">
<label for="email">Email</label>
<input id="email" name="email" type="email" required autocomplete="email">
<label for="pw">Password</label>
<input id="pw" name="password" type="password" minlength="12" required
autocomplete="new-password" aria-describedby="pw-hint">
<p id="pw-hint">At least 12 characters.</p>
<fieldset>
<legend>Plan</legend>
<label><input type="radio" name="plan" value="free" checked> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
</fieldset>
<label for="country">Country</label>
<select id="country" name="country">
<option value="in">India</option>
<option value="us">United States</option>
</select>
<label><input type="checkbox" name="terms" required> I accept the terms</label>
<button type="submit">Create account</button>
</form>- Every input needs a label (click target + accessible name).
typematters:email,tel,url,number,date,searchgive validation and the right mobile keyboard.- Validation attributes:
required,min,max,minlength,pattern. Always validate on the server too — client validation is a convenience, not security (Web security). method="get"puts fields in the URL (searches, filters);postsends them in the body (creating or changing data).
Media
<img src="cat-800.jpg"
srcset="cat-400.jpg 400w, cat-800.jpg 800w, cat-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 50vw"
width="800" height="600" <!-- reserves space: no layout shift -->
loading="lazy" decoding="async"
alt="Grey cat asleep on a keyboard">
<picture> <!-- art direction / modern formats -->
<source srcset="hero.avif" type="image/avif">
<source srcset="hero.webp" type="image/webp">
<img src="hero.jpg" alt="…">
</picture>
<video src="demo.mp4" controls muted playsinline poster="demo.jpg">
<track kind="captions" src="demo.en.vtt" srclang="en" label="English">
</video>alt text describes the image's purpose; decorative images get alt="" so screen readers skip them (Accessibility).
Metadata, SEO and sharing
<link rel="canonical" href="https://acme.com/orders">
<meta property="og:title" content="Track your orders"> <!-- link previews -->
<meta property="og:image" content="https://acme.com/og.png">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<script type="application/ld+json">{ "@context": "https://schema.org", "@type": "Product", "name": "Widget" }</script>Search engines rely on: a unique <title> and description, real headings, descriptive link text (not "click here"), crawlable links, fast pages, and structured data.
How the browser reads HTML
The HTML parser is forgiving: unclosed tags and misnested elements are repaired per the spec rather than rejected. It builds the DOM incrementally while downloading; a classic <script> without defer/async blocks parsing until it downloads and runs, which is why scripts go at the end or use defer (Browser rendering).
| Script attribute | Downloads | Executes |
|---|---|---|
| none | Blocks parsing | Immediately |
async | In parallel | As soon as downloaded (order not guaranteed) |
defer | In parallel | After parsing, in document order |
type="module" | In parallel | Deferred by default |
- HTML describes structure and meaning; the browser parses it into the DOM.
- Use semantic elements (header, nav, main, article, button, a) — accessibility, SEO and behaviour come free.
- Forms: label every input, use proper input types and native validation, and always revalidate on the server.
- Responsive images (srcset, sizes, width/height, lazy loading) and meaningful alt text.
- Use defer or module scripts so JavaScript doesn't block parsing.