ANSI Escape Code to HTML Converter
Convert ANSI escape codes to clean semantic HTML with CSS classes. Supports 8/16/256/truecolor SGR codes, bold, italic, underline, and more.
About
Terminal output uses ANSI SGR (Select Graphic Rendition) escape sequences to encode color and style information inline. These sequences follow the CSI format: ESC[n1;n2;โฆm, where each n is a numeric parameter mapped to a specific rendering attribute. Pasting raw terminal output into a web page without conversion produces garbled escape characters and zero formatting. This tool parses every SGR parameter - standard 8-color, bright 16-color, extended 256-color palette (38;5;n), and 24-bit truecolor (38;2;r;g;b) - and emits clean, semantic HTML using only CSS classes and no inline styles. The output is production-ready: every token is a <span> element, every color is a class name, and the accompanying CSS stylesheet maps those classes to the correct hex values.
Limitations: this tool handles SGR sequences only (codes that end with m). Cursor movement, screen clearing, and other CSI commands (H, J, K) are stripped. The 256-color cube uses the standard xterm mapping where channel values for indices 0 - 5 correspond to intensities 0, 95, 135, 175, 215, 255. Truecolor values are passed through directly as CSS rgb() values using inline custom properties on the span. Pro Tip: pipe your terminal command through --color=always to force ANSI output even when stdout is not a TTY, then paste the result here.
Formulas
The ANSI CSI (Control Sequence Introducer) sequence format for SGR commands:
Where ESC = 0x1B (decimal 27), each n is an integer SGR parameter, and m is the literal terminator character. Multiple parameters are separated by semicolons.
For extended 256-color mode, the palette index n maps to RGB as follows:
Where the channel mapping function f(i) for index i โ {0,1,2,3,4,5} returns values {0, 95, 135, 175, 215, 255} respectively. The first index 0 maps to intensity 0, and subsequent indices follow the pattern 55 + 40 ร i for i โฅ 1.
Reference Data
| SGR Code | CSS Class | Effect | Reset Code |
|---|---|---|---|
| 0 | - | Reset all attributes | - |
| 1 | bold | Bold / bright colors | 21 or 22 |
| 3 | italic | Italic text | 23 |
| 4 | underline | Underline text | 24 |
| 7 | reverse | Swap foreground & background | 27 |
| 8 | conceal | Hidden text | 28 |
| 9 | strike | Strikethrough | 29 |
| 30 - 37 | foreground-0โฆ7 | Standard foreground color 0 - 7 | 39 |
| 38;5;n | foreground-n | Extended 256-color foreground | 39 |
| 38;2;r;g;b | Inline --fg custom property | 24-bit truecolor foreground | 39 |
| 39 | foreground-fg | Default foreground | - |
| 40 - 47 | background-0โฆ7 | Standard background color 0 - 7 | 49 |
| 48;5;n | background-n | Extended 256-color background | 49 |
| 48;2;r;g;b | Inline --bg custom property | 24-bit truecolor background | 49 |
| 49 | background-bg | Default background | - |
| 90 - 97 | foreground-8โฆ15 | Bright foreground color 8 - 15 | 39 |
| 100 - 107 | background-8โฆ15 | Bright background color 8 - 15 | 49 |
| 2, 5, 6 | - | Not implemented (dim, blink) | - |
Frequently Asked Questions
--color=always (GNU coreutils, grep), CLICOLOR_FORCE=1 (macOS), or TERM=xterm-256color. For tmux, use tmux capture-pane -eJp to capture with escape sequences intact. Then copy the raw output and paste it into the input field.foreground-42) with the correct hex value in the stylesheet. For 24-bit truecolor, it applies a CSS custom property (--ansi-fg or --ansi-bg) on the span since there are 16 million possible values, making class-based approaches impractical.reverse to the span. The accompanying CSS uses a technique with custom properties to swap the foreground and background values visually. If no explicit foreground or background has been set, the terminal's default colors are swapped. Code 27 removes the reverse attribute. The converter tracks this as a boolean flag in its state machine independently of color assignments.