HTML Guide for space not allowed
Spaces in the URL fragment are invalid; encode them or remove them (e.g., use %20 or hyphens/underscores).
The href attribute must contain a valid URL. When using a fragment identifier (the part after #), it must follow URL syntax rules: no unescaped spaces. Fragments usually reference an element’s id. An element’s id must be unique and is case-sensitive; while spaces aren’t allowed in id values, many authors accidentally mirror text with spaces in the fragment. Use hyphens or underscores in ids and match the fragment, or percent-encode reserved characters. Prefer readable, dash-separated ids for accessibility and shareable links.
For example, instead of href=”#My Section”, use href=”#my-section” and set id=”my-section” on the target. If you must preserve spaces in a generated URL, encode them as %20, but it’s better to avoid spaces entirely in ids.
HTML Examples
Invalid: reproduces the validator error
<!doctype html>
<html lang="en">
<head>
<title>Fragment with space</title>
</head>
<body>
<a href="#My Section">Go to section</a>
<h2 id="My Section">My Section</h2>
</body>
</html>
Fixed: use a valid fragment and id
<!doctype html>
<html lang="en">
<head>
<title>Valid fragment</title>
</head>
<body>
<a href="#my-section">Go to section</a>
<h2 id="my-section">My Section</h2>
</body>
</html>
Alternatively, encoding the space also passes validation, though less ideal as the id would be invalid because it contains spaces:
<a href="#My%20Section">Go to section</a>
<h2 id="My Section">My Section</h2>
Space characters are not permitted in the value of the href attribute; they must be properly percent-encoded.
The href attribute specifies a URL, and URLs must follow specific syntax rules defined by RFC 3986. Spaces and some other characters are considered illegal in URLs. To include a space in the URL, use the percent escape sequence %20 in place of the space character.
Incorrect example with an illegal space in the query string:
<a href="search.html?q=my search">Search for 'my search'</a>
Correct example using percent-encoding for the space:
<a href="search.html?q=my%20search">Search for 'my search'</a>
Replace all spaces in URLs within href attributes with %20 to ensure W3C validation and proper browser behavior.
A space character in the email address within the mailto: link is invalid syntax.
The href attribute on an <a> tag must contain a valid email address after mailto: in order to conform to HTML standards. Email addresses cannot contain spaces. Including a space (as in user@example com) results in invalid markup.
Correct usage:
Remove any spaces from the email address and use a correctly formatted address such as user@example.com.
Invalid example
<a href="mailto:user@example com">Send Email</a>
Valid example
<a href="mailto:user@example.com">Send Email</a>
A space in the href URL wasn’t percent-encoded, making the link invalid.
The href attribute must contain a valid URL. Spaces and certain characters are not allowed in URLs and must be percent-encoded. In paths and query strings, replace spaces with %20 (or use + only in application/x-www-form-urlencoded query values, not paths).
Prefer generating properly encoded URLs server-side or via encodeURI/encodeURIComponent in JS. For fragment-only links, use href="#id". Avoid unencoded spaces anywhere after the scheme (e.g., https: or mailto:).
HTML Examples
Invalid (reproduces the validator error)
<a href="https://example.com/files/My File.pdf">Download</a>
Fixed (spaces percent-encoded)
<a href="https://example.com/files/My%20File.pdf">Download</a>
An <iframe> element allows to embed an HTML document inside another HTML document, and its src attribute is indicated the source URL of the embedded web page. The query part of that URL contains one or more space characters, which are not allowed, for example:
<iframe src="https://maps.google.it/maps?q=2700 6th Avenue"></iframe>
You should properly escape all space characters as %20 like this:
<iframe src="https://maps.google.it/maps?q=2700%206th%20Avenue"></iframe>
The issue arises from the space character in the src attribute value of the script element. In URLs, spaces are not allowed and should be properly encoded to avoid validation errors.
Fix
Replace spaces with %20, which is the URL-encoded representation of a space.
Example
Before:
<script src="https://example.com/media assets/app.js"></script>
After:
<script src="https://example.com/media%20assets/app.js"></script>
Explanation
In this example, the space between “media” and “assets” in the URL is replaced with %20. This change ensures that the URL conforms to standards and is correctly processed by browsers and servers. Spaces and other special characters in URLs must be encoded to ensure proper formatting and accessibility.