Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to use single quotes for HTML attributes?

Recently I've been seeing a lot of this:

<a href='http://widget-site-example.com/example.html'>     <img src='http://widget-site-example.com/ross.jpg' alt='Ross&#39;s Widget' /> </a> 

Is it valid to use single quotes in HTML? As I've highlighted above it's also problematic because you have to escape apostrophes.

like image 712
Ross Avatar asked Oct 28 '08 10:10

Ross


People also ask

Do you need quotes for HTML attributes?

The HTML specification says: Attributes are placed inside the start tag, and consist of a name and a value, separated by an = character. The attribute value can remain unquoted if it doesn't contain spaces or any of " ' ` = < or > . Otherwise, it has to be quoted using either single or double quotes.

Should we quote the attribute values?

The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), and periods (ASCII decimal 46). We recommend using quotation marks even when it is possible to eliminate them.

Should all values be placed in quotation marks for HTML?

The attribute value can remain unquoted if it doesn't contain ASCII whitespace or any of " ' ` = < or >. Otherwise, it has to be quoted using either single or double quotes. The value, along with the "=" character, can be omitted altogether if the value is the empty string.


2 Answers

It's certainly valid to use single quotes (HTML 4.01, section 3.2.2). I haven't noticed such a trend, but perhaps there's some framework that powers web sites you've visited that happens to quote using single quotes.

like image 103
Greg Hewgill Avatar answered Sep 18 '22 13:09

Greg Hewgill


I find using single quotes is handy when dynamically generating HTML using a programming language that uses double quote string literals.

e.g.

String.Format("<a href='{0}'>{1}</a>", Url, Desc) 
like image 45
Ady Avatar answered Sep 20 '22 13:09

Ady