View Full Version : Why do we use " instead of ' ?

01-05-2004, 02:46 AM
First off, I've always used " as I believed it was the only option. Now I'm working with a C++ guy who uses ' instead. I told him it wouldn't validate, but it does?? (I didn't check his doctype however..)
I did a little investigating at the good 'ol W3C and found although they accept both, ALL W3C examples are shown with ".
In another thread, both Skyzyx and me' insisted that XHTML dictates the use of " for all attributes. Now I'm not doubting either of these guys as I've learnt a fair bit from both, but why???

I'd really like to see some kind of official documentation or just reasoning that I'm not aware of 'cause this one's been floating round in my head for a week or two.

01-05-2004, 02:51 AM
As long as you quote attributes (with ' or "), it really doesn't matter. De facto standard is ". " seems to make the values stand out for me anyway. But they're both equally valid, I'm pretty sure.

01-05-2004, 05:21 AM
Yes, both are perfectly valid. However, I generally recommend using double-quotes in (X)HTML because that's how the majority of HTML code and documentation is written.

Also, I tend to use single-quotes in JavaScript, and it helps me differentiate easily between the two.

The most important thing is to be consistent and get a style going.

The point that I was making in that other thread was that all attributes needed to be quoted in the first place. The guy I was trying to help hadn't quoted hardly anything at all, and was wondering about validation errors.

01-05-2004, 05:59 AM
Well, just my two cents, but when someone suggests quoting something, the first thing that comes into my head is "." So, I guess there's reasoning enough, structure attribute quotes in the same manner you would in everyday life.

<<Unless, of course, you're from overseas and use another form of punctuation,>> I replied.


01-05-2004, 12:29 PM
Originally posted by rmedek
<<Unless, of course, you're from overseas and use another form of punctuation,>> I replied. Good point, but we have ways of getting round this (http://www.w3.org/TR/CSS21/generate.html#propdef-quotes).

I didn't realise you could quote attribute values with ' myself, so I popped over to the w3c and had a look. After a few minutes of searching (jeez, you gotta hate the XML spec), I found it:
The Name in the start- and end-tags gives the element's type. [Definition: The Name-AttValue pairs are referred to as the attribute specifications of the element], [Definition: with the Name in each pair referred to as the attribute name] and [Definition: the content of the AttValue (the text between the ' or " delimiters) as the attribute value.]Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.[quote source (http://www.w3.org/TR/xml11/#dt-attr)]]

Well, there you go. I've never thought of using ", and I don't think this'll change things, but if you feel more comfortable this way, why not!?

*goes back and edits other thread*

01-05-2004, 08:50 PM
i tend to prefer using the double quotes, unless i'm outputting the code via PHP, and i'm too lazy to go round espacing all my double quotes...

01-06-2004, 04:35 AM
Readme, you're supposed to use double quotes for strings really. Seeing as PHP is a C scripting language and in C 'a' would be 96 ( I think ) and "a" would be a ... It's just wrong in my oppinion to use ' for strings greater than 1 character in length ..

01-06-2004, 01:20 PM
Originally posted by me'
Good point, but we have ways of getting round this (http://www.w3.org/TR/CSS21/generate.html#propdef-quotes).

Thanks for that one me'.
:o :D

01-06-2004, 05:46 PM
Originally posted by mindlessLemming
Thanks for that one me'.
:o :D I should point out that the support for :lang is extremely poor, so I'd use:
html[xml:lang|="fr"] span.example { quotes: "" "" }where the support is slightly better (moz, op off the top of my head)

01-08-2004, 08:44 AM
I do know in the C++ language that the double quotes are used for strings and the signle quotes are used for a character for instance it would be a good thing to use for a menu. I do not know if that is one of the answers you are looking for, or I might have hit it way way off...

01-08-2004, 02:03 PM
The C or PERL (and later scripting languages using the same idea, such as Ruby, PHP etc.) difference between single and double quotes does not in any way affect SGML, XML or HTML. Neither of these do any differentiation between them. In fact, SGML is really lax and allows the language to specify other quoting characters if you want them.

However, there are some rather good reasons for why you should use double instead of single quotes:
- English language as well as many other languages uses single quotes (or apostrophes really, but ASCII doesn't differentiate those two characters) in regular writing, which means that you can't enclose such writing in single quotes without prematurely closing the string. Since SGML/HTML/XML character escaping is rather wieldy, you may want to avoid it altogether, by using a character not present in regular writing for string delimiter.
- JavaScript may also use either single or double quotes for strings, but unlike SGML/HTML/XML character escaping, the JavaScript character escaping is small and makes the raw string only marginally less readable, which is the reason JavaScript traditionally uses single quotes. If we use double quotes for SGML/HTML/XML strings, that makes enclosing a single quoted JavaScript string in for example event handlers that much easier.
- XML makes quoting strings necessary, but HTML is laxer and doesn't need it unless the attribute contains a space (parsers ignore that it should only be numbers that are allowed to be unquoted, really). As a preparation for XML and because it makes the code look nicer, you should use some quoting character for HTML strings even for numbers.

01-08-2004, 05:19 PM
Yeah, single and double quotes are interchangeable.

But it will probably become more clear as to why when working with JavaScript.


onclick = "document.location('http://www.google.com')";

Here I could've started with the single quotes, but the inner quotes would've been double. And vice versa!

01-08-2004, 05:28 PM
Ok I think this thread has achieved it purpose (possibly several posts back) and the same thing has been essentially said by a whole bunch of different people :) time to close this all you can post buffet.