What to use " or ' when coding [duplicate] - html

This question already has answers here:
Closed 12 years ago.
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"?

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).

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">

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.

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 ("").

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.

Related

Different delimiters to seperate attributes in HTML tags

What are different delimiters used to separate HTML attributes when spaces,double quotes,single quote are not allowed
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). [w3.org]
HTML attributes follow the SGML definition of attributes, therefore you cannot have a HTML attribute without these characters. If " and ' are not allowed then you cannot write valid HTML.

HTML attributes without quotation marks?

I've always thought html requires quotation marks in his properties:
<div class="service_definition"> or <div class='service_definition'>
But recently i noticed that the w3 validator doesn't recognize the following as an error:
<div class=service_definition>
So is it all right if i omit the quotation marks? Or are there any restrictions?
Always use quotation marks.
I don't believe that HTML properties without quotation marks are classed as Invalid HTML, but they will potentially cause you problems later on down the line.
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 ".
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.
Source: http://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.2
I think they're a great way of clearly defining when an attribute value starts and finishes.
Take for example the class attribute which can have multiple classes seperated by spaces:
<div class="classa classb" id="123">
This clearly shows the browser that my classes are classa and classb, with an element id of 123.
Take away the quotation marks and we've got:
<div class=classa classb id=123>
A browser could now interpret this as 3 classes, with no id. classa, classb and id=123.
Or it may even interpret it as 3 attributes. class="classa", classb="" and id="123"
(Even stackoverflow's syntax styling is struggling with this one!)
The rules depend on HTML version.
In all flavors of XHTML, attribute values must always appear in quotes, since XHTML is XML. Validators naturally check this. In browser practice, it's different. In genuine XML mode, a missing quote aborts document interpretation: the content is not shown, only an error message (which may contain an extract of source code) is shown to the user. But when XHTML is served as HTML, which is the normal way (especially due to the XHTML-illiteracy of old versions of IE) browsers treat it by HTML rules.
Otherwise in HTML, the formal rules vary by specification, but browsers accept attribute values without quotation marks. HTML5 drafts reflect this liberal attitude: the quotes are needed only if the value contains a space, a line break, grave (`), equals sign (=), less than sign (<), greater than sign (>), Ascii quote ("), or Ascii apostrophe (').
What you do in practice is large a matter of convention, and it should depend on the opinions of coworkers or others who may work on your code, rather than public opinion. Many people think that the restrictive syntax of XHTML is cool. Others may think that unnecessary quotes mess up the code and have even some risks: whenever you need to use some characters in pairs, there’s always a chance of forgetting the closing component or mistyping it.
You can omit the quotes from an attribute value if the value consists of the following characters only (cf to the technical concept of name):
letters of the English alphabet (A to Z, a to z)
digits (0 to 9)
periods .
hyphens -
Thus, WIDTH=80 and ALIGN=CENTER are legal shorthands for WIDTH="80" and ALIGN="CENTER". A reference to a URL like HREF=foo.html is acceptable, but in general URLs must be quoted when used in attributes, e.g. HREF="http://www.hut.fi/". - Some browsers are more permissive. Some browsers may even accept elements with a starting quote but without any closing quote. Such use is very bad practise.
refer http://www.cs.tut.fi/~jkorpela/HTML3.2/3.4.html
Sometimes you can use HTML properties without quotation marks.
I recently created a text document in SharePoint that links to a company web application. I was getting an error message when using quotation marks for an href value (website url). Which is technically correct.
I noticed in the DOM, that a set of quotes was being added automatically to the href value by the browser. So I tried entering the href value without quotation marks, this fixed the navigation error.
The w3 validator shows it as an error for me:
An attribute value specification must be an attribute value literal unless SHORTTAG YES is specified.

Quotation marks in HTML attribute values?

This may seem like a realy basic question but...
How do you use double speech marks in HTML code (alt tags and the such)?
For example..
I'm trying to set a tag in my webpage to Opening Credits for "It's Liverpool" but it's limiting it to Opening Credits for.
You'll want to use the corresponding HTML entity in place of the quotes:
<span alt="Opening Credits for "It's Liverpool"">A span</span>
You can normally avoid the issue by using appropriate language-dependent quotation marks, instead of Ascii quotation marks, which should be confined to use as delimiters in computer code. Example:
alt="Opening Credits for “It’s Liverpool”"
or (in British English)
alt="Opening Credits for ‘It’s Liverpool’"
Should you really need to use Ascii quotation marks inside an attribute value, use Ascii apostrophes as delimiters:
alt='The statement foo = "bar" is an assignment.'
In the extremely rare case where an attribute value really needs to contain both an Ascii quotation mark and an Ascii apostrophe, you need to escape either of them (namely the one you decide to use as attribute value delimiter):
alt="The Ascii characters " and ' should not be used in natural languages."
or
alt='The Ascii characters " and ' should not be used in natural languages.'
Note that these considerations are relevant only inside attribute values. In element content, both " and ' can be used freely:
<strong>The Ascii characters " and ' should not be used in natural languages.</strong>

escaping inside html tag attribute value

I am having trouble understanding how escaping works inside html tag attribute values that are javascript.
I was lead to believe that you should always escape & ' " < > . So for javascript as an attribute value I tried:
It doesn't work. However:
and
does work in all browsers!
Now I am totally confused. If all my attribute values are enclosed in double quotes, does this mean I do not have to escape single quotes? Or is apos and ascii 39 technically different characters? Such that javascript requires ascii 39, but not apos?
There are two types of “escapes” involved here, HTML and JavaScript. When interpreting an HTML document, the HTML escapes are parsed first.
As far as HTML is considered, the rules within an attribute value are the same as elsewhere plus one additional rule:
The less-than character < should be escaped. Usually < is used for this. Technically, depending on HTML version, escaping is not always required, but it has always been good practice.
The ampersand & should be escaped. Usually & is used for this. This, too, is not always obligatory, but it is simpler to do it always than to learn and remember when it is required.
The character that is used as delimiters around the attribute value must be escaped inside it. If you use the Ascii quotation mark " as delimiter, it is customary to escape its occurrences using " whereas for the Ascii apostrophe, the entity reference &apos; is defined in some HTML versions only, so it it safest to use the numeric reference ' (or ').
You can escape > (or any other data character) if you like, but it is never needed.
On the JavaScript side, there are some escape mechanisms (with \) in string literals. But these are a different issue, and not relevant in your case.
In your example, on a browser that conforms to current specifications, the JavaScript interpreter sees exactly the same code alert('Hello');. The browser has “unescaped” &apos; or ' to '. I was somewhat surprised to hear that &apos; is not universally supported these days, but it’s not an issue: there is seldom any need to escape the Ascii apostrophe in HTML (escaping is only needed within attribute values and only if you use the Ascii apostrophe as its delimiter), and when there is, you can use the ' reference.
&apos; is not a valid HTML reference entity. You should escape using '

Why shouldn't `&apos;` be used to escape single quotes?

As stated in, When did single quotes in HTML become so popular? and Jquery embedded quote in attribute, the Wikipedia entry on HTML says the following:
The single-quote character ('), when used to quote an attribute value, must also be escaped as ' or ' (should NOT be escaped as &apos; except in XHTML documents) when it appears within the attribute value itself.
Why shouldn't &apos; be used? Also, is " safe to be used instead of "?
" is on the official list of valid HTML 4 entities, but &apos; is not.
From C.16. The Named Character Reference &apos;:
The named character reference &apos;
(the apostrophe, U+0027) was
introduced in XML 1.0 but does not
appear in HTML. Authors should
therefore use ' instead of
&apos; to work as expected in HTML 4
user agents.
" is valid in both HTML5 and HTML4.
&apos; is valid in HTML5, but not HTML4. However, most browsers support &apos; for HTML4 anyway.
&apos; is not part of the HTML 4 standard.
" is, though, so is fine to use.
If you need to write semantically correct mark-up, even in HTML5, you must not use &apos; to escape single quotes. Although, I can imagine you actually meant apostrophe rather then single quote.
single quotes and apostrophes are not the same, semantically, although they might look the same.
Here's one apostrophe.
Use ' to insert it if you need HTML4 support. (edited)
In British English, single quotes are used like this:
"He told me to 'give it a try'", I said.
Quotes come in pairs. You can use:
<p><q>He told me to <q>give it a try</q></q>, I said.<p>
to have nested quotes in a semantically correct way, deferring the substitution of the actual characters to the rendering engine. This substitution can then be affected by CSS rules, like:
q {
quotes: '"' '"' '<' '>';
}
An old but seemingly still relevant article about semantically correct mark-up: The Trouble With EM ’n EN (and Other Shady Characters).
(edited) This used to be:
Use ’ to insert it if you need HTML4 support.
But, as #James_pic pointed out, that is not the straight single quote, but the "Single curved quote, right".
If you really need single quotes, apostrophes, you can use
html | numeric | hex
‘ | ‘ | ‘ // for the left/beginning single-quote and
’ | ’ | ’ // for the right/ending single-quote