Code library
Free snippets & functions
Copy, adapt and use them in your own projects.
Beginner 35 snippets
<nav aria-label="Main">
<ul>
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/services">Services</a>
<ul>
<li><a href="/services/web">Web</a></li>
<li><a href="/services/db">Databases</a></li>
</ul>
</li>
<li><a href="/about">About</a></li>
</ul>
</nav>Menus are lists of links.
<meta name="description" content="Short summary of this page, up to about 155 characters.">
<meta property="og:title" content="My Page Title">
<meta property="og:description" content="Short summary for social networks.">
<meta property="og:image" content="https://example.com/share.jpg">
<meta property="og:url" content="https://example.com/page">
<meta name="twitter:card" content="summary_large_image">Controls how your page looks when someone shares the link.
<p contenteditable="true">Click and type here.</p>
<p hidden>This paragraph is not shown.</p>
<button type="button" title="Shows a tooltip" tabindex="0">Hover me</button>
<p id="x" class="note" data-user-id="42" lang="en">Custom data attributes start with data-.</p>Some attributes work on every element.
<form>
<label for="u">Username</label>
<input id="u" name="u" type="text" required minlength="3" maxlength="20" pattern="[A-Za-z0-9_]+" placeholder="letters, numbers, _" autocomplete="username">
<label for="p">Password</label>
<input id="p" name="p" type="password" required minlength="8" autocomplete="new-password">
<button type="submit">Create account</button>
</form>Built-in validation without JavaScript.
<label for="up">Upload:</label>
<progress id="up" value="40" max="100">40%</progress>
<label for="disk">Disk used:</label>
<meter id="disk" value="0.7" min="0" max="1" low="0.5" high="0.8" optimum="0.2">70%</meter>Native bars. Style them with CSS if needed.
<p>Published on <time datetime="2026-11-03">3 November 2026</time>.</p>
<p>The event starts at <time datetime="2026-12-01T18:30">6:30 pm on 1 December</time>.</p>People read the text, machines read datetime.
<a href="/files/brochure.pdf" download="brochure.pdf">Download the brochure (PDF, 2 MB)</a>The download attribute asks the browser to save the file.
<address>
Khurram Bashir<br>
Islamabad<br>
Pakistan
</address>
<hr>
<p>A new topic starts after the line.</p>Use br inside text (addresses, poems), hr for a topic change.
<pre><code>function hello() {
return "Hi";
}</code></pre>
<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save.</p>pre keeps spaces and line breaks.
<blockquote cite="https://example.com/speech">
<p>The best way to predict the future is to invent it.</p>
</blockquote>
<p>Written by <cite>Alan Kay</cite>. Short quote: <q>Think different.</q></p>
<p><abbr title="HyperText Markup Language">HTML</abbr> is not a programming language.</p>Mark up quotes properly.
<html lang="en" dir="ltr">
<head><title>English page</title></head>
<body>
<p>Hello, and in Urdu: <span lang="ur" dir="rtl">السلام علیکم</span></p>
</body>
</html>Helps screen readers, translation and hyphenation.
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">Without it phones show a tiny zoomed-out page.
<link rel="icon" href="/favicon.ico" sizes="32x32">
<link rel="icon" href="/icon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<meta name="theme-color" content="#2563eb">The small icon in the browser tab.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Linked files</title>
<link rel="stylesheet" href="css/style.css">
<script src="js/app.js" defer></script>
</head>
<body>
<h1>Hello</h1>
</body>
</html>CSS goes in the head, scripts at the end or with defer.
<div>A block element takes the whole row.</div>
<div>Another block below it.</div>
<p>Inline elements like <a href="#">links</a>, <b>bold</b> and <span>spans</span> stay inside the line.</p>Blocks start on a new line, inline elements stay in the text.
<details>
<summary>What is your refund policy?</summary>
<p>You can ask for a refund within 30 days.</p>
</details>
<details open>
<summary>Do you ship abroad?</summary>
<p>Yes, to most countries.</p>
</details>No JavaScript needed.
<figure>
<img src="chart.png" alt="Bar chart: sales grew 20 percent" width="600" height="360">
<figcaption>Sales grew 20% in 2026.</figcaption>
</figure>Tie an image to its caption.
<iframe src="https://www.openstreetmap.org/export/embed.html" title="Map of our office" width="600" height="400" loading="lazy" style="border:0"></iframe>Always give an iframe a title.
<video controls width="640" poster="poster.jpg" preload="metadata">
<source src="movie.webm" type="video/webm">
<source src="movie.mp4" type="video/mp4">
<track kind="captions" src="captions.vtt" srclang="en" label="English">
Your browser does not support video.
</video>
<audio controls src="podcast.mp3"></audio>Provide controls and more than one format when you can.
<!-- This comment is not shown on the page -->
<p>5 < 10 && 10 > 5</p>
<p>Copyright © 2026 — All rights reserved.</p>
<p>Price: 20 USD</p>Show characters that HTML would otherwise treat as code.
<form>
<button type="submit">Save</button>
<button type="reset">Reset</button>
<button type="button" id="help">Help</button>
<button type="submit" disabled>Disabled</button>
</form>Always set the type, otherwise a button inside a form submits it.
<fieldset>
<legend>Plan</legend>
<label><input type="radio" name="plan" value="free" checked> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
</fieldset>
<label><input type="checkbox" name="terms" required> I accept the terms</label>Group related options in a fieldset.
<label for="country">Country</label>
<select id="country" name="country">
<option value="">Choose...</option>
<optgroup label="Asia">
<option value="pk">Pakistan</option>
<option value="in">India</option>
</optgroup>
</select>
<label for="msg">Message</label>
<textarea id="msg" name="msg" rows="5" cols="40" maxlength="500"></textarea>A dropdown and a multi-line box.
<label>Text <input type="text" name="t"></label>
<label>Email <input type="email" name="e"></label>
<label>Password <input type="password" name="p"></label>
<label>Number <input type="number" name="n" min="0" max="100" step="5"></label>
<label>Phone <input type="tel" name="tel"></label>
<label>Date <input type="date" name="d"></label>
<label>Color <input type="color" name="c" value="#2563eb"></label>
<label>Range <input type="range" name="r" min="0" max="10"></label>
<label>Website <input type="url" name="u"></label>The right type gives the right keyboard on phones.
<form action="/subscribe" method="post">
<label for="name">Name</label>
<input type="text" id="name" name="name" required>
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<button type="submit">Subscribe</button>
</form>Every field needs a label.
<div class="card">
<h2 id="title">Product</h2>
<p>Price: <span class="price">$20</span></p>
</div>Generic containers. Use them when no meaningful tag fits.
<header>
<a href="/">My Site</a>
<nav aria-label="Main">
<a href="/">Home</a>
<a href="/blog">Blog</a>
</nav>
</header>
<main>
<article>
<h1>Article title</h1>
<p>Content...</p>
</article>
<aside>
<h2>Related</h2>
</aside>
</main>
<footer>
<p>© 2026 My Site</p>
</footer>These tags tell browsers and screen readers what each part is.
<table>
<caption>Monthly sales</caption>
<thead>
<tr><th scope="col">Month</th><th scope="col">Sales</th></tr>
</thead>
<tbody>
<tr><td>January</td><td>1200</td></tr>
<tr><td>February</td><td>1500</td></tr>
</tbody>
<tfoot>
<tr><th scope="row">Total</th><td>2700</td></tr>
</tfoot>
</table>Use tables for data, not for page layout.
<ul>
<li>Apples</li>
<li>Bananas</li>
</ul>
<ol>
<li>Open the editor</li>
<li>Write the code</li>
<li>Save the file</li>
</ol>
<dl>
<dt>HTML</dt>
<dd>The structure of a page.</dd>
<dt>CSS</dt>
<dd>The look of a page.</dd>
</dl>Bulleted, numbered and description lists.
<img src="photo.jpg" alt="Two people reviewing a plan on a whiteboard" width="800" height="450">
<!-- Decorative image: empty alt, so screen readers skip it -->
<img src="divider.png" alt="" width="100" height="10">Always write alt text, and set width and height to avoid page jumps.