Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 122 ยท Text Content

HTML Entities

Learn when you need HTML entities, how named, decimal and hex references work, how to use no-break spaces and soft hyphens, plus a table of common ones.

A few characters have a special job in HTML. The less-than sign < starts a tag, and the ampersand & starts something called a character reference. If you want those characters to appear as text, you cannot always type them directly โ€” you need another way to write them.

That other way is a character reference, usually called an HTML entity: a short code, starting with & and ending with ;, that the browser replaces with a single character. &lt; becomes <, &amp; becomes &, and so on. In this lesson you will learn the three ways to write them, which characters really need them, and two invisible characters that are surprisingly useful.

The characters that need escaping

You already met the problem in Code, Keyboard Input and Program Output: if you type <b> in your text, the browser starts a bold element instead of showing the letters. Here are the characters to watch:

Character Why it is special Write it as
< Starts a tag &lt;
& Starts a character reference &amp;
> Ends a tag (harmless in normal text) &gt; (optional)
" Ends a double-quoted attribute value &quot; (inside such values)
' Ends a single-quoted attribute value &apos; (inside such values)

In everyday text, < and & are the two that matter. The quote characters only cause trouble inside an attribute value that uses the same kind of quote:

HTML
<abbr title="The &quot;Big Apple&quot;">NYC</abbr>
Showing reserved characters

In the broken line, the browser saw <b followed by a space and treated it as the start of a bold tag, swallowing the rest of the sentence. Escaping the < fixes it.

What about a lone &? Modern HTML is forgiving: an ampersand followed by a space, like Salt & Pepper, is not mistaken for a reference. But & followed by letters can be โ€” &copy=2 inside text turns into ยฉ=2. Writing &amp; every time is the safe habit, including inside URLs in href attributes.

Three ways to write a reference

Every character can be written as a reference in up to three ways. All three below produce the same copyright sign, ยฉ:

Form Example Notes
Named &copy; Easy to read; only exists for some characters.
Decimal &#169; &# + the character's number.
Hexadecimal &#xA9; &#x + the number in hex.

The number is the character's Unicode code point โ€” its unique number in the Unicode standard, often written as U+00A9. You will learn more about Unicode in Character Encoding and UTF-8. Numeric references work for every character, including the thousands that have no name. Named references are just friendly shortcuts for a set of about two thousand common ones.

Named, decimal and hex references

A few rules keep references working reliably:

  • Names are case-sensitive. &Eacute; is ร‰, &eacute; is รฉ, and &EURO; is not a reference at all.
  • Always end with a semicolon. Browsers recognize some old names without it, but only as error recovery, and not in every context.
  • The x in hex references can be upper- or lowercase, and so can the hex digits: &#xa9; works too.

Do you still need entities?

Years ago, entities were the only safe way to put characters like รฉ, โ‚ฌ or โ€” into a page. Today every page should be saved and served as UTF-8, which can store any character directly. So you can simply type:

HTML
<p>Cafรฉ prices start at โ‚ฌ3 โ€” cash or card.</p>

That is easier to read and edit than Caf&eacute; prices start at &euro;3 &mdash; cash or card. The browser shows the same result.

Entities are still the right tool in three situations:

  1. Reserved characters โ€” < and & in text, and quotes inside attribute values.
  2. Invisible or look-alike characters โ€” such as the no-break space and the soft hyphen below. In your editor they look like a normal space or like nothing at all, so an entity makes your intention visible in the code.
  3. Characters you cannot type easily โ€” when you know a code point but have no way to enter the symbol. The next lesson, Symbols and Emojis, is full of these.

The no-break space: &nbsp;

&nbsp; is a space that does not allow a line break. The words on either side stay together on the same line. Use it between things that would look odd if separated:

  • a number and its unit: 20&nbsp;kg, 9&nbsp;p.m.
  • a title or initial and a name: Dr.&nbsp;Okafor
  • a short label and its number: Chapter&nbsp;4

Unlike normal spaces, a no-break space is never collapsed, so several in a row produce several spaces. That tempts people to use &nbsp;&nbsp;&nbsp; for indentation or layout.

Do not use &nbsp; for spacing. Rows of no-break spaces break on different screen sizes and fonts, and screen readers may pause on them. Use CSS (margin, padding, gap) for space between things.

The soft hyphen: &shy;

&shy; is invisible โ€” unless a line break is needed at that point. Then the browser breaks the word there and shows a hyphen. It is perfect for very long words in narrow spaces, such as headings on a phone.

No-break spaces and soft hyphens

For automatic hyphenation of whole paragraphs, the CSS property hyphens: auto (with the right lang attribute) is usually better. &shy; gives you exact control over individual words.

Showing HTML code on a page

The most common real-world reason to escape is a tutorial, a blog post or documentation that shows HTML as an example. Every < in the sample must become &lt;, and every & must become &amp;, or the browser will treat your sample as real markup. Wrapping it in <pre> and <code> keeps the spacing and uses a monospaced font, but it does not escape anything for you.

