BF50 Black Friday · 50% off all plans
from $5/mo · Auto-applied ---:--:-- Claim 50% off →
Skip to content

Search Seonio

Type a keyword, domain, or paste a URL

Start free with $1 to see full results. Powered by Seonio
Web Development

HTML entities: When and how to use them

HTML entities are short codes, starting with an ampersand, that stand for a character. You need them for characters a browser would otherwise read as code.

Monthly searches · html entities
14.8k
Keyword research data
html entities
HTML entities: When and how to use them
Click the image to view it full size

An HTML entity, or in the standard's own words a character reference, is a short code that a browser replaces with a character. Each one starts with & and ends with ;. There are three ways to write the same character: WHATWG

Form Example Shows
Named © ©
Decimal number © ©
Hexadecimal number © ©

The number is the character's Unicode code point, so any character has a numeric form. The standard's list of names has 2,231 entries, 106 of them old spellings without the semicolon. Names are case-sensitive: É is É and é is é.

When you need one

Mostly for characters that HTML would otherwise read as code. The W3C's advice is to always escape these three in content: W3C

  • &lt; for <, which would otherwise start a tag. To show <p> as text on a page, write &lt;p&gt;.
  • &amp; for &, which would otherwise start an entity.
  • &gt; for >, for symmetry with <.

Inside an attribute value, you also need to escape the quote character that surrounds the value: &quot; in a double-quoted value, &apos; or &#39; in a single-quoted one.

The second reason is characters you can't see or can't tell apart. A no-break space looks like any other space, so &nbsp; makes it visible in your source. The same goes for direction marks in Arabic or Hebrew text, like &rlm;.

When you don't

Nearly everything else. If your page is saved and served as UTF-8, and it should be, you can type ©, é, €, — and → straight into the HTML. The W3C recommends real characters over escapes for ordinary text, because escaped text is harder to read, harder to edit and larger. Imagine a paragraph of French or Czech with every accented letter written as a code. See character encoding if your page shows garbled characters instead.

Browsers turn every entity back into its character when they read the page, so caf&eacute; and café produce the same text. For search and readers alike, there's no benefit in using the entity.

The ones worth knowing

Write For Why you'd use it
&lt; < Show markup or a "less than" as text
&amp; & Show an ampersand in text, like Tom &amp; Jerry
&quot; " A double quote inside a double-quoted attribute
&nbsp; no-break space Keep "10 km" or "Fig. 3" on one line
&shy; soft hyphen Allow a long word to break at that point, with a hyphen
&copy;, &mdash;, &euro; ©, —, € Only if you can't type the character itself

Mistakes to avoid

  • Encoding twice. If a system escapes text that's already escaped, &amp; becomes &amp;amp;, and readers see &amp; on the page instead of &. It tends to happen where text passes through more than one system, such as a CMS and a plugin that both escape it. Escape once, at the moment you write the text into HTML.
  • Leaving off the semicolon. Browsers accept the 106 old names without it, no others, and even those not in every position. Always end with ;.
  • Getting the case wrong. &COPY; happens to work, but most names have one exact spelling; &Mdash; isn't an entity.
  • Using &nbsp; for layout. A row of no-break spaces to indent text breaks on other screen sizes. Use CSS for spacing.
  • Printing user input unescaped. A comment or search term containing <script> must be shown as text, not run. Escaping <, >, & and quotes is how that works, and good templating engines do it for you; don't switch it off. MDN

Not the same as URL encoding

Entities are for HTML text. Inside a web address, special characters use URL encoding instead, such as %20 for a space. In an href, you may need both: a URL-encoded address, and an &amp; wherever an ampersand in its query string could be mistaken for an entity.

Seonio

Put it to work on your own site

Keyword research, traffic, backlinks and clustering in one place. Start with a free $1 to test, no card required.