Code library
Free snippets & functions
Copy, adapt and use them in your own projects.
Beginner 35 snippets
<a href="about.html">About (same site)</a>
<a href="https://example.com/">Another site</a>
<a href="https://example.com/" target="_blank" rel="noopener noreferrer">Open in a new tab</a>
<a href="mailto:hello@example.com">Send email</a>
<a href="tel:+920000000000">Call us</a>
<a href="#contact">Jump to the contact section</a>Relative, absolute and email links.
<p>This is <strong>very important</strong> and this is <em>emphasized</em>.</p>
<p>Search results show <mark>highlighted</mark> words.</p>
<p>Run <code>npm install</code> first.</p>
<p><small>Prices include tax.</small></p>
<p>H<sub>2</sub>O and E = mc<sup>2</sup></p>
<p>He said <q>HTML is easy</q>.</p>Use tags for meaning, not only for looks.
<h1>Learning HTML</h1>
<p>HTML describes the <strong>structure</strong> of a page.</p>
<h2>Why headings matter</h2>
<p>Screen readers and search engines use them as an outline.</p>
<h3>A smaller section</h3>
<p>Do not skip levels just to get a smaller font. Use CSS for that.</p>One h1 per page, then h2 and h3 in order.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My first page</title>
</head>
<body>
<h1>Hello, world</h1>
<p>This is my first web page.</p>
</body>
</html>Every page starts with this skeleton.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page title</title>
<meta name="description" content="A short description for search engines.">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>…</header>
<main>…</main>
<footer>…</footer>
<script src="app.js"></script>
</body>
</html>The minimum every modern page needs, including the viewport tag.
Intermediate 37 snippets
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(max-width: 700px) 100vw, 800px"
width="800" height="533"
loading="lazy" decoding="async"
alt="Describe what the picture shows">Offer several sizes and let the browser pick the best one. Width and height stop the page from jumping while loading.
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page title – Site name</title>
<meta name="description" content="One or two sentences (about 150 characters) that describe this page.">
<link rel="canonical" href="https://www.example.com/this-page">
<meta property="og:type" content="website">
<meta property="og:title" content="Page title">
<meta property="og:description" content="Same short description.">
<meta property="og:url" content="https://www.example.com/this-page">
<meta property="og:image" content="https://www.example.com/share.jpg">
<meta name="twitter:card" content="summary_large_image">
</head>The tags every public page should have: title, description, canonical address and social-sharing tags.
<form oninput="total.value = Number(qty.value) * 20">
<label for="qty">Quantity</label>
<input id="qty" name="qty" type="number" value="1" min="1">
<p>Total: <output name="total" for="qty">20</output> USD</p>
</form>The output element shows a calculated value.
<p><ruby>漢<rp>(</rp><rt>kan</rt><rp>)</rp></ruby> is a Chinese character.</p>
<p>User <bdi>إيان</bdi> scored 90 points.</p>
<p><bdo dir="rtl">This text is reversed.</bdo></p>Small helpers for other languages.
<img src="floor.png" alt="Office floor plan" usemap="#plan" width="600" height="400">
<map name="plan">
<area shape="rect" coords="0,0,300,200" href="/room-a" alt="Room A">
<area shape="circle" coords="450,100,50" href="/lobby" alt="Lobby">
</map>Clickable regions on one image.
<!DOCTYPE html>
<html lang="en">
<head>
<base href="https://example.com/docs/" target="_self">
<title>Docs</title>
</head>
<body>
<a href="intro.html">Relative to the base</a>
</body>
</html>Sets a starting address for all relative links.
<button type="button" class="buy" data-product-id="42" data-price="19.99" data-name="Notebook">Buy</button>
<script>
document.querySelector('.buy').addEventListener('click', function () {
const d = this.dataset; // d.productId, d.price, d.name
console.log(d.productId, Number(d.price), d.name);
});
</script>Pass small values from HTML to JavaScript.
<template id="card-tpl">
<style>.card { border: 1px solid #ddd; padding: 1rem; border-radius: 8px; }</style>
<div class="card">
<slot name="title">Default title</slot>
<slot></slot>
</div>
</template>
<my-card>
<h3 slot="title">Hello</h3>
<p>Content goes into the default slot.</p>
</my-card>Reusable markup that does nothing until you use it.
<button type="button" onclick="window.print()">Print this page</button>
<link rel="stylesheet" href="print.css" media="print">A print stylesheet is loaded only when printing.
<div style="aspect-ratio:16/9;max-width:720px">
<iframe src="https://www.youtube-nocookie.com/embed/VIDEO_ID" title="Video title" style="width:100%;height:100%;border:0" loading="lazy" allowfullscreen></iframe>
</div>Keep the 16:9 shape at any width.
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" style="background:#f3f4f6;padding:24px 0">
<tr>
<td align="center">
<table role="presentation" width="600" cellpadding="0" cellspacing="0" style="background:#ffffff;border-radius:8px">
<tr><td style="padding:24px;font-family:Arial,sans-serif;font-size:16px;color:#111827">
<h1 style="margin:0 0 12px;font-size:22px">Welcome!</h1>
<p style="margin:0 0 16px">Thanks for joining us.</p>
<a href="https://example.com/start" style="display:inline-block;padding:12px 20px;background:#2563eb;color:#ffffff;text-decoration:none;border-radius:6px">Get started</a>
</td></tr>
</table>
</td>
</tr>
</table>Emails still use tables and inline styles.
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src 'self' data: https:; script-src 'self'; style-src 'self' 'unsafe-inline'; object-src 'none'; base-uri 'self'">Better as an HTTP header, but a meta tag works for static pages.
<a href="https://other.example/" target="_blank" rel="noopener noreferrer nofollow">External</a>
<iframe src="https://widget.example/" title="Widget" sandbox="allow-scripts allow-same-origin" referrerpolicy="no-referrer" loading="lazy"></iframe>
<img src="https://cdn.example/logo.png" alt="Logo" referrerpolicy="no-referrer" width="100" height="40">Small attributes that reduce risk.
<link rel="canonical" href="https://example.com/page">
<meta name="robots" content="index, follow, max-image-preview:large">
<!-- For a private page: <meta name="robots" content="noindex, nofollow"> -->
<link rel="alternate" hreflang="ur" href="https://example.com/ur/page">
<link rel="alternate" type="application/rss+xml" title="Feed" href="/feed.xml">Tell search engines which URL is the main one.
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "How to learn SQL",
"author": { "@type": "Person", "name": "KB" },
"datePublished": "2026-11-03",
"image": "https://example.com/sql.jpg"
}
</script>Helps search engines understand the page.
<nav aria-label="Pagination">
<ul>
<li><a href="?page=1" rel="prev">Previous</a></li>
<li><a href="?page=1">1</a></li>
<li><a href="?page=2" aria-current="page">2</a></li>
<li><a href="?page=3">3</a></li>
<li><a href="?page=3" rel="next">Next</a></li>
</ul>
</nav>Links with previous and next hints.
<nav aria-label="Breadcrumb">
<ol>
<li><a href="/">Home</a></li>
<li><a href="/code">Code</a></li>
<li><a href="/code/php" aria-current="page">PHP</a></li>
</ol>
</nav>An ordered list inside a labelled nav.
<article>
<header>
<h1>How to learn SQL</h1>
<p>By <a href="/author/kb" rel="author">KB</a> on <time datetime="2026-11-03">3 Nov 2026</time></p>
</header>
<p>Start with SELECT...</p>
<section>
<h2>Joins</h2>
<p>...</p>
</section>
<footer>
<p>Tags: <a href="/tag/sql" rel="tag">SQL</a></p>
</footer>
</article>Semantic markup for a blog post.
<table>
<thead>
<tr><th rowspan="2" scope="col">Name</th><th colspan="2" scope="colgroup">Score</th></tr>
<tr><th scope="col">Math</th><th scope="col">Science</th></tr>
</thead>
<tbody>
<tr><th scope="row">Ali</th><td>90</td><td>85</td></tr>
</tbody>
</table>A header spanning columns and rows.
<table>
<caption>Plans and prices</caption>
<colgroup><col><col span="2"></colgroup>
<thead>
<tr><th scope="col">Plan</th><th scope="col">Monthly</th><th scope="col">Yearly</th></tr>
</thead>
<tbody>
<tr><th scope="row">Basic</th><td>$5</td><td>$50</td></tr>
<tr><th scope="row">Pro</th><td>$12</td><td>$120</td></tr>
</tbody>
</table>Headers tied to cells.
<button type="button" popovertarget="menu">Menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<a href="/logout">Log out</a>
</div>Open and close a floating panel without JavaScript.
<button type="button" id="open">Open</button>
<dialog id="dlg" aria-labelledby="dlg-title">
<h2 id="dlg-title">Are you sure?</h2>
<form method="dialog">
<button type="submit" value="cancel">Cancel</button>
<button type="submit" value="ok">OK</button>
</form>
</dialog>
<script>
document.getElementById('open').addEventListener('click', function () {
document.getElementById('dlg').showModal();
});
</script>A built-in modal with focus handling and Escape to close.
<div class="tabs">
<div role="tablist" aria-label="Product info">
<button role="tab" id="t1" aria-selected="true" aria-controls="p1" type="button">Details</button>
<button role="tab" id="t2" aria-selected="false" aria-controls="p2" tabindex="-1" type="button">Reviews</button>
</div>
<div role="tabpanel" id="p1" aria-labelledby="t1">Details...</div>
<div role="tabpanel" id="p2" aria-labelledby="t2" hidden>Reviews...</div>
</div>The HTML that tab scripts should produce.
<div role="status" aria-live="polite" id="saved">Saved</div>
<div role="alert" id="error">Please fix the errors above.</div>
<p aria-live="polite" aria-atomic="true">3 results found</p>Screen readers read the text when it changes.
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title>Page title</title></head>
<body>
<a class="skip-link" href="#content">Skip to main content</a>
<header><a href="/">Home</a></header>
<main id="content" tabindex="-1">
<h1>Page title</h1>
</main>
</body>
</html>The first link on the page for keyboard users.