Are spaces in "href" valid?

In (X)HTML5, the a element’s href attribute

[…] must have a value that is a valid URL potentially surrounded by spaces.

As the anchor suggests, it may contain leading and trailing spaces.

The linked section makes clear that these spaces will be stripped:

[…] the user agent must remove all space characters that are at the start or end of the string […]


If the actual URL starts/ends with (or contains) spaces, you have to percent-encode them with %20.

These elements have different URL values:

<a href=" foo">…</a> <!-- the URL is <foo> -->
<a href="%20foo">…</a> <!-- the URL is < foo>, i.e., <%20foo> -->

These elements have the same URL value:

<a href="%20foo">…</a>
<a href="%20foo ">…</a>
<a href="%20foo   ">…</a>
<a href=" %20foo">…</a>
<a href="    %20foo">…</a>
<a href="    %20foo    ">…</a>