Code library
Free snippets & functions
Copy, adapt and use them in your own projects.
<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.
<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.
.spinner {
width: 32px;
height: 32px;
border: 4px solid #e5e7eb;
border-top-color: #2563eb;
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }A circle with one colored edge, rotating.
.box {
background: #e0e7ff;
transition: background-color 0.25s ease, transform 0.25s ease;
}
.box:hover {
background: #c7d2fe;
transform: scale(1.03);
}Add transition to the normal state, not only to :hover.
.icon-wrap { position: relative; display: inline-block; }
.icon-wrap .count {
position: absolute;
top: -6px;
right: -8px;
min-width: 18px;
padding: 0 5px;
border-radius: 999px;
background: #ef4444;
color: #fff;
font-size: 0.7rem;
line-height: 18px;
text-align: center;
}The parent is relative, the child is absolute.
.one-line {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.three-lines {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}One line or a fixed number of lines.
li:first-child { border-top-left-radius: 8px; border-top-right-radius: 8px; }
li:last-child { border-bottom: 0; }
li:nth-child(odd) { background: #f9fafb; }
li:nth-child(3n) { font-weight: 700; }
li:not(:last-child) { border-bottom: 1px solid #e5e7eb; }Select items by position without extra classes.
.nav-link {
position: relative;
text-decoration: none;
color: inherit;
}
.nav-link::after {
content: "";
position: absolute;
left: 0;
bottom: -3px;
width: 100%;
height: 2px;
background: currentColor;
transform: scaleX(0);
transform-origin: left;
transition: transform 0.25s ease;
}
.nav-link:hover::after, .nav-link:focus-visible::after { transform: scaleX(1); }Grows from left to right using a pseudo-element.
body {
min-height: 100vh;
display: flex;
flex-direction: column;
}
main { flex: 1; }
footer { padding: 1rem; background: #111827; color: #d1d5db; }Short pages still push the footer to the bottom of the window.