9

Is <a href=" #id "> (note spaces) correct (X)HTML?

Does it mean the same as <a href="#id">?

1

4 Answers 4

9

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>
0

Why would you do this? Who writes code like that? That's unnecessary space in between "".

Literal spaces are not allowed in URLs and they are not part of the data. just formatting for human consumption.

You need to remove the whitespace. Some browsers render it as a space.

Spaces are valid. And they both are different because, one has spaces, other doesn't. That means, that formatting or link is different.

0

Yes, it will be automagically corrected by the browser ,anyway if using spaces you get an error you can write them in another way:

a URL, so a href, supports spaces, just replace them with "%20" Anyway if you write

<a href="%20#id"> </a>

This will result in a " #id" and not "#id"

Eg:

<a href="?How%20Are%20You">How are you?</a>
-1

You can run your markup through the validator yourself to find out if what you wrote is valid. But valid markup is not always an indication of valid transport and spaces must be URL encoded (and the hash mark also).

You can learn more by searching Stackoverflow. In particular, this SO answer. Your characters are not safe to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.