Code library
Free snippets & functions
Copy, adapt and use them in your own projects.
Advanced 33 snippets
HTML · Gallery markup with a lightbox targetAdvanced
<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.
HTML · A printable CV structureAdvanced
<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.
HTML · Critical CSS and async CSSAdvanced
<!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.
HTML · PWA head tagsAdvanced
<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.
HTML · Responsive HTML emailAdvanced
<!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.
HTML · Lightweight video facadeAdvanced
<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.
HTML · A simple custom elementAdvanced
<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.
HTML · Form errors for screen readersAdvanced
<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.
HTML · Disclosure navigationAdvanced
<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.
HTML · Accessible combobox markupAdvanced
<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.
HTML · Accessible modal markupAdvanced
<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.
HTML · Multi-language pagesAdvanced
<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.
HTML · JSON-LD for a person or businessAdvanced
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Person",
"name": "Khurram Bashir",
"jobTitle": "Solutions Architect",
"url": "https://example.com/",
"sameAs": ["https://www.linkedin.com/in/example", "https://github.com/example"],
"knowsAbout": ["PHP", "MySQL", "Database design"]
}
</script>Tell search engines who is behind the site.
HTML · JSON-LD for breadcrumbs and FAQAdvanced
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@graph": [
{
"@type": "BreadcrumbList",
"itemListElement": [
{ "@type": "ListItem", "position": 1, "name": "Home", "item": "https://example.com/" },
{ "@type": "ListItem", "position": 2, "name": "Code", "item": "https://example.com/code" }
]
},
{
"@type": "FAQPage",
"mainEntity": [{
"@type": "Question",
"name": "Is the code free?",
"acceptedAnswer": { "@type": "Answer", "text": "Yes, copy and use it." }
}]
}
]
}
</script>Structured data for rich results.
HTML · A complete SEO and social headAdvanced
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Learn PHP step by step | Example</title>
<meta name="description" content="Short, useful summary of the page in about 155 characters.">
<link rel="canonical" href="https://example.com/php">
<meta property="og:type" content="article">
<meta property="og:title" content="Learn PHP step by step">
<meta property="og:description" content="Short, useful summary.">
<meta property="og:image" content="https://example.com/og/php.jpg">
<meta property="og:url" content="https://example.com/php">
<meta name="twitter:card" content="summary_large_image">
<meta name="theme-color" content="#2563eb">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
</head>
<body><h1>Learn PHP step by step</h1></body>
</html>Everything a public page needs in the head.