Skip to content

HTML Tutorial Advanced and Practical HTML

URL Encoding in HTML: Special Characters in Links

Lesson 74 of 81 2 min read By Deepak Paswan Updated Oct 4, 2026

A web address can only contain a limited set of characters. Everything else must be percent encoded: replaced by a % followed by two hex digits.

CharacterEncoded
space%20
&%26
?%3F
/%2F
é%C3%A9
index.html
HTML
<a href="search.html?q=cats%20and%20dogs">Search for cats and dogs</a>

Where it matters

  • Links with query strings: the part after ? often contains user text.
  • File names: a file called my photo.jpg should be linked as my%20photo.jpg. It is easier to avoid spaces in file names altogether.
  • mailto links with a subject, as in Email links.

Forms do it for you

When a form with method="get" is sent, the browser encodes the values automatically.

In JavaScript

script.js
JS
encodeURIComponent("cats & dogs");  // "cats%20%26%20dogs"

Use encodeURIComponent when you build a link from user text.

warning
Unencoded ampersand in a value
An & inside a value, such as ?q=salt&pepper, starts a new parameter. Encode it as %26.