Code library

Free snippets & functions

Copy, adapt and use them in your own projects.

Intermediate 37 snippets

HTML · Landmarks for screen readersIntermediate
<header>
  <nav aria-label="Main"><a href="/">Home</a></nav>
</header>
<main id="content">
  <section aria-labelledby="news-title">
    <h2 id="news-title">News</h2>
  </section>
  <form role="search" aria-label="Site search">
    <input type="search" name="q" aria-label="Search">
  </form>
</main>
<footer>
  <nav aria-label="Footer"><a href="/privacy">Privacy</a></nav>
</footer>

Label areas when you have more than one of a kind.

HTML · Accessible labelsIntermediate
<label for="email">Email</label>
<input id="email" type="email" aria-describedby="email-hint">
<p id="email-hint">We never share your address.</p>

<button type="button" aria-label="Close dialog">&times;</button>

<label>
  Search
  <input type="search" name="q">
</label>

Every control needs a name that a screen reader can read.

HTML · A file upload formIntermediate
<form action="/upload" method="post" enctype="multipart/form-data">
  <label for="doc">Choose files (PDF, max 5)</label>
  <input id="doc" name="docs[]" type="file" accept=".pdf,application/pdf" multiple required>
  <button type="submit">Upload</button>
</form>

enctype is required for files.

HTML · Suggestions with datalistIntermediate
<label for="browser">Browser</label>
<input id="browser" name="browser" type="text" list="browsers">
<datalist id="browsers">
  <option value="Chrome">
  <option value="Firefox">
  <option value="Safari">
  <option value="Edge">
</datalist>

The user can type or pick.

HTML · autocomplete and inputmodeIntermediate
<form>
  <input type="text" name="name" autocomplete="name" aria-label="Full name">
  <input type="email" name="email" autocomplete="email" inputmode="email" aria-label="Email">
  <input type="tel" name="tel" autocomplete="tel" inputmode="tel" aria-label="Phone">
  <input type="text" name="otp" autocomplete="one-time-code" inputmode="numeric" pattern="[0-9]{6}" aria-label="Code">
  <input type="password" name="pw" autocomplete="current-password" aria-label="Password">
</form>

Faster filling, and the right keyboard on phones.

HTML · Built-in validation attributesIntermediate
<form>
  <label for="age">Age (18-65)</label>
  <input id="age" name="age" type="number" min="18" max="65" required>

  <label for="zip">Postal code</label>
  <input id="zip" name="zip" type="text" inputmode="numeric" pattern="[0-9]{5}" title="Five digits" required>

  <label for="site">Website</label>
  <input id="site" name="site" type="url" placeholder="https://">

  <button type="submit">Send</button>
</form>

Let the browser validate before you do it again on the server.

HTML · defer, async and moduleIntermediate
<!-- Runs after the HTML is parsed, in order. Best default. -->
<script src="app.js" defer></script>

<!-- Runs as soon as it is downloaded. For independent scripts (analytics). -->
<script src="analytics.js" async></script>

<!-- Modern module, deferred automatically. -->
<script type="module" src="main.js"></script>
<script nomodule src="legacy.js"></script>

How scripts load matters for speed.

HTML · Preload and preconnectIntermediate
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/css/critical.css" as="style">
<link rel="dns-prefetch" href="https://cdn.example.com">

Tell the browser early about files it will need.

HTML · Lazy loading images and iframesIntermediate
<img src="hero.jpg" alt="Hero" width="1200" height="600" fetchpriority="high">
<img src="gallery-1.jpg" alt="Gallery photo 1" width="400" height="300" loading="lazy" decoding="async">
<iframe src="https://www.youtube-nocookie.com/embed/VIDEO_ID" title="Intro video" width="560" height="315" loading="lazy" allowfullscreen></iframe>

Delay offscreen files and prioritise the main image.

HTML · srcset and sizesIntermediate
<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
  sizes="(min-width: 1000px) 33vw, (min-width: 600px) 50vw, 100vw"
  alt="A mountain lake at sunrise"
  width="800" height="533" loading="lazy">

Several widths of the same image. The browser downloads only what fits.

