How to Encode < in HTML

The less-than sign < in HTML becomes &lt;. Without this encoding, the browser interprets < as the start of an HTML tag. This can break your page layout and, more critically, create cross-site scripting (XSS) vulnerabilities if user input contains <script> tags.

Essential HTML Entities

CharacterEntityNumericDescription
<&lt;&#60;Less-than
>&gt;&#62;Greater-than
&&amp;&#38;Ampersand
"&quot;&#34;Double quote
'&#39;&#39;Single quote

Code Examples

JavaScript

// Safe: use textContent (auto-escapes)
element.textContent = '<script>alert("xss")</script>';

// Manual escaping
function escapeHTML(str) {
  return str.replace(/&/g, '&amp;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/"/g, '&quot;');
}

Python

import html
html.escape('<script>')
# '&lt;script&gt;'

Try It Yourself

Use our HTML Entity Encode & Decode tool to encode any text for safe HTML display.

Frequently Asked Questions

What are the most common HTML entities?

The five essential HTML entities are: &lt; for <, &gt; for >, &amp; for &, &quot; for double quotes, and &#39; for single quotes.

Why is HTML encoding important for security?

Without proper encoding, user-supplied input containing <script> tags will execute as JavaScript in other users' browsers. This is called cross-site scripting (XSS) and is one of the most common web vulnerabilities.

Built by Michael Lip. 100% client-side — no data leaves your browser.