HTML URL Encoding

Beginner
9 min read
Updated: Nov 2026
6 examples
%20

Definition and Usage

URL encoding (also called percent-encoding) replaces characters that are unsafe or reserved in a URL with a % followed by two hexadecimal digits. A space becomes %20. This keeps href values and query strings parseable.

What is URL encoding for in an HTML tutorial?

Links often carry search terms, file names, or IDs. Encoding those values prevents spaces and symbols like & from breaking the URL structure.

Where does encoding apply?

Path segments, query parameter values, and fragment data when they contain spaces, non-ASCII text, or reserved characters. You usually leave ?, &, and = alone when they act as separators.

When should you encode?

Whenever a value is not a fixed safe token—user input, multi-word searches, emails in query params, or paths with spaces. Static safe paths like /about need no encoding.

Space → %20.

The most common beginner fix: hello world in a query becomes hello%20world.

Encode values, not the whole URL.

Do not percent-encode https:// or the structural ? / & that separate parameters.

encodeURIComponent concept.

In JavaScript, encodeURIComponent(value) applies the same idea when you build an href from variables.

URL Encoding Syntax

The basic form in HTML attributes:

encode.html
href ="/search?q=hello%20world"
HTML
<!-- Space encoded as %20 -->
<a href="/search?q=html%20tutorial">Search</a>

<!-- Reserved & inside a value must be encoded as %26 -->
<a href="/search?q=cats%26dogs">Cats & dogs</a>

<!-- Build in JavaScript -->
<script>
  var q = encodeURIComponent("hello world");
  var url = "/search?q=" + q; // /search?q=hello%20world
</script>

Percent-encoding uses uppercase or lowercase hex (%20 or %20). Encode % itself as %25 when it is data. In attribute text, remember HTML entities like &amp; are separate from URL encoding.

Core HTML Concepts

Rules that keep links valid:

ConceptMeaning
Percent-encoding% + two hex digits standing for one byte
SpaceUsually %20 in paths and query values
Reserved chars: / ? # [ ] @ ! $ & ' ( ) * + , ; =
Encode as dataWhen a reserved char is part of a value, encode it (e.g. & → %26)
encodeURIComponentJS helper for encoding a single component/value
encodeURIEncodes a fuller URL but leaves structural delimiters
UTF-8 bytesNon-ASCII characters encode as one or more %XX sequences

Common encodings

CharacterEncodedWhy
space%20Not allowed raw in most URL parts
&%26Otherwise starts a new query pair
=%3DOtherwise splits name/value
?%3FOtherwise starts the query
#%23Otherwise starts the fragment
%%25Otherwise begins an escape

How to Use URL Encoding in HTML

Practical habits for safe href values.

  1. Decide which parts are structure (?, &, =) and which are data.
  2. Encode each data value: spaces → %20, reserved symbols → their %XX forms.
  3. In static HTML, write the encoded form directly in href.
  4. In JavaScript, use encodeURIComponent on each query value before concatenating.
  5. Never double-encode (%20 must not become %2520) unless intentional.
  6. Decode on the server or with decodeURIComponent when reading values.
GoalPattern
Static search link<a href="/search?q=html%20css">
JS query value"/search?q=" + encodeURIComponent(term)
Path with space/docs/my%20guide.html
Ampersand in valueq=rock%26roll

Live Preview

Type a search phrase and see the encoded href query.

Uses encodeURIComponent-style rules for the query value only.

Encoded href
<a href="/search?q=hello%20world">Search</a>

HTML Examples

Six patterns: spaces as %20, reserved characters, path segments, multi-param queries, encodeURIComponent, and a full page.

Example 1 — Encode Spaces as %20

HTML
<p><a href="/search?q=html%20tutorial">Search: html tutorial</a></p>
<p>Raw space in a URL is invalid; use %20 instead.</p>
Try It Yourself

How It Works

  1. Space becomes %20 in the query value.
  2. Link text can still show normal words with spaces.
  3. Browser sends the encoded form to the server.
  4. Use for any multi-word search in href.

Example 2 — Reserved Characters in Values

HTML
<ul>
  <li><a href="/search?q=cats%26dogs">q = cats&dogs</a></li>
  <li><a href="/search?q=a%3Db">q = a=b</a></li>
  <li><a href="/search?q=100%25">q = 100%</a></li>
</ul>
Try It Yourself

How It Works

  1. & as data must be %26 or it starts another parameter.
  2. = as data becomes %3D.
  3. % as data becomes %25.
  4. Separators between parameters stay literal & and =.

