As you may have discovered, HTML specifies that certain are “reserved” or serve a special purpose for browsers. The most obvious conflicts are the greater than and less than characters (<
, >
), since these identify tags to browsers in HTML. Any character that may be accidentally interpreted as markup by the browser should be replaced with its escape code equivalent. There are also characters that have no easy “keyboard” equivalent, (such as the copyright symbol - © ) in which case an escape code must be used.
Question: So what is a developer to do if they need to write some math (or something else less stressful than including comparison operators in their page)?
Answer: Use special codes, known as escape characters, that the browser can use to render the intended character!
This is not a fun process, but one that is necessary since HTML is a language.
NOTE: Also, for those of you who are more “security” minded or wanting to protect yourself and your client from “middle-man” attacks, using escape characters is critical, as evidence by some discussions around the web.
Escape codes always start with an ampersand (&
) character followed by the individual code and finished with a semicolon (;
).
All characters have a numerical, decimal-based (in addition to hexadecimal), value. In HTML, prepend decimal this value with the ampersand identifier as well as a number sign (&#
).
Using escape character codes in HTML text is straight forward. Simply replace the character you want with its escape code equivalent in the text.
For example, we could write the word “CAT”, by wrapping the individual letter’s decimal values (67, 65, and 84) in the escape code values as;
<h1>Where is the CAT ?</h1>
There are two ways of including an escape character in your HTML text. The first, mentioned above, simply utilizes the decimal based character code. This of course can be difficult to remember for the characters you may need.
Characters used often have special codes that are more “human readable”, known as “entity names”. For example, the less than and greater than characters (<
& >
) have entity names ‘lt’ and ‘gt’, respectively. To use an entity name, prepend it with the ampersand and appended with a semicolon. So to use <
& >
as actual characters in HTML write <
and >
.
These are some of the more commonly used escape code characters, along with their entity name and decimal code.
Symbol | Name | Entity Name | Code |
---|---|---|---|
© | Copyright | © |
© |
® | Registered trademark | ® |
® |
™ | Trademark | ™ |
™ |
< | Less than | < |
< |
> | Greater than | > |
> |
& | Ampersand | & |
& |
" | Quotation mark | " |
" |
¢ | Cent | ¢ |
&162; |
£ | Pound | £ |
&163; |
¤ | Currency | ¤ |
&164; |
¥ | Yen | ¥ |
&165; |
€ | Euro | € |
&8364; |
Read pages 194-199 of Chapter 08 in Duckett.