<picture>
  <source media="(min-width: 900px)" srcset="hero-wide.avif 1600w, hero-wide.webp 1600w" sizes="100vw">
  <source media="(max-width: 899px)" srcset="hero-small.webp">
  <img src="hero-small.jpg" alt="Our team at work" width="800" height="500" loading="lazy" decoding="async">
</picture>

The browser picks the best file.

HTML · An accessible formIntermediate
<form action="save.php" method="post">
  <label for="name">Full name</label>
  <input id="name" name="name" type="text" required autocomplete="name">

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

  <label for="msg">Message</label>
  <textarea id="msg" name="msg" rows="4" required></textarea>

  <button type="submit">Send</button>
</form>

Every input has a label, the right type and basic validation.

Advanced 33 snippets

<!-- ===== HEADER ===== -->
<header class="site-header">
  <a href="/">Brand</a>
</header>

<!-- ===== MAIN ===== -->
<main id="content">
  <!-- Hero -->
  <section class="hero"><h1>Welcome</h1></section>
  <!-- Features -->
  <section class="features"><h2>Features</h2></section>
</main>

<!-- ===== FOOTER ===== -->
<footer class="site-footer"><p>&copy; 2026</p></footer>

Clear markers help when pages are assembled from parts.

HTML · Prevent layout shiftAdvanced
<img src="a.jpg" alt="Product photo" width="800" height="600" style="max-width:100%;height:auto">
<div style="min-height:250px"><!-- ad slot --></div>
<iframe title="Map" src="about:blank" width="600" height="400" style="max-width:100%"></iframe>

Reserve space for images, ads and embeds.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Moved</title>
  <meta http-equiv="refresh" content="0; url=https://example.com/new-page">
  <link rel="canonical" href="https://example.com/new-page">
  <meta name="robots" content="noindex">
</head>
<body>
  <p>This page moved to <a href="https://example.com/new-page">the new address</a>.</p>
</body>
</html>

Prefer an HTTP 301 header. Use this only on static hosting.

<form>
  <input type="text" name="user" autocapitalize="none" autocorrect="off" spellcheck="false" autocomplete="username" aria-label="Username">
  <input type="text" name="city" autocapitalize="words" enterkeyhint="next" aria-label="City">
  <input type="search" name="q" enterkeyhint="search" inputmode="search" aria-label="Search">
  <textarea name="note" spellcheck="true" aria-label="Note"></textarea>
</form>

Small attributes that make typing easier.

HTML · A well-structured long pageAdvanced
<nav aria-label="On this page">
  <ol>
    <li><a href="#intro">Introduction</a></li>
    <li><a href="#setup">Setup</a></li>
  </ol>
</nav>
<article>
  <section id="intro"><h2>Introduction</h2><p>...</p></section>
  <section id="setup"><h2>Setup</h2><p>...</p></section>
</article>

Table of contents with working anchors.

HTML · A no-JavaScript fallbackAdvanced
<form action="/search" method="get" id="search-form">
  <input type="search" name="q" aria-label="Search">
  <button type="submit">Search</button>
</form>

<noscript>
  <p>Live search needs JavaScript. You can still use the button above.</p>
</noscript>
<script>
  document.documentElement.classList.add('js');
</script>

The page still works without scripts.

<section aria-labelledby="pro-title">
  <h2 id="pro-title">Pro plan</h2>
  <dl>
    <dt>Price</dt><dd>$12 per month</dd>
    <dt>Projects</dt><dd>Unlimited</dd>
    <dt>Support</dt><dd>Email, 24 hours</dd>
  </dl>
</section>

A semantic way to list properties.

HTML · Microdata for a productAdvanced
<div itemscope itemtype="https://schema.org/Product">
  <h2 itemprop="name">Notebook</h2>
  <img itemprop="image" src="nb.jpg" alt="Notebook" width="300" height="300">
  <div itemprop="offers" itemscope itemtype="https://schema.org/Offer">
    <span itemprop="priceCurrency" content="USD">$</span><span itemprop="price" content="9.99">9.99</span>
    <link itemprop="availability" href="https://schema.org/InStock">In stock
  </div>
</div>

Structured data inside the visible HTML.

<video controls width="720" preload="metadata" poster="poster.jpg">
  <source src="lesson.mp4" type="video/mp4">
  <track kind="captions" src="en.vtt" srclang="en" label="English" default>
  <track kind="captions" src="ur.vtt" srclang="ur" label="Urdu">
  <track kind="chapters" src="chapters.vtt" srclang="en" label="Chapters">
