Code library
Free snippets & functions
Copy, adapt and use them in your own projects.
<!-- ===== 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>© 2026</p></footer>Clear markers help when pages are assembled from parts.
<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.
<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.
<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.
<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.
<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.
<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.
<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="<h1>Preview</h1><p>Scripts do not run here.</p>" width="400" height="200"></iframe>Show untrusted HTML safely.
<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.
<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.
<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.
<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.
<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.
<ul class="gallery">
<li>
<a href="img/large-1.jpg" data-caption="Sunrise">
<img src="img/thumb-1.jpg" alt="Sunrise over hills" width="300" height="200" loading="lazy">
</a>
</li>
<li>
<a href="img/large-2.jpg" data-caption="Lake">
<img src="img/thumb-2.jpg" alt="A calm lake" width="300" height="200" loading="lazy">
</a>
</li>
</ul>Links point to the large image, so it still works without JavaScript.
<main class="cv">
<header>
<h1>Khurram Bashir</h1>
<p>Solutions Architect · <a href="mailto:name@example.com">name@example.com</a></p>
</header>
<section aria-labelledby="exp">
<h2 id="exp">Experience</h2>
<article>
<h3>Deputy Director IT</h3>
<p><time datetime="2021">2021</time> – <time datetime="2026">present</time></p>
<ul><li>Led database design.</li><li>Built internal systems.</li></ul>
</article>
</section>
<section aria-labelledby="skills">
<h2 id="skills">Skills</h2>
<ul><li>PHP</li><li>MySQL</li><li>JavaScript</li></ul>
</section>
</main>Semantic markup that prints cleanly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fast page</title>
<style>
body{margin:0;font:16px/1.5 system-ui,sans-serif}
.hero{min-height:60vh;display:grid;place-items:center}
</style>
<link rel="preload" href="/css/site.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="/css/site.css"></noscript>
</head>
<body><div class="hero"><h1>Hello</h1></div></body>
</html>Inline the small critical part, load the rest without blocking.
<link rel="manifest" href="/manifest.webmanifest">
<meta name="theme-color" content="#2563eb">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="My App">
<link rel="apple-touch-icon" href="/icons/180.png">Make the site installable.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Newsletter</title>
<style>
@media (max-width: 480px) { .col { display: block !important; width: 100% !important; } }
</style>
</head>
<body style="margin:0;background:#f3f4f6">
<table role="presentation" width="100%" cellpadding="0" cellspacing="0">
<tr><td align="center">
<table role="presentation" width="600" style="max-width:600px;width:100%;background:#ffffff" cellpadding="0" cellspacing="0">
<tr>
<td class="col" width="300" style="padding:16px;font-family:Arial,sans-serif">Left column</td>
<td class="col" width="300" style="padding:16px;font-family:Arial,sans-serif">Right column</td>
</tr>
</table>
</td></tr>
</table>
</body>
</html>Fluid table that stacks columns on phones.
<button type="button" class="yt" data-id="VIDEO_ID" aria-label="Play video: Intro">
<img src="https://img.youtube.com/vi/VIDEO_ID/hqdefault.jpg" alt="" width="480" height="360" loading="lazy">
</button>
<script>
document.querySelector('.yt').addEventListener('click', function () {
var f = document.createElement('iframe');
f.src = 'https://www.youtube-nocookie.com/embed/' + this.dataset.id + '?autoplay=1';
f.title = 'Intro';
f.allow = 'autoplay; encrypted-media';
f.allowFullscreen = true;
f.width = 480; f.height = 360;
this.replaceWith(f);
});
</script>Load the heavy player only when the visitor clicks.
<rating-stars value="3" max="5"></rating-stars>
<script>
class RatingStars extends HTMLElement {
connectedCallback() {
const max = Number(this.getAttribute('max') || 5);
const value = Number(this.getAttribute('value') || 0);
this.attachShadow({ mode: 'open' }).innerHTML =
'<style>span{color:#f59e0b}</style>' +
'<span role="img" aria-label="' + value + ' of ' + max + '">' +
'★'.repeat(value) + '☆'.repeat(max - value) + '</span>';
}
}
customElements.define('rating-stars', RatingStars);
</script>Your own tag with its own behavior.
<form novalidate>
<div role="alert" id="summary" hidden>Please correct 1 error.</div>
<label for="email">Email</label>
<input id="email" name="email" type="email" aria-invalid="true" aria-describedby="email-err" required>
<p id="email-err">Enter a valid address like name@example.com.</p>
<button type="submit">Send</button>
</form>Link each error to its field.
<nav aria-label="Main">
<ul>
<li><a href="/">Home</a></li>
<li>
<button type="button" aria-expanded="false" aria-controls="sub-services">Services</button>
<ul id="sub-services" hidden>
<li><a href="/web">Web</a></li>
<li><a href="/db">Databases</a></li>
</ul>
</li>
</ul>
</nav>A menu button the right way.
<label for="city">City</label>
<input id="city" type="text" role="combobox" aria-expanded="false" aria-controls="city-list" aria-autocomplete="list" autocomplete="off">
<ul id="city-list" role="listbox" hidden>
<li role="option" id="c1">Islamabad</li>
<li role="option" id="c2">Lahore</li>
</ul>Search box with suggestions.
<div class="backdrop" hidden id="backdrop">
<div role="dialog" aria-modal="true" aria-labelledby="m-title" aria-describedby="m-desc" tabindex="-1">
<h2 id="m-title">Delete project?</h2>
<p id="m-desc">This cannot be undone.</p>
<button type="button" data-close>Cancel</button>
<button type="button" data-confirm>Delete</button>
</div>
</div>The structure of a dialog that keyboard and screen reader users can use.
<link rel="alternate" hreflang="en" href="https://example.com/en/page">
<link rel="alternate" hreflang="ur" href="https://example.com/ur/page">
<link rel="alternate" hreflang="ar" href="https://example.com/ar/page">
<link rel="alternate" hreflang="x-default" href="https://example.com/page">Link every language version to the others.