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.
Syntax
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 & are separate from URL encoding.
Reference
Core HTML Concepts
Rules that keep links valid:
Concept
Meaning
Percent-encoding
% + two hex digits standing for one byte
Space
Usually %20 in paths and query values
Reserved chars
:/?#[]@!$&'()*+,;=
Encode as data
When a reserved char is part of a value, encode it (e.g. & → %26)
encodeURIComponent
JS helper for encoding a single component/value
encodeURI
Encodes a fuller URL but leaves structural delimiters
UTF-8 bytes
Non-ASCII characters encode as one or more %XX sequences
Common encodings
Character
Encoded
Why
space
%20
Not allowed raw in most URL parts
&
%26
Otherwise starts a new query pair
=
%3D
Otherwise splits name/value
?
%3F
Otherwise starts the query
#
%23
Otherwise starts the fragment
%
%25
Otherwise begins an escape
Approach
How to Use URL Encoding in HTML
Practical habits for safe href values.
Decide which parts are structure (?, &, =) and which are data.
Encode each data value: spaces → %20, reserved symbols → their %XX forms.
In static HTML, write the encoded form directly in href.
In JavaScript, use encodeURIComponent on each query value before concatenating.
Never double-encode (%20 must not become %2520) unless intentional.
Decode on the server or with decodeURIComponent when reading values.
Goal
Pattern
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 value
q=rock%26roll
Try it
Live Preview
Type a search phrase and see the encoded href query.
Uses encodeURIComponent-style rules for the query value only.
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 & vs URL %26
In HTML source, attribute & often written &. 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
URL encodingUniversal
Bottom line: Encode data values with %XX (space → %20); leave URL structure characters alone.
Remember
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.