HTML Encode
Escape special characters into HTML entities so your text shows up exactly as written instead of being read as markup. Everything runs in your browser.
0 characters
0 characters
About HTML Encode
This tool performs HTML entity encoding, a process that escapes special characters into their corresponding character entity references[5]. It converts characters with syntactic meaning in HTML, specifically `<`, `>`, `&`, `"`, and `'`, into their safe representations: `<`, `>`, `&`, `"`, and `'`. This transformation is a fundamental requirement for securely embedding untrusted text within an HTML document, ensuring that the data is rendered as literal content rather than being interpreted as part of the page's structure or executable code.
The primary motivation for HTML encoding is security, specifically the prevention of Cross-Site Scripting[3] (XSS) attacks. When user-provided data is inserted directly into an HTML page, a malicious actor could include script tags or other active content. By encoding the input, a string like `<script>alert(1)</script>` is transformed into `<script>alert(1)</script>`. This encoded string is then rendered harmlessly as visible text by the browser instead of being executed as a script, neutralizing the attack vector within that specific context.
The concept of entities originates from SGML[7] (Standard Generalized Markup Language), the powerful markup language[6] from which HTML[4] was derived. Early web specifications from the W3C, such as HTML 4.01[13] and XHTML 1.0[14], defined the core entities. Today, the canonical specification is the WHATWG HTML Living Standard[15], which provides the definitive parsing algorithms and maintains the official list of named character references[16], ensuring consistent behavior across all modern web browsers.
HTML character references exist in two primary forms: named references (e.g., `©` for the copyright symbol) and numeric references. Numeric references directly map to a character's code point[2] in the Universal Coded Character Set[9], which is standardized by Unicode[8]. They can be expressed in decimal (e.g., `©`) or hexadecimal (e.g., `©`). While named entities are more readable, numeric entities provide a universal mechanism to represent any character, which is especially useful when a document's declared character encoding[1] (ideally UTF-8[10]) might not be preserved during transport.
It is critical to recognize that HTML encoding is context-specific. The encoding applied by this tool is correct for data placed within HTML element content (e.g., between `<div>` and `</div>`) and for attribute values enclosed in quotes. It is not the correct encoding for all situations. For example, data injected into a `<script>` block requires JavaScript Unicode escaping, while data placed in a URL href attribute requires percent-encoding as defined by RFC 3986[12]. Adhering to this principle of context-sensitive output encoding, as detailed in the OWASP Cross Site Scripting Prevention Cheat Sheet[11], is essential for building robust and secure web applications.
How to Use
- Paste or type your raw, unencoded string into the input text area.
- The tool automatically processes your input in real-time.
- The resulting HTML-encoded string appears instantly in the output text area.
- Click the "Copy" button to copy the encoded output to your clipboard.
- Paste the encoded string into your HTML source code where literal text content is required, such as inside a tag or a quoted attribute.
Frequently Asked Questions
What is the difference between HTML encoding and URL encoding?
HTML encoding (e.g., `<`) prevents data from being interpreted as HTML markup and is used for content displayed on a web page. URL encoding, also known as percent-encoding (e.g., `%3C`), ensures data can be safely transmitted within a URL's path or query string, as defined in RFC 3986[12]. They solve different problems in different contexts and are not interchangeable.
Why is the ampersand character (&) itself encoded?
The ampersand is the initiator character for all HTML entity references. To display a literal ampersand, it must be encoded to `&`. This prevents the browser from misinterpreting a sequence like `A&B` as the beginning of an unknown entity `&B;`. This is a classic example of escaping the escape character.
Should I encode all non-ASCII characters?
Not always. If your HTML document is served with the header `Content-Type: text/html; charset=utf-8`, modern browsers will correctly render literal UTF-8[10] characters like `é` or `€`. However, encoding them to numeric entities (`é`, `€`) provides a robust fallback that works even if the character encoding is misconfigured or stripped during transit.
Is it always necessary to encode the single quote (')?
It is context-dependent but highly recommended. Encoding the single quote to `'` (or `'`) is critical when the output is placed within an HTML attribute delimited by single quotes, e.g., `onmouseover='...'`. While not strictly required for text content inside an element, it is a best practice for defense-in-depth against XSS.
What are named vs. numeric character references?
Named character references are human-readable mnemonics like ` `. Numeric character references point directly to a Unicode[8] code point[2] and can be decimal (` `) or hexadecimal (` `). Named references are limited to the set defined in the HTML standard[16], while numeric references can represent any Unicode character.
Does HTML encoding prevent all types of Cross-Site Scripting (XSS)?
No. It is a critical defense but only for the context of HTML element content and quoted attributes. It does not protect against XSS vulnerabilities in other contexts, such as inside `<script>` blocks, CSS `url()` values, or event handlers. You must use the correct encoding for each specific output context, a principle known as context-sensitive output encoding[11].
What is the difference between `&#...;` and `&#x...;` syntax?
Both are numeric character references. The `&#...;` syntax uses a decimal number representing the character's Unicode code point (e.g., `<` for '<'). The `&#x...;` syntax uses a hexadecimal number (e.g., `<`). Both forms are valid and produce the exact same character.
Which standard defines the modern list of HTML entities?
The WHATWG HTML Living Standard[16] maintains the single, canonical list of named character references. This standard is continuously updated and is the definitive source implemented by all major browser vendors, superseding older W3C lists from HTML 4.01[13].
Sources (16)
- Wikipedia: Character encoding in HTML
- Wikipedia: Code point
- Wikipedia: Cross-site scripting
- Wikipedia: HTML
- Wikipedia: List of XML and HTML character entity references
- Wikipedia: Markup language
- Wikipedia: SGML
- Wikipedia: Unicode
- Wikipedia: Universal Coded Character Set
- Wikipedia: UTF-8
- OWASP: Cross Site Scripting Prevention Cheat Sheet
- IETF: RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax
- W3C: HTML 4.01 Specification
- W3C: XHTML 1.0 The Extensible HyperText Markup Language
- WHATWG: HTML Living Standard
- WHATWG: HTML Living Standard - Named character references
About Matt George
I'm Matt George, a software engineer in the startup space. I run my own shop building software for early-stage founders, and I love making free tools and experiences that people actually use. Enjoy using HTML Encode!