Example 3 — Encode a Path Segment

HTML
<p>
  <a href="/docs/getting%20started.html">Getting started guide</a>
</p>
<p>File names with spaces need encoding in the path.</p>
Try It Yourself

How It Works

  1. Path segments encode the same way as query values for spaces.
  2. Prefer hyphenated file names when you control the files.
  3. Slashes that separate directories stay unencoded.
  4. Great for legacy file names you cannot rename yet.

Example 4 — Multiple Query Parameters

HTML
<a href="/shop?category=web%20dev&sort=price">
  Web dev — sort by price
</a>
Try It Yourself

How It Works

  1. Encode each value independently.
  2. Join pairs with literal & (in HTML source often written &amp;).
  3. Names like category and sort stay readable tokens.
  4. Order of parameters usually does not matter for encoding rules.

Example 5 — encodeURIComponent for href

HTML
<p>Built link: <a id="dyn" href="#"></a></p>
<script>
  var term = "cats & dogs";
  var href = "/search?q=" + encodeURIComponent(term);
  var a = document.getElementById("dyn");
  a.href = href;
  a.textContent = href;
</script>
Try It Yourself

How It Works

  1. encodeURIComponent turns spaces and & into percent sequences.
  2. Prefix the path and ?q= without encoding them.
  3. Assign the result to a.href.
  4. Use whenever values come from inputs or variables.

Example 6 — Complete HTML Page

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>URL Encoding Example</title>
</head>
<body>
  <h1>Search links</h1>
  <ul>
    <li><a href="/search?q=html%20forms">html forms</a></li>
    <li><a href="/search?q=a%26b">a&b</a></li>
  </ul>
  <p>Dynamic:</p>
  <a id="go" href="#">Loading…</a>
  <script>
    var go = document.getElementById("go");
    go.href = "/search?q=" + encodeURIComponent("hello world");
    go.textContent = go.getAttribute("href");
  </script>
</body>
</html>
Try It Yourself

How It Works

  1. Static links hard-code %20 and %26.
  2. Script encodes a runtime string the same way.
  3. Visible text can show the decoded meaning.
  4. Full page combines both techniques.

Edge Cases & Pitfalls

Most broken query links come from unencoded & or double encoding.

space

Literal spaces in href

Browsers may tolerate them, but results are inconsistent. Prefer %20.

amp

Unencoded & in a value

Splits the query into extra parameters. Encode as %26.

html

HTML &amp; vs URL %26

In HTML source, attribute & often written &amp;. That is HTML escaping, not percent-encoding.

double

Double encoding

Encoding %20 again yields %2520. Encode once per layer.

plus

+ for spaces

Form application/x-www-form-urlencoded may use +. In general URLs prefer %20.

whole

Encoding the entire URL

encodeURIComponent on a full URL breaks :// and ?. Encode components only.

Browser Support

Percent-encoded URLs in href and encodeURIComponent are supported in all modern browsers.

✓ Baseline · Widely available

URL encoding

Browsers accept percent-encoded href values and expose encodeURIComponent in JavaScript.

100% percent-encoding
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
URL encoding Universal

Bottom line: Encode data values with %XX (space → %20); leave URL structure characters alone.

Key Takeaways

  • Spaces in URLs become %20.
  • Reserved characters inside values must be percent-encoded.
  • encodeURIComponent builds safe query values for dynamic href.
  • Encode components, not the entire URL string.

One line: Percent-encode spaces and reserved characters in path/query values, and use encodeURIComponent when assembling href in script.

Frequently Asked Questions

URL encoding (percent-encoding) replaces unsafe or reserved characters with a percent sign and two hex digits—for example a space becomes %20—so links and query strings stay valid.
Spaces are not allowed as literal characters in most URL parts. Encoding turns each space into %20 so servers and browsers agree on the same value. Some forms historically used +, but %20 is the general rule for path and query values.
Spaces, non-ASCII text, and reserved characters that would change URL structure—such as &, =, ?, #, /, and % itself—when they appear inside a value rather than as URL syntax.
In JavaScript, encodeURIComponent encodes a string for safe use as a query parameter or path segment. When you build an href in script, encode each value before joining with & and =.
No. Encode the pieces that are data (path segments, query values). Leave the scheme, host, and structural delimiters (?, &, =, #) unencoded when they act as syntax.
Yes. In static markup you can write href="/search?q=hello%20world". Prefer encoding dynamically when values come from users or variables.

Practice in the editor

Open a Try It demo and encode spaces and reserved characters.

Open Try It editor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful