views:

3375

answers:

10

Recently I've been seeing a lot of this:

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

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

+12  A: 

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.

Greg Hewgill
Funnily enough it's mainly sites that give external code out - for example a stats image for Ohloh or as in http://www.pushuptheweb.com/
Ross
HTML, not XHTML though
Joe Philllips
Single quotes are the default in HAML (http://haml-lang.com).
brad
+19  A: 

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)

Ady
StingyJack
String.Format(@"<a href=""{0}"">{1}</a>", Url, Desc) this is what you might consider :)
Cshift3iLike
or String.Format("<a href=\"{0}\">{1}</a>", Url, Desc)
rizzle
+4  A: 

When using PHP to generate HTML it can be easier to do something like:

$html = "<img src='$url' />";

than concatenating a string with a variable with a string, as PHP parses variables in double-quoted strings.

Dean
It's also easier than staring at $html = "<img src=\"{$url}\" />";As escaped quotes are all to easy to forget
Sekhat
A: 

What's against single quotes?

You can have single/double quotes all over your html code without any problem, as long as you keep the same quoting style inside a current tags ( many browser won't complain even about this, and validation just want that if you start with a quote, end with the same, inside the same propriety )

Free support to random quoting styles!!! (yay ;D )

Joshi Spawnbrood
+2  A: 

It's easier when you want to embed double quotes.

Same applies if you completely reverse the scenario... so not a good reason IMO
Joe Philllips
+1  A: 

In ASP.NET, it's easier to use single quotes if you're using data-binding expressions in attributes:

<asp:TextBox runat="server" Text='<%# Bind("Name") %>' />
Danko Durbić
A: 

Someone may use it in PHP to avoid escaping " if they're using double quoted string to parse variables within it, or to avoid using string concatenation operator.

Example:

echo "<input type='text' value='$data'/>";

instead of

echo "<input type=\"text\" value=\"$data\" />";

or

echo '<input type="text" value="' . $data . '" />';

Nowadays I always stick to using double quotes for HTML and single quotes for Javascript.

Imran
A: 

Single quotes are perfectly legal in (X)HTML. Using a backslash to escape them, on the other hand, isn't. <img src='http://widget-site-example.com/ross.jpg' alt='Ross\'s Widget' /> is an image with the alt text "Ross\", and empty s and Widget/Widget' attributes. The correct way of escaping an apostrophe in HTML is &#39;.

Ms2ger
Single quotes are not perfectly legal in XHTML.
Joe Philllips
Single quotes are legal XHTML, aren't they?
Bennett McElwee
d03boy: that's incorrect.
Ms2ger
A: 

In PHP, echo takes multiples parameters. So, if one would like to omit the concatenation operator, they could done something like and still use double quotes :

echo '<input type="text" value="', $data, '" />';
matv
A: 

Single quotes generate a cleaner page with less clutter. You shouldn't be escaping them in HTML strings and it's your choice which to use... my preference is single quotes normally and if I need to include a single quote in the string (e.g. as delimiters for a string inside the string), I use double quotes for one & single for the other.

Anthony