HTML Entities Encoder & Decoder
Securely encode special characters into HTML entities or decode them back to raw text. Prevent Cross-Site Scripting (XSS) and display code snippets safely.
When building web applications, safely handling user input and dynamic data is crucial. The HTML Entities Converter allows you to instantly encode reserved characters into safe HTML entities, protecting your site against Cross-Site Scripting (XSS) attacks. By converting characters like < and > into < and >, you ensure browsers render the data as text rather than executing it as markup or scripts.
While essential characters must be encoded for security, modern UTF-8 encoding means you don't always have to encode every non-ASCII character. However, this tool gives you granular control, allowing you to choose between named, decimal, or hex formats, and even optionally encode all non-ASCII characters or quotes depending on your specific use case, such as rendering formatted HTML safely.
Common HTML Entities Reference
| Character | Description | Named | Decimal | Hex |
|---|
This tool runs 100% in your browser; your data never leaves your device. Privacy details
What Are HTML Entities and Why Do They Exist?
In HTML, certain characters are reserved and have special meaning. For instance, the less-than sign (<) and greater-than sign (>) are used to define tags. If you want to display these literal characters on your webpage without the browser interpreting them as code, you must use HTML entities.
An HTML entity is a sequence of characters starting with an ampersand (&) and ending with a semicolon (;). These entities instruct the browser to display a specific character. Beyond reserved characters, entities were historically used to display symbols not present on standard keyboards or characters outside the basic ASCII set before universal adoption of UTF-8.
Named vs Decimal vs Hex Entities: Which to Use
Our encoder gives you three formats to choose from. While they all result in the same visual output, they differ in syntax and support:
- Named Entities: Use human-readable abbreviations (e.g.,
©for ©). These are the easiest to read in raw code but aren't available for every single Unicode character. - Decimal Entities: Use the base-10 numerical Unicode value of the character (e.g.,
©). Every single character can be represented this way, making it universally reliable. - Hex Entities: Use the base-16 numerical Unicode value (e.g.,
©). Similar to decimal, this can represent any character and is often preferred by developers who work closely with hexadecimal encodings.
HTML Entity Encoding and XSS Prevention
Cross-Site Scripting (XSS) is one of the most common web security vulnerabilities. It occurs when an application includes untrusted data in a web page without proper validation or escaping. If an attacker inputs <script>alert('Hacked')</script> and your site renders it directly, the script executes in the victim's browser.
By properly encoding HTML entities, that malicious input becomes <script>alert('Hacked')</script>. The browser displays it harmlessly as text on the screen. For robust security, you should complement entity encoding with proper security headers like Content Security Policy (CSP).
The Most Important HTML Entities You Must Know
While there are hundreds of entities, the "Big Five" are absolutely critical for securing web applications:
- Ampersand (&) →
& - Less than (<) →
< - Greater than (>) →
> - Double quote (") →
" - Single quote (') →
'(or'in HTML5)
Failing to encode quotes is a common oversight. If you inject user data into an input value like <input value="USER_DATA">, an unencoded quote allows the attacker to break out of the attribute and inject event handlers like onload.
Encoding Entities in JavaScript vs Server-Side Templates
Modern web development frameworks often handle entity encoding automatically. For example, React's JSX and Vue's template interpolation ({{ data }}) automatically escape HTML entities to prevent XSS. However, if you are explicitly setting inner HTML (like React's dangerouslySetInnerHTML) or using vanilla JavaScript's innerHTML, you must manually encode the data first.
Similarly, in backend languages like PHP or Node.js, you must ensure data sent to the client is properly escaped, or utilize tools like a CORS header generator to strictly control data access policies. Data passed through URLs should also be handled carefully using a URL encoder rather than HTML entity encoding.
Common Mistakes: When Not to Double-Encode
A frequent issue developers encounter is "double encoding." This happens when text that has already been encoded (e.g., &) is passed through an encoder again, resulting in &amp;. When rendered, the user sees literal entity codes on the screen instead of the expected character.
To avoid this, establish a clear architecture where encoding happens at the very last step before rendering, typically at the view or template layer. Data stored in your database should generally remain unencoded raw text, or perhaps stored as Base64 if necessary for binary data, ensuring you only apply HTML entity encoding right when it hits the DOM.
How to Use the HTML Entities Encoder & Decoder
- Choose encode or decode mode: Toggle the mode at the top depending on whether you are securing text or retrieving original characters.
- Paste your text or HTML: Input your source data into the left textarea.
- Select entity format: For encoding, choose between named, decimal, or hex output based on your specific requirements.
- Click convert: Hit the Convert button to process your text instantly within the browser.
- Copy output: Use the Copy button to quickly copy the encoded or decoded result to your clipboard.
Common Use Cases
- Preventing XSS in user-generated content: Safely render blog comments, forum posts, or profile bios by encoding special characters.
- Embedding code examples in HTML: Display HTML, XML, or JavaScript code snippets inside <pre> and <code> tags without the browser parsing them.
- Encoding email addresses: Obfuscate email addresses from basic spam bots by converting characters to decimal or hex entities.
- Fixing broken characters in RSS feeds: Ensure XML compatibility by encoding strict entities like ampersands in feed descriptions.
- Sanitizing API responses before rendering: Clean up JSON data that contains unsafe HTML characters before dumping it into the DOM.
- Encoding special chars in template strings: Prepare text strings that need to be injected into complex HTML templates securely.
Frequently Asked Questions
What are HTML entities?
HTML entities are special character codes used in HTML to display reserved characters or characters not present on a standard keyboard. They begin with an ampersand (&) and end with a semicolon (;).
When should I encode HTML entities?
You should encode HTML entities whenever you are displaying user-generated content, displaying code snippets, or placing untrusted data inside HTML attributes. This prevents browsers from interpreting the data as executable code or markup.
What is the difference between named, decimal, and hex entities?
Named entities use readable names (like &lt; for <), decimal entities use the character's base-10 Unicode number (like &#60;), and hex entities use base-16 (like &#x3C;). They all produce the same character, but decimal/hex can represent any Unicode character while named entities are limited to specific ones.
How does HTML entity encoding prevent XSS?
Cross-Site Scripting (XSS) occurs when malicious scripts are injected into web pages. By encoding characters like < and > into &lt; and &gt;, browsers display them as harmless text instead of executing them as HTML tags or script elements.
Why is & encoded as &amp;?
Because the ampersand (&) is the start character for all HTML entities. If you want to display an actual ampersand on your page, you must encode it to tell the browser not to treat it as the beginning of an entity.
Should I encode quotes in HTML attributes?
Yes, definitely. If you output untrusted data inside an HTML attribute (e.g., value="user_input"), an attacker could input a double quote (") to close your attribute and add their own malicious attributes like onload or onmouseover. Always encode quotes in attributes.
What entities are required vs optional?
The essential characters to encode for security are <, >, &, ", and '. Other characters like copyright symbols (©) or accented letters do not strictly need encoding in modern UTF-8 documents, but doing so can prevent display issues in misconfigured environments.
Related Tools
Base64 Encoder
Encode or decode Base64 strings instantly with zero uploads.
URL Encoder
Encode and decode query parameters locally for APIs, redirects, and forms.
HTML Formatter
Pretty-print messy markup and inspect structure without exposing page code.
CORS Header Generator
Generate CORS headers for Nginx, Apache, and Express.js with a visual builder. No data uploaded.
Security Headers Builder
Generate HTTP security response headers for Nginx, Apache, Express, Caddy, and Cloudflare Workers.