Code library

Free snippets & functions

Copy, adapt and use them in your own projects.

Beginner 35 snippets

HTML · A navigation menu from a listBeginner
<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.

HTML · Share preview tagsBeginner
<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.

HTML · Editable contentBeginner
<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.

HTML · Required fields and hintsBeginner
<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.

HTML · Progress and meterBeginner
<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.

HTML · Dates with the time tagBeginner
<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.

HTML · A download linkBeginner
<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.

HTML · Line breaks and dividersBeginner
<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.

HTML · Show code on a pageBeginner
<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.

HTML · Quotes and citationsBeginner
<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 · Set the language and directionBeginner
<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.

HTML · The viewport meta tagBeginner
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

Without it phones show a tiny zoomed-out page.

HTML · Add a faviconBeginner
<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.

HTML · Add CSS and JavaScript filesBeginner
<!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.

HTML · Block and inline elementsBeginner
<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.

HTML · Open and close sectionsBeginner
<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.

HTML · Figure with captionBeginner
<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.

HTML · Embed another pageBeginner
<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.

HTML · Audio and videoBeginner
<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 &lt; 10 &amp;&amp; 10 &gt; 5</p>
<p>Copyright &copy; 2026 &mdash; All rights reserved.</p>
<p>Price: 20&nbsp;USD</p>

Show characters that HTML would otherwise treat as code.

HTML · ButtonsBeginner
<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.

HTML · Radio buttons and checkboxesBeginner
<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.

HTML · Select and textareaBeginner
<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.

HTML · Common input typesBeginner
<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.

HTML · A basic formBeginner
<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.

HTML · div and spanBeginner
<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.

HTML · Semantic page layoutBeginner
<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>&copy; 2026 My Site</p>
</footer>

These tags tell browsers and screen readers what each part is.

HTML · A simple tableBeginner
<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.

HTML · ListsBeginner
<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.

HTML · Images with alt textBeginner
<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.