Tutorial 17: Shady Characters - HTML with Style | 6 | WebReference

Tutorial 17: Shady Characters - HTML with Style | 6


Tutorial 17: Shady Characters

Character entity references

Character entity references allow you to use a simple, memorable name instead of a number to refer to a character. The upside of this approach is twofold: First of all, as humans, we are more likely to remember a name like "quot" instead of the number 34 (which is the number to use for a quotation mark in a numerical character reference). Secondly, browsers will tend to handle character entity references they know about more reliably than numerical character references, as character entity references can refer to a character without making assumptions about the character set or encoding. A user agent should understand the entity reference "quot" as the character with number 34, but even if it doesn't, it's still a quotation mark.

The downside of character entity references is again twofold: First, some of the names of character entity references are not easy to remember or even decipher - who can figure out that "raquo" stands for "right-pointing double angle quotation mark" (or even know what that is). Secondly, not all user agents understand all character entity references. HTML 4.0 may define a whole slew of these, but Netscape will probably crease its eyebrows at more than half of them.

The syntax for a character entity reference is an ampersand (&) followed by the name of the entity, followed by a semi-colon (;). Here are some examples:

<P>The pub we went to was named the &laquo;Horse &amp;
Carriage&raquo;. They served fine draught ale for just

The pub we went to was named the «Horse & Carriage». They served fine draught ale for just £1.20.

Since character entity references refer to characters in the document character set, it is generally better to use the actual character encoded in the document instead of using them, for the same reasons that numerical character references should be avoided. However, there is one important exception: Look at the example above again. What if I had used the ampersand character directly? A user agent reading the document might be mistaken into believing that it denoted a character reference. And what if I literally wanted to include the letters (&amp;) in my document? More importantly, what if I wanted to use the less-than or greater-than sign in my document, without implying that a tag begins or ends at that point? The answer is to use the character entity references &lt;, &gt; and &amp; whenever you want to insert a greater-than, less-than, or ampersand character in your document without implying an entity or character reference, or a tag. The following example illustrates this:

<P>Elementary mathematics states that 3 &lt; 5 &gt; 1.</P>
<P>To insert an ampersand in an HTML document, you should use the
syntax &amp;amp;</P>

Elementary mathematics states that 3 < 5 > 1.

To insert an ampersand in an HTML document, you should use the syntax &amp;

If I did not use named character entity references in the above example, a user agent would be mystified by the start-tag for the mysterious "5" element, and render the ampersand character entity reference as an ampersand, making the tutorial writer's job quite impossible. For this reason, you must always use character entity references when using the ampersand, greater-than and less-than characters in your documents.



All Rights Reserved. Legal Notices.

URL: http://www.webreference.com/html/tutorial17/5.html

Produced by Stephanos Piperoglou
Created: December 02, 1999
Revised: December 15, 1999