Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the functional differences between single-quoted vs double-quoted html attributes?

Tags:

html

c#

Let sEncodedHref represent an HttpUtility.HtmlAttributeEncode(..)'d string.

Are there any functional differences between generated html like this:

String.Format(@"<span class='blue' src='{0}'>", sEncodedHref);

vs. generated html like this:

String.Format(@"<span class=""blue"" src=""{0}"">", sEncodedHref);

I've been under the impression that the single-quoted variant is both less supported and less "safe", however I have trouble providing reasons to support that argument.

like image 532
Jude Allred Avatar asked Dec 16 '22 23:12

Jude Allred


2 Answers

There is no functional difference. Quoting the W3C on SGML and HMTL:

By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa.

...

In certain cases, authors may specify the value of an attribute without any quotation marks. The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), periods (ASCII decimal 46), underscores (ASCII decimal 95), and colons (ASCII decimal 58). We recommend using quotation marks even when it is possible to eliminate them.

like image 162
Daniel Vassallo Avatar answered Dec 24 '22 00:12

Daniel Vassallo


Absolutely no functional difference. Both are valid, although double quotes are more widely used and are preferred.

like image 25
Darin Dimitrov Avatar answered Dec 24 '22 00:12

Darin Dimitrov