Developers

How to add a QR code in HTML

To add a QR code in HTML, use an img tag whose src points at https://qrcodesforever.com/qr.svg with your content in the query string, like ?url=example.com. The browser loads the finished code like any other image, so a static page with no JavaScript, build step or API key is enough.

The snippet

<a href="https://qrcodesforever.com/">
  <img src="https://qrcodesforever.com/qr.svg?url=example.com"
       alt="QR code for example.com" width="200" height="200">
  <br>QR code by QR Codes Forever
</a>

Change example.com to your link. The code will contain https://example.com; the API adds the scheme when it's missing.

  • width and height are needed. The SVG has a viewBox but no size of its own, and setting both stops the page from jumping while it loads.
  • alt should say what scanning does, for screen reader users.
  • The link around it is optional credit. The image works the same without it.

SVG is the right default for web pages. A short link comes out around 11 KB and stays sharp on any screen. Use /qr.png where SVG isn't welcome, such as email.

Other kinds of code

Inside an HTML attribute, write &amp; between parameters. Browsers accept a bare &, but &amp; is the valid form.

<!-- Wi-Fi: guests scan to join -->
<img src="https://qrcodesforever.com/qr.svg?ssid=Cafe%20Guest&amp;password=coffee123"
     alt="Scan to join the Cafe Guest Wi-Fi" width="200" height="200">

<!-- Contact card -->
<img src="https://qrcodesforever.com/qr.svg?type=vcard&amp;first=Ada&amp;last=Lovelace&amp;email=ada@example.com"
     alt="Scan to save Ada Lovelace's contact" width="200" height="200">

<!-- Brand color and round dots -->
<img src="https://qrcodesforever.com/qr.svg?url=example.com&amp;fg=1d3fd6&amp;pattern=dots"
     alt="QR code for example.com" width="200" height="200">

The Wi-Fi code contains WIFI:T:WPA;S:Cafe Guest;P:coffee123;;. Anyone who can view the page source can read that password, so only publish it for a network you're happy to share. Contact cards are denser than links: a name, phone and email made a version 8 code (49×49 modules) against version 2 (25×25) for a short link, so give them more room. The parameter reference lists every kind.

Encode anything you insert

A value with &, #, +, ? or spaces has to be percent-encoded first. A link like https://shop.com/?a=1&b=2 becomes:

https://qrcodesforever.com/qr.svg?url=https%3A%2F%2Fshop.com%2F%3Fa%3D1%26b%3D2

Unencoded, the same request makes a code for https://shop.com/?a=1, and nothing warns you. In templates, use the language's encoder: encodeURIComponent in JavaScript, urlencode in Jinja, rawurlencode in PHP.

A QR code for the current page

For printable pages, event pages or product pages, so a reader on a laptop can carry the page to their phone:

<img id="page-qr" alt="QR code for this page" width="160" height="160">
<script>
  document.getElementById("page-qr").src =
    "https://qrcodesforever.com/qr.svg?url=" + encodeURIComponent(location.href);
</script>

Sizing, and what happens if we're down

On screen, 150 to 250 CSS pixels is easy to scan from a monitor. For print, the size calculator works out the size for the scanning distance, and the SVG stays sharp at any of them.

The image loads from our server, so if the site were down, embedded codes would show as broken images until it's back. Codes already printed or saved keep scanning, because the content is inside the code. For a page that must never miss, save the SVG once and host it yourself.

Written by Last updated