SGR 48;5;n — 256-color background
Pick a background color from the 256-color xterm palette.
Byte forms
Every common string-literal form so you can paste-and-search either direction.
\x1b[48;5;Nm\033[48;5;Nm\e[48;5;NmESC [ 4 8 ; 5 ; N m1b 5b 34 38 3b 35 3b <N> 6dLive preview
Rendered in your browser via the same tokeniser the decoder uses — no terminal needed.
Description
Background sibling of sgr-fg-256 (\x1b[38;5;Nm). N is 0–255 with identical palette layout — 0–7 the basic 16-color slots (the 40–47 equivalents), 8–15 the bright slots (the 100–107 equivalents), 16–231 a 6×6×6 RGB cube indexed as 16 + 36*r + 6*g + b with each component in 0..5, and 232–255 a 24-step grayscale ramp from near-black to near-white. The colon-form \x1b[48:5:Nm is accepted by ECMA-48-strict parsers alongside the more common semicolon form. The 256-color extension originated in xterm and is documented in xterm-ctlseqs §"256-color support"; it predates the truecolor escape by ~15 years and remains the most portable way to express anything beyond the basic 16.
Terminal support is broader than truecolor but narrower than the 16-color baseline. The Linux console / fbcon refuses 256-color in framebuffer mode (the kernel palette only holds 16 slots), so \x1b[48;5;202m renders as a no-op or fallback color depending on the kernel build — the support matrix above marks it no. Windows conhost.exe supports 256 only since Windows 10 1809; older builds quantize to the nearest 16-color slot. The 16–231 cube is universally available on every other emulator in the matrix; the 232–255 grayscale ramp is where emulators diverge most — alacritty, kitty, wezterm and modern gnome-terminal render an even ramp, while screen 4.x quantizes the dimmer grays to pure black on some builds and 5.0 fixed it. tmux requires set -ag terminal-overrides ',xterm-256color:Tc' (or the modern RGB cap) to forward 256-color escapes intact to the outer terminal; without it \x1b[48;5;202m reaches tmux but never makes it past.
Reset just the background with \x1b[49m — surgical, leaves foreground and other attributes intact. The full reset \x1b[0m clears everything. To convert an arbitrary RGB triplet to the nearest cube index, use 16 + 36*round(r*5/255) + 6*round(g*5/255) + round(b*5/255); for grayscale targets use 232 + round(luma*23/255). If the rendered output looks wrong on a specific terminal, probe $COLORTERM — when it is truecolor or 24bit prefer sgr-bg-truecolor for exact colors; when it is unset or empty but $TERM matches *-256color, the 256-cube is the most portable target.
Spec citation: xterm-ctlseqs (256-color extension)
Examples
printf '\033[48;5;202m highlight \033[49m\n'print('\x1b[48;5;202m highlight \x1b[0m')fmt.Print("\x1b[48;5;202m highlight \x1b[0m")console.log('\x1b[48;5;202m highlight \x1b[0m')printf("\x1b[48;5;202m highlight \x1b[0m\n");Used in
Real-world tools that emit this sequence — anchors the bytes to commands you've already used.
- batsyntax-theme line-highlight background (active line gutter)
- delta256-palette diff backgrounds — soft pastels for added/removed hunks
- vim, neovim colourschemes (Solarized, Gruvbox) on 256-colour terminals
- tig (git TUI) — selected line bg
- fzf default --color=bg+:236 dark grey on the active row
Frequently asked
Short answers to the questions developers actually search for this sequence.
- Why does
\x1b[48;5;0mnot equal\x1b[40mon iTerm2? - Both are nominally slot 0, but they go through different palette tables. iTerm2's per-profile colour preset (e.g. Solarized Dark, Tango) remaps slots 0–15 of the 256-colour palette independently from the legacy 8-colour
\x1b[40m/SGR-40 table. xterm and most other emulators tie the two tables together for slots 0–7 + their bright pair, so\x1b[40m==\x1b[48;5;0mthere. To get bitwise-stable colour across emulators use truecolor (\x1b[48;2;r;g;bm); to target the legacy table specifically, stick with\x1b[40m–\x1b[47m. - Why does my code ignore the
5;and run\x1b[48;Nminstead — what does the terminal do? - ECMA-48 reserves the leading sub-parameter (
5= indexed /2= direct RGB) precisely so a single parameter following SGR 48 has no defined meaning. Most terminals either ignore the whole48;Nclause as malformed or treatNas if it were the next top-level parameter (so\x1b[48;5mbecomes\x1b[48m;\x1b[5mand your text starts blinking). Always write the prefix:\x1b[48;5;Nmfor palette,\x1b[48;2;r;g;bmfor truecolor — and lint your codebase to catch the missing5;.
Terminal support
- xterm
- yes
- Linux console (fbcon)
- no
- macOS Terminal.app
- yes
- iTerm2
- yes
- Windows Terminal
- yes
- cmd.exe / ConPTY
- partial
- kitty
- yes
- alacritty
- yes
- WezTerm
- yes
- Ghostty
- yes
- GNOME Terminal
- yes
- Konsole
- yes
- tmux
- yes
- GNU screen
- yes
| xterm | Linux console (fbcon) | macOS Terminal.app | iTerm2 | Windows Terminal | cmd.exe / ConPTY | kitty | alacritty | WezTerm | Ghostty | GNOME Terminal | Konsole | tmux | GNU screen |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| yes | no | yes | yes | yes | partial | yes | yes | yes | yes | yes | yes | yes | yes |