CHAPTER 13 · HTML

Characters, UTF-8 and entities: display the words and symbols you intend

Your page may contain several writing systems, a price, a copyright symbol or literal HTML tags. This chapter explains how saved characters and character references work together. It also separates HTML escaping from encoding a web address.

Download example files (ZIP)

What you will learn

  • Save and identify UTF-8 consistently.
  • Use named, decimal and hexadecimal character references.
  • Display angle brackets, ampersands and quotation marks safely in examples.
  • Choose a non-breaking space for a real text need.
  • Understand why entities are not encryption or a page-layout system.

Encoding belongs to the saved file

An encoding is a rule for representing characters as bytes. UTF-8 can represent English, Hindi and many other writing systems. Save the file in UTF-8 and identify that with a charset declaration near the start of head.

HTML fragment — part of a document
<meta charset="UTF-8">

Changing this line alone does not convert a file saved in another encoding. If characters appear as unexpected symbols, check both the editor's saved encoding and the document declaration. When content is served online, its HTTP response information also matters.

You can type ordinary Unicode characters directly in a UTF-8 file. Unicode is a system for identifying characters across writing systems. You do not need to replace every Hindi character, symbol or emoji with a numeric code.

Some characters have a job in HTML syntax

The < character can begin markup. An ampersand can begin a character reference. When you want readers to see those characters as text, use a suitable reference instead of accidentally asking the browser to interpret markup.

HTML fragment — part of a document
<p>Write <code>&lt;p&gt;</code> to start a paragraph.</p>
<p>Research &amp; practice belong together.</p>

The source spelling &lt; represents a less-than sign. The browser displays the character; it does not display that source spelling unless you escape its ampersand too.

Three ways to represent a character

KindExampleDisplayed character
Named reference&lt;<
Decimal numeric reference&#60;<
Hexadecimal numeric reference&#x3C;<

Decimal uses base ten, the ordinary counting system. Hexadecimal uses base sixteen, often found in technical notation. You do not need to convert numbers by hand to begin; recognise that these spellings can represent the same character.

Use the final semicolon in our references. Named references are case-sensitive; do not freely change their spelling. Some legacy missing-semicolon cases are parsed in limited contexts, but relying on them makes examples harder to reason about.

A useful starting reference table

Character or purposeNamed spellingNumeric spelling
Less than&lt;&#60;
Greater than&gt;&#62;
Ampersand&amp;&#38;
Double quote&quot;&#34;
Apostrophe&apos;&#39;
Copyright sign&copy;&#169;
Non-breaking space&nbsp;&#160;

You can use quotation marks directly in normal text. Escaping is particularly useful when a quote would otherwise close the attribute value that contains it.

HTML fragment — part of a document
<p title="Read the &quot;Lists&quot; chapter">Start with the reading task.</p>

The attribute value contains the quoted word without ending the value early. Single outer quotes could be another appropriate writing choice.

Complete example: display an HTML instruction

character-guide.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Character Reference Practice</title>
</head>
<body>
  <h1>Read the characters carefully</h1>
  <p>Example tag: <code>&lt;p&gt;Try one page.&lt;/p&gt;</code></p>
  <p>Research &amp; practice.</p>
  <p>The spellings &lt;, &#60; and &#x3C; display the same character.</p>
  <p>To show the spelling itself, write <code>&amp;lt;</code>.</p>
</body>
</html>
Browser previewDocument title: Character Reference Practice

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

The first paragraph displays literal tags. The last paragraph displays the source spelling &lt; because its ampersand was escaped. The reference is decoded once for the document's text; it does not repeatedly turn that resulting text into new markup.

Non-breaking spaces belong between words that should stay together

Ordinary text usually wraps at spaces. A non-breaking space can keep a particular pair together, such as a number and its unit.

HTML fragment — part of a document
<p>The walk is 2&nbsp;km.</p>

This keeps 2 and km together at that space. It does not mean the entire paragraph can never wrap. It is also not a reliable way to align columns or create a large visual gap. Use CSS for layout and ordinary spaces for ordinary prose.

On a very narrow display, long unbreakable content can still overflow. Use non-breaking characters only where the reading convention needs them.

Symbols, emojis and multilingual text

