HTML - characters that should be escaped

10 points
Created by:

In this short article, we would like to count what characters should be escaped in HTML.

Quick solution:


&    should be replaced with    &
<    should be replaced with    &lt;
>    should be replaced with    &gt;

REPLACED CONDITIONALLY in element attribute (depending on the case):

"    should be replaced with    &quot;    e.g.   <div attribute="abc &quot; abc"></div>
'    should be replaced with    &#39;     e.g.   <div attribute='abc  &#39; abc'></div>


Detailed description

CharacterHTML escape codeDescription
&&amp;It is required to escape this character because it is used as an escape character, e.g. &lt;&gt;&quot;&#39;, etc.
<&lt;Opens HTML elements.
>&gt;Closes HTML elements.

Additionally, inside of attribute values, it is necessary to escape the quote character:

CharacterHTML escape codeDescription

Needed only when " opens and closes the valve.

<p title="&quot;John&quot; life story ...">
    Few words about John life story...

Needed only when ' opens and closes the valve.

<a href='' title='John&#39;s site'>

Hint: It is recommended to use " with attribute values.

Warning: use &#39; or &#039; or &#0039; because of:

  • &apos; is not supported by IE8,
  • &apos;  is not defined in HTML 4 (defined since HTML 5).


Practical example

// ONLINE-RUNNER:browser;

<!doctype html>
  <p title="&quot;John&quot; life story ...">Few words about John life story...</p>
  <a href='' title='John&#39;s site'>Home</a>
  <p>Source code should be placed between &lt;code&gt; and &lt;/code&gt;.</p>


  1. 13.5 Named character references - HTML Living Standard
  2. Common HTML entities used for typography - W3C Wiki
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