</video>

Subtitles and chapter markers.

HTML · Site search formAdvanced
<search>
  <form action="/search" method="get">
    <label for="q">Search the site</label>
    <input id="q" name="q" type="search" required minlength="2" autocomplete="off">
    <button type="submit">Search</button>
  </form>
</search>

A GET form that produces shareable URLs.

HTML · Advanced list attributesAdvanced
<ol reversed>
  <li>Third place</li>
  <li>Second place</li>
  <li>First place</li>
</ol>

<ol start="5" type="A">
  <li>Starts at E</li>
  <li value="10">Jumps to J</li>
  <li>K</li>
</ol>

Control numbering without CSS.

HTML · The inert attributeAdvanced
<main id="app" inert>
  <h1>Page behind the modal</h1>
  <a href="/x">Not focusable while inert</a>
</main>
<dialog open aria-labelledby="mt"><h2 id="mt">Modal</h2><button type="button">Close</button></dialog>

Block interaction with the page behind a modal.

<iframe title="Preview" sandbox srcdoc="&lt;h1&gt;Preview&lt;/h1&gt;&lt;p&gt;Scripts do not run here.&lt;/p&gt;" width="400" height="200"></iframe>

Show untrusted HTML safely.

HTML · Inline SVG iconsAdvanced
<svg width="0" height="0" style="position:absolute" aria-hidden="true">
  <symbol id="i-check" viewBox="0 0 24 24"><path d="M5 13l4 4L19 7" fill="none" stroke="currentColor" stroke-width="2"/></symbol>
</svg>

<button type="button">
  <svg width="16" height="16" aria-hidden="true"><use href="#i-check"></use></svg>
  Saved
</button>

Define once, reuse many times.

HTML · Art direction with pictureAdvanced
<picture>
  <source media="(max-width: 600px)" srcset="hero-portrait-600.jpg 600w, hero-portrait-1200.jpg 1200w" sizes="100vw">
  <source media="(min-width: 601px)" srcset="hero-wide-1200.jpg 1200w, hero-wide-2400.jpg 2400w" sizes="100vw">
  <img src="hero-wide-1200.jpg" alt="Our office at sunset" width="1200" height="500">
</picture>

A tall crop on phones, a wide image on desktop.

HTML · Contact form with a honeypotAdvanced
<form action="/contact" method="post">
  <input type="hidden" name="csrf" value="TOKEN_FROM_SERVER">

  <label for="c-name">Name</label>
  <input id="c-name" name="name" type="text" required maxlength="80" autocomplete="name">

  <label for="c-msg">Message</label>
  <textarea id="c-msg" name="message" required maxlength="2000" rows="6"></textarea>

  <div style="position:absolute;left:-9999px" aria-hidden="true">
    <label for="c-web">Leave this empty</label>
    <input id="c-web" name="website" type="text" tabindex="-1" autocomplete="off">
  </div>

  <button type="submit">Send</button>
</form>

A hidden field that bots fill but people never see.

HTML · Multi-step form markupAdvanced
<form id="wizard" novalidate>
  <progress value="1" max="3" aria-label="Step 1 of 3"></progress>

  <fieldset>
    <legend>Step 1: Account</legend>
    <label for="w-email">Email</label>
    <input id="w-email" name="email" type="email" required>
    <button type="button" data-next>Next</button>
  </fieldset>

  <fieldset hidden>
    <legend>Step 2: Profile</legend>
    <label for="w-name">Name</label>
    <input id="w-name" name="name" type="text" required>
    <button type="button" data-prev>Back</button>
    <button type="button" data-next>Next</button>
  </fieldset>
</form>

Each step is a fieldset with a legend.

HTML · Sortable table markupAdvanced
<table>
  <thead>
    <tr>
      <th scope="col" aria-sort="ascending"><button type="button">Name</button></th>
      <th scope="col" aria-sort="none"><button type="button">Age</button></th>
    </tr>
  </thead>
  <tbody>
    <tr><td>Ali</td><td>30</td></tr>
    <tr><td>Sara</td><td>28</td></tr>
  </tbody>
</table>

Sort buttons inside headers with the right state.