46

In HTML, What is the preferred way to specify html codes like ", and what is the major differences? For example:

&quot;    <!-- friendly code -->
&#34;     <!-- numerical code -->
&#x22;    <!-- hex code -->

Which one should I use and would it ever be an issue if one of these gets deprecated?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161

3 Answers3

81

There really aren't any differences.

&quot; is processed as &#34; which is the decimal equivalent of &x22; which is the ISO 8859-1 equivalent of ".

The only reason you may be against using &quot; is because it was mistakenly omitted from the HTML 3.2 specification.

Otherwise it all boils down to personal preference.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
12

Google recommend that you don't use any of them, source.

There is no need to use entity references like &mdash, &rdquo, or &#x263a, assuming the same encoding (UTF-8) is used for files and editors as well as among teams.

Is there a reason you can't simply use "?

William Isted
  • 11,641
  • 4
  • 30
  • 45
Andy
  • 14,427
  • 3
  • 52
  • 76
  • 5
    No reason really @Andy. For years I have been conditioned to use html codes whenever and wherever possible. Mainly due to rendering content from databases, WordPress posts, the fact that junky characters come from people copying and pasting Word documents into textareas, etc. So my conditioning has always been to utilize html codes as a practice for content creation. I was just curious as to why there needs to be 3 different ways to code a double quotes in html codes, for example. – H. Ferrence Feb 28 '13 at 12:48
  • 4
    Hey @Andy -- your link to Google's recommendations / Style Guide is actually very useful. I just perused it and it sets a lot of great standards (what I like to call my personal "Best Practices"). Thanks for sharing that link! I still need to wrap my mind around the HTML Codes method but this document gives me a lot to think about. – H. Ferrence Feb 28 '13 at 13:05
  • 3
    Well, there is a good reason for that. I just had to fix a situation where for example length of 12" is saved in a DB and later becomes a part of image alt attribute - generated by JavaScript. So something linke var myAlt = "some stuf 12" long"; will break the script, but var myAlt = "some stuf 12" long"; will work fine. – vector Jun 26 '14 at 13:05
  • 2
    @vector Then you should escape using `\"`. – Andy Jun 26 '14 at 13:17
  • The only time you would need to use the code is if you are converting user-entered data to html and you're escaping it manually, which is a bad idea. Pretty much all frameworks have a way to escape this stuff for you. Edit: Just saw the below answer, which is another use case. – frodo2975 Sep 29 '17 at 20:19
  • 1
    use “ and ” when referencing or quoting someone. &quo; for everything else. – MC9000 Jul 19 '19 at 16:40
5

There is no difference, in browsers that you can find in the wild these days (that is, excluding things like Netscape 1 that you might find in a museum). There is no reason to suspect that any of them would be deprecated ever, especially since they are all valid in XML, in HTML 4.01, and in HTML5 CR.

There is no reason to use any of them, as opposite to using the Ascii quotation mark (") directly, except in the very special case where you have an attribute value enclosed in such marks and you would like to use the mark inside the value (e.g., title="Hello &quot;world&quot;"), and even then, there are almost always better options (like title='Hello "word"' or title="Hello “word”".

If you want to use “smart” quotation marks instead, then it’s a different question, and none of the constructs has anything to do with them. Some people expect notations like &quot; to produce “smart” quotes, but it is easy to see that they don’t; the notations unambiguously denote the Ascii quote ("), as used in computer languages.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • I wish you had cited a reference regarding "smart quotes." For starters, there is http://www.personal.psu.edu/ejp10/blogs/gotunicode/2007/02/smart_quotes_entity_codes_1.html. – David A. Gray Jul 15 '21 at 17:48