Introduction

"HTML5" is the name for the modern HTML standard. Compared with older HTML it added elements that describe what content is, native audio and video, form controls that validate themselves, and a set of browser APIs. Many of these features remove the need for JavaScript libraries that used to be standard on every site.

Each section below has a short example. Paste it into the HTML Runner, run it, then change it to see how it behaves.

Semantic Elements

HTML5 introduced semantic elements that describe the meaning of content, improving accessibility and SEO:

Document Structure Elements

<header>     <!-- Page or section header -->
<nav>        <!-- Navigation links -->
<main>       <!-- Main content area -->
<article>    <!-- Self-contained content -->
<section>    <!-- Thematic grouping -->
<aside>      <!-- Sidebar content -->
<footer>     <!-- Page or section footer -->

Text-Level Semantics

  • <mark> - Highlighted/marked text
  • <time> - Dates and times with machine-readable format
  • <progress> - Progress indicator
  • <meter> - Measurement within a known range
  • <details> and <summary> - Expandable content

Multimedia Elements

HTML5 made embedding audio and video native to the web:

Video Element

<video controls width="640" height="360">
    <source src="video.mp4" type="video/mp4">
    <source src="video.webm" type="video/webm">
    Your browser doesn't support HTML5 video.
</video>

Audio Element

<audio controls>
    <source src="audio.mp3" type="audio/mpeg">
    <source src="audio.ogg" type="audio/ogg">
    Your browser doesn't support HTML5 audio.
</audio>

Canvas for Graphics

<canvas id="myCanvas" width="500" height="400"></canvas>
<script>
    const canvas = document.getElementById('myCanvas');
    const ctx = canvas.getContext('2d');
    ctx.fillStyle = '#667eea';
    ctx.fillRect(50, 50, 200, 100);
</script>

๐Ÿ’ก Provide captions

Add a <track kind="captions"> file to videos with speech. Captions help deaf and hard-of-hearing viewers and anyone watching with the sound off.

Enhanced Form Features

HTML5 introduced new input types and attributes that improve user experience:

New Input Types

<input type="email" placeholder="[email protected]">
<input type="url" placeholder="https://...">
<input type="tel" placeholder="Phone number">
<input type="number" min="0" max="100" step="5">
<input type="range" min="0" max="100">
<input type="date">
<input type="time">
<input type="datetime-local">
<input type="color">
<input type="search" placeholder="Search...">

Form Validation Attributes

  • required - Field must be filled
  • pattern - Regex validation
  • min / max - Numeric range
  • minlength / maxlength - Text length
  • placeholder - Hint text
  • autofocus - Focus on page load
  • autocomplete - Browser autocomplete control

Data Attributes

Custom data attributes let you store extra information on elements:

<article data-author="Jane Doe" data-category="technology" data-id="123">
    <h2>Article Title</h2>
    <p>Content here...</p>
</article>

<script>
    const article = document.querySelector('article');
    console.log(article.dataset.author);    // "Jane Doe"
    console.log(article.dataset.category);  // "technology"
</script>

Responsive Images

HTML5 added features for serving different images based on screen size:

srcset Attribute

<img src="small.jpg"
     srcset="small.jpg 480w,
             medium.jpg 800w,
             large.jpg 1200w"
     sizes="(max-width: 600px) 480px,
            (max-width: 1000px) 800px,
            1200px"
     alt="Responsive image">

Picture Element

<picture>
    <source media="(min-width: 800px)" srcset="large.jpg">
    <source media="(min-width: 400px)" srcset="medium.jpg">
    <img src="small.jpg" alt="Responsive image">
</picture>

Web APIs

HTML5 introduced powerful APIs accessible through JavaScript:

  • Geolocation API - Access user location
  • Web Storage - localStorage and sessionStorage
  • Web Workers - Background processing
  • WebSocket - Real-time communication
  • Drag and Drop - Native drag-drop support
  • History API - URL manipulation

Try It: A Form That Validates Itself

With the input types and validation attributes above, many forms need no JavaScript to check what people type. Paste this into the HTML Runner and select Join without filling anything in. The browser stops the submission and shows a message on the email field; then try an address without an @, an age of 9 and the code ab12.

<form>
  <label for="email">Email</label>
  <input id="email" name="email" type="email" required>

  <label for="age">Age</label>
  <input id="age" name="age" type="number" min="13" max="120">

  <label for="code">Invite code</label>
  <input id="code" name="code" pattern="[A-Za-z]{3}[0-9]{3}"
         title="Three letters followed by three digits">

  <button>Join</button>
</form>

<style>
  label, input { display: block; margin-top: 8px; }
  input:user-invalid { border: 2px solid crimson; }
</style>

The title attribute on a field with a pattern is added to the browser's error message, so use it to explain the expected format. The :user-invalid pseudo-class only matches after someone has interacted with a field, which avoids painting an empty form red before anyone has typed. Built-in validation improves the experience, but it runs in the visitor's browser and can be bypassed, so a server must still check anything it receives.

Native Disclosure Widgets and Dialogs

<details> and <summary> create a show-and-hide section that works with the keyboard and screen readers without any script. <dialog>, supported in all major browsers since 2022, creates a modal window: showModal() opens it on top of the page, keeps keyboard focus inside it and lets Esc close it.

<details>
  <summary>Shipping details</summary>
  <p>Orders ship within two working days.</p>
</details>

<button id="open">Delete project</button>
<dialog id="confirm">
  <form method="dialog">
    <p>Delete this project? This cannot be undone.</p>
    <button value="cancel">Cancel</button>
    <button value="delete">Delete</button>
  </form>
</dialog>

<script>
  const dialog = document.getElementById("confirm");
  document.getElementById("open").addEventListener("click", () => dialog.showModal());
  dialog.addEventListener("close", () => console.log("You chose:", dialog.returnValue));
</script>

A form with method="dialog" closes the dialog when one of its buttons is pressed, and the button's value becomes dialog.returnValue. Open the console in the HTML Runner to see which button was chosen. If the dialog is closed with Esc instead, no button value is recorded.

Conclusion

Start with the semantic elements, because they cost nothing and make pages easier to navigate for everyone. Use native form validation, <details> and <dialog> before reaching for a library, and serve images at sensible sizes with srcset. Check your markup with the HTML validator as you go: it catches unclosed elements, missing alt text and labels that point to the wrong field.

Ready to Put This into Practice?

Paste the form and dialog examples into the HTML Runner and change them to see how each attribute behaves.

Open the HTML Runner โ†’