HTML URI/URL Encoding

Can I use spaces (blank space chars) in links (link href),
images (img src) and other URLs?

Answer:

No. Spaces in URIs/URLs should be encoded using %20

 

URL Encoding - Percent (%) Encoding

URLs use the ASCII charset.
URL encoding replaces space characters with "%20" (percent followed by the ASCII code for a blank space).

 

Sample:

Invalid URI: http://www.yourdomain.tld/sample image.jpg

Correct URI: http://www.yourdomain.tld/sample%20image.jpg

 

Image Sample:

<img alt="alternate text" src="sample%20image.jpg">

 

Link href= Sample:

<a href="some%20other%20page.html">Link Anchor Text</a>

 

Notes:

  • It's best practice to avoid spaces in URLs, e.g. use "sample-image.jpg" instead of "sample image.jpg"
  • Some browsers might auto-correct invalid links in double-quotes with un-encoded spaces in it, but you should not rely on it.
  • In general: avoid un-encoded spaces and other unsafe ASCII chars in URIs.
  • Since the percent sign is used for URL encoding, any other appearance of a percent % in an URL should be encoded to %25.
  • Spaces inside a query should be replaced using the plus sign, e.g. http://www.yourdomain.tld/help%20me.php?topic=green+blue
  • Javascript contains the "encodeURI()" function to encode an URI.
  • php contains the "rawurlencode()" function to encode an URI.
Disclaimer: The information on this page is provided "as is" without warranty of any kind. Further, Arclab Software does not warrant, guarantee, or make any representations regarding the use, or the results of use, in terms of correctness, accuracy, reliability, currentness, or otherwise. See: License Agreement