HTML Character References: Escape Special Characters
Display reserved characters
Character references allow you to write a character using a special sequence. In normal HTML text, < represents the less-than sign and & represents an ampersand. End named references with a semicolon. Use > for greater-than when showing code for clarity.
<p>5 < 8 & 9 > 2</p><p> <code><h1></code></p>The first line displays comparison signs; the second displays a literal tag.
Named and numeric forms
Named references use a name, such as ©. Numeric references use a decimal number after &# or a hexadecimal number after &#x; both identify Unicode code points. In UTF-8 pages, ordinary Unicode characters can usually be written directly.
<p>© / © / © / ©</p><p>10 kg</p>The four copyright symbols are identical. The nonbreaking space prevents a normal line break between 10 and kg.
Escape according to context
Inside a double-quoted attribute value, use " for a literal double quote. ' represents an apostrophe. Character references are not a general-purpose security sanitizer: handling untrusted input depends on where it is inserted. Do not use repeated nonbreaking spaces for layout.
<p title="A "quoted" word">A quote in an attribute.</p><p>&lt; displays the reference text.</p>The title contains literal quotes. The second line shows the text < rather than decoding it twice.
Practice and next steps
Display a literal opening and closing HTML tag and the same copyright symbol using two different reference forms.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Escaping and character references — Knowledge Check
Apply the concepts from Escaping and character references.
Which source displays a literal less-than sign?
Which is a hexadecimal character reference?
Which reference prevents a normal break between a number and its unit?
Which source displays the literal text < rather than a less-than sign?
Which reference represents a double quote within a double-quoted attribute?