Escaping a code sample

Look at the last sample closely. To show the text &amp;, you must escape its ampersand too, writing &amp;amp;. Escaping the > signs isn't strictly necessary, but many people escape them as well so the code looks symmetrical.

Let tools do the escaping. When you paste a large code sample, escaping by hand is slow and easy to get wrong. Many editors and online formatters can "escape HTML" for you, and server-side languages have functions for it, such as htmlspecialchars() in PHP. In JavaScript, setting textContent escapes automatically.

Entities and JavaScript

Character references are an HTML feature. The browser decodes them while it parses your markup, so by the time JavaScript sees the page, &amp; is just &. That leads to a few surprises when you set text from a script:

Entities through JavaScript
  • Reading textContent gives you the decoded characters: Fish & chips ยฉ 2026.
  • Assigning a string to textContent shows it literally, so the reader sees &amp; on the page. No escaping is needed, and none happens.
  • Assigning to innerHTML parses the string as HTML, so &amp; becomes &.

The practical rule: when you write text with JavaScript, use textContent and type the real characters. It is both simpler and safer, as The Document Object Model explains.

Common mistakes

  • Double escaping. Text that is escaped twice shows the code to the reader: "Salt &amp; Pepper" appears as Salt &amp; Pepper. This often happens when a content system escapes text that was already escaped. If you see &amp; or &lt; on a live page, something escaped once too often.
  • Forgetting the semicolon. &copy 2026 may work in one place and fail in another. Always close the reference.
  • Wrong case. &Copy; is not a reference. Copy names exactly.
  • Escaping in the wrong place. Inside <script> and <style> elements, entities are not decoded, so &lt; stays as four characters. Those elements follow their own rules.
  • Using entities instead of UTF-8. A page full of &eacute; and &uuml; is hard to read and edit. Save as UTF-8 and type the letters.

A table of common entities

Here are the references you are most likely to need. You do not have to memorize them โ€” bookmark this page.

Character Named Decimal Hex Meaning
< &lt; &#60; &#x3C; less than
> &gt; &#62; &#x3E; greater than
& &amp; &#38; &#x26; ampersand
" &quot; &#34; &#x22; double quote
' &apos; &#39; &#x27; apostrophe
(invisible) &nbsp; &#160; &#xA0; no-break space
(invisible) &shy; &#173; &#xAD; soft hyphen
ยฉ &copy; &#169; &#xA9; copyright
ยฎ &reg; &#174; &#xAE; registered trademark
โ„ข &trade; &#8482; &#x2122; trademark
โ€“ &ndash; &#8211; &#x2013; en dash (ranges: 9โ€“5)
โ€” &mdash; &#8212; &#x2014; em dash
โ€ฆ &hellip; &#8230; &#x2026; ellipsis
โ‚ฌ &euro; &#8364; &#x20AC; euro
ยฃ &pound; &#163; &#xA3; pound sterling
ยฐ &deg; &#176; &#xB0; degree
ร— &times; &#215; &#xD7; multiplication
รท &divide; &#247; &#xF7; division
โ†’ &rarr; &#8594; &#x2192; right arrow

Avoid references between 128 and 159. Very old pages used &#150; for a dash or &#153; for โ„ข, because those were the byte values in a Windows character set. Browsers still translate them for compatibility, but they are errors. Use the real Unicode numbers (&#8211;, &#8482;) or the named references instead.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which two characters most often need escaping in normal HTML text?

    < can start a tag and & can start a character reference. > is harmless in normal text, and quotes only matter inside attribute values.
  2. 2. Which of these all produce the euro sign โ‚ฌ?

    Names are case-sensitive, decimal references start with &# and hex references start with &#x. All end with a semicolon.
  3. 3. Your page is saved as UTF-8. Do you need an entity to show the letter รฉ?

    UTF-8 can store every character directly. Entities are mainly for reserved, invisible or hard-to-type characters.
  4. 4. What is the right use of &nbsp;?

    A no-break space prevents a line break between two words. Spacing and indentation are jobs for CSS.
  5. 5. A live page shows Rock &amp; Roll instead of Rock & Roll. What went wrong?

    The ampersand was escaped once into &amp;, then that ampersand was escaped again into &amp;amp;, which displays as &amp;.

Summary

  • Character references (entities) start with & and end with ;; the browser replaces them with one character.
  • Escape < as &lt; and & as &amp; in text; escape quotes inside attribute values.
  • Every character has a numeric form โ€” decimal &#169; or hex &#xA9; โ€” based on its Unicode code point; some also have names like &copy;.
  • Names are case-sensitive; always include the semicolon.
  • With UTF-8 you can type most characters directly; keep entities for reserved, invisible and hard-to-type characters.
  • &nbsp; keeps words together; &shy; marks where a long word may break.

Next: Symbols and Emojis โ€” arrows, math, currencies and emoji, all from Unicode.