# Output Encoding

> Turning signs like < into safe codes. Then user text can go on a page.

**Field:** Software Development  
**Category:** Security  
**Also known as:** HTML Escaping, Output Escaping, HTML Encoding

## Think of it like

Think of it like putting a toy sword in a glass case. People can see it, but nobody can swing it.

## Definition

Output encoding changes special characters before user text is put on a page. In HTML, < becomes &lt; and > becomes &gt;. The browser then shows the text instead of running it as code. Each place needs its own rules, so HTML, links and scripts are each encoded their own way. It is the main defence against cross-site scripting.

## Example

Emma types <b>hi</b> as her name. The page encodes it, so other users see the tags as plain text, not bold words.

## Example code

```html
<!-- What the user typed -->
<script>alert(1)</script>

<!-- What the page writes -->
&lt;script&gt;alert(1)&lt;/script&gt;
```

Once encoded, the script tag shows up as text and never runs.

## Why it matters

It turns an attacker's script into harmless words. Most modern frameworks do it for you by default.

## Common mistake

Turning the safe default off to show rich text. That puts raw user text back on the page.

## Three principles

- **Encode on the way out.** Just before text reaches the page.
- **Match the place.** HTML, links and scripts differ.
- **Trust the framework.** Keep its safe default on.

## Related terms

- [Cross-Site Scripting (XSS)](https://learn.benibix.com/term/development/cross-site-scripting)
- [Escape Character](https://learn.benibix.com/term/development/escape-character)
- [Input Validation](https://learn.benibix.com/term/development/input-validation)
- [Content Security Policy (CSP)](https://learn.benibix.com/term/development/content-security-policy)
- [HyperText Markup Language (HTML)](https://learn.benibix.com/term/development/html)

## Told apart from

- [Input Validation vs Output Encoding](https://learn.benibix.com/compare/input-validation-vs-output-encoding): People think checking what users type stops cross-site scripting, and it does not. Input validation turns away data with the wrong shape as it comes in. But a comment box has to take almost any text. Output encoding works on the way out. It turns signs like the less-than sign into safe codes, so the browser shows the text and never runs it. You want both.

![Three panels. Same-origin policy: a page on bad.example asks to read a reply from bank.example and the answer is no, another origin, where an origin is the scheme, host and port. Content Security Policy: a header says script-src 'self', so /app.js from the site runs and a script from evil.example is blocked. Output encoding: a user types a script tag, the page writes it with &lt; and &gt; codes, and it is shown as text, never run.](https://learn.benibix.com/diagrams/dev2-browser-guards.svg)

---

Source: https://learn.benibix.com/term/development/output-encoding
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
