tags:

views:

194

answers:

5

Possible Duplicate:
When did single quotes in HTML become so popular?

Should I use ' or " when coding, for example width='100px' or width="100px". Is it only a matter of taste, or does it matter to the browsers?

The reason why I ask, I have always used "" for everything, so when I code with PHP, I have to escape like this:

echo "<table width=\"100px\>"";

But I've found out that I will probably save 2 minutes per day if I do this:

echo "<table width='100px'>"

Fewer key strokes. Of course I could also do this:

echo '<table width="100px">'

What should the HTML look like; 'option1' or "option2"?

+6  A: 

Yes, it's a matter of taste. It makes no difference in HTML. 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.

However note that the width attribute is deprecated, even though it is still supported in all major browsers. Actually the width attribute is not deprecated when used in <table> in HTML 4.01. Only when used in <hr>, <pre>, <td>, <th> (Source 1, 2, 3, 4).

Daniel Vassallo
+1 - width attribute has been deprecated to what?
VoodooChild
... In favour of `style="width: 100px;"`... **EDIT:** Actually I'm note sure if it has been deprecated when used in a `<table>`... Checking...
Daniel Vassallo
@Voodoo: Updated my answer :)
Daniel Vassallo
A: 

For me it is better to use ' than "". PHP will parse the string wrap with "" and using ' is faster because PHP will treat it just a string and no more parsing.

Manie
A: 

echo '<table width="100px">'

Because other HTML tags are probably with double quotes, keep it clean, you don't need to escape quotes this way.

Webarto
A: 

I agree with Daniel - it's a matter of taste.

Personally, I use double quotes when assigning values to attributes (e.g. width="100%"). Also, if you deal with JSON, strictly speaking, the names and values should be surrounded with double quotes ("").

David Hoerster
Why the down vote?
David Hoerster
no idea, here I will make it even :)
VoodooChild
Thanks! I appreciate it.
David Hoerster
+1  A: 

HTML5 supports attributes specified in any of these four different ways:

  • Empty attribute syntax: <input disabled>
  • Unquoted attribute value syntax: <input value=yes>
  • Single-quoted attribute value syntax: <input type='checkbox'>
  • Double-quoted attribute value syntax: <input name="be evil">
cxfx