UTF-8 allows a direct rupee sign, a Hindi word or an emoji in the source. Their appearance still depends on available fonts and platform support. An emoji can look different across devices.

Do not use a lone emoji as the only essential instruction if its meaning may be unclear. Write a visible description where needed. Identify a language change with a suitable lang attribute when a phrase uses a different language.

Complete example: a bilingual practice note

multilingual-note.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Multilingual Practice Note</title>
</head>
<body>
  <h1>Welcome to the study walk</h1>
  <p>Welcome: <span lang="hi">नमस्ते</span>.</p>
  <p>Route length: 2&nbsp;km.</p>
  <p>Notebook budget: ₹50.</p>
  <p>Practice complete ✓</p>
  <p>&copy; 2026 Fictional Practice Club.</p>
</body>
</html>
Browser previewDocument title: Multilingual Practice Note

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Span is a small text container used here only to identify the Hindi phrase. Its detailed uses will come later. The amount is fictional teaching data, not a product price. The check mark accompanies words, so the completion message does not rely only on a symbol.

From a saved character to displayed text
  1. 1Save the file as UTF-8
  2. 2Declare UTF-8 near the start
  3. 3Escape reserved HTML characters
  4. 4Check the displayed words and symbols

HTML character references and URL percent encoding are different systems.

HTML escaping is different from URL encoding

HTML character references represent characters in markup text or attribute values. URL encoding represents characters within a web address. For example, %20 may represent a space in a URL; writing %20 in an ordinary paragraph does not ask HTML to display a space.

The two may appear together. A URL in an HTML attribute can need valid URL encoding and correct HTML escaping of its source characters. Chapter 17 explains the address side in detail.

Problem and solution: the sample tag vanishes

Problem: a page intended to teach <strong> instead creates a real strong element or displays an unexpected result.

Solution: decide which text is the document's own markup and which is the sample being shown to readers. Escape the sample's markup characters. Check the displayed text and count actual elements if using developer tools.

Character references are not a way to hide secrets or automatically make arbitrary user input safe in every context. Handling untrusted input requires the right rules for where it is placed; a later security lesson will explain that boundary.

Common mistakes and useful tips

  • Declaring UTF-8 while saving a different encoding.
  • Omitting semicolons or changing case in named references casually.
  • Expecting code to stop nested tags being parsed automatically.
  • Encoding every ordinary character unnecessarily.
  • Using repeated non-breaking spaces to arrange a page.
  • Confusing character references with URL encoding or encryption.
  • Assuming every emoji has the same appearance or meaning everywhere.

Practice questions

  1. Write a character reference for an ampersand.
  2. Which character do &#60; and &#x3C; represent?
  3. How do you display the spelling &lt; itself?
  4. When might a non-breaking space be useful?
  5. Does writing %20 in a normal paragraph create a space?
  6. Create a small page with an escaped tag example, a direct Unicode symbol and a clearly labelled phrase in a second language.
Check your practice answers
  1. &amp; or &#38;.
  2. The less-than sign, <.
  3. Escape the ampersand: &amp;lt;.
  4. Between a number and its unit where they should stay together, such as 2 and km. It is not a page-layout tool.
  5. No. That is URL-encoding notation, not an HTML character reference.
  6. Save and declare UTF-8, escape the sample markup and use a suitable lang value for the second-language phrase. Check the actual displayed characters.

Interview questions

Why are character references useful?

They let authors represent characters that need to be distinguished from markup syntax or are inconvenient to type. Named and numeric forms can represent the same character.

Does UTF-8 remove every need for escaping?

No. Encoding determines how bytes represent characters. Some characters still have a syntactic role in HTML and need appropriate escaping when used as literal content.

What does a non-breaking space change?

It prevents a normal line break at that particular space. It does not create a general layout system or stop every possible wrapping issue.

Are character references encryption?

No. Browsers decode them into ordinary content, and the source remains inspectable. Do not use them as a privacy mechanism.

Quick revision

  • Save and declare UTF-8 consistently.
  • Distinguish the document's markup from sample markup displayed as text.
  • Named, decimal and hexadecimal references can identify characters.
  • Use non-breaking spaces sparingly for a real reading need.
  • Keep HTML escaping, URL encoding and security responsibilities separate.