: Are spaces in "href" valid? Is <a href=" #id "> (note spaces) correct (X)HTML? Does it mean the same as <a href="#id">?
Is <a href=" #id "> (note spaces) correct (X)HTML?
Does it mean the same as <a href="#id">?
More posts by @Kaufman445
4 Comments
Sorted by latest first Latest Oldest Best
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>
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>
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.
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.