Josh (the blog)

I’ve delivered simple, clear and easy-to-use services for 20 years, for startups, scaleups and government. I write about the nerdy bits here.


@joahua

The attribute myth

I’ve been talking with Ben this evening about… markup, amongst other things, and discovered a conviction that using single quotes with an attribute is evil.

Clearing this up right now: it’s not, either in HTML 4 or XHTML (which retains much of the semantics of HTML 4, except where explicitly contradicted — “The semantics of the elements and their attributes are defined in the W3C Recommendation for HTML 4. These semantics provide the foundation for future extensibility of XHTML.”). Section 3 of the HTML specification states:

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. Authors may also use numeric character references to represent double quotes (") and single quotes ('). For double quotes authors can also use the character entity reference ".

Ben didn’t quite get the “Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa.” bit, so here’s a quick example of both:

<img alt=&#34;If you can see this, the image isn&#39;t working&#34; />
<img alt=&#39;You can probably see this because the &#34;src&#34; attribute is not defined&#39; />

Both are valid and should work fine (with the exception of the lack of src, obviously). Feel free to use single or double quote marks, safe in the knowledge neither is better than the other.