CSS Color Formats: HEX, RGB and HSL Explained with Conversions
When to use hex, rgb() or hsl() in CSS: how each format stores channels, alpha handling, browser support notes and quick mental conversion recipes.
CSS accepts the same color in three syntaxes — and picking deliberately makes stylesheets easier to theme, audit and compress. Here is what each format is for.
HEX: compact and universal
Hex packs channels into pairs: #1e90ff is red 30, green 144, blue 255. Optional 4- and 8-digit forms carry alpha (the last byte). It stays the default for design tokens and copy-paste from design tools because every picker emits it.
RGB: explicit channels
rgb(30 144 255 / 0.5) writes channels as numbers with a separate alpha. Modern space-separated syntax replaces commas and, unlike hex, lets you reuse channel numbers freely — easier for computed tints in CSS custom properties.
HSL: human mental model
Hue, saturation, lightness map to how people actually tweak colors: rotate the hue wheel for a variant, drop lightness for a hover state. hsl(210 100% 56%) is one turn of the dial from hsl(180 100% 56%) — predictable without any picker.
Converting in your head (and for real)
- Hex pair to decimal: each digit is 16 times its place —
90is 9 times 16 = 144. - Decimal to hex: divide by 16, write remainder after quotient: 144 gives 9 and 0 →
90.
For real work, the Color Converter converts HEX, RGB and HSL both ways with a live swatch, contrast hint against dark and light backgrounds, and the nearest CSS named color.
Practical pairing
Theme with HSL, ship tokens as hex for tool compatibility, and keep stylesheets clean with the CSS Formatter & Minifier — it preserves color values byte-for-byte while compacting the rest, so audits and diffs stay meaningful.
Related guides
Minifying JSON and CSS Safely: What Must Never Be Removed
What minifiers are allowed to touch: whitespace rules in JSON, strings and url() in CSS, real-world savings numbers and a pre-flight validation checklist.
Read guide →2026-10-12Base64 vs Hex Encoding: When to Use Each (Developer Guide)
Base64 vs hex encoding compared: size overhead, readability, common use cases in JWTs, data URLs and git diffs — with a quick decision table for engineers.
Read guide →2026-10-11How to Decode a JWT: Reading Header, Payload and Signature
Decode any JWT by hand or with a browser tool: how the three parts are encoded, which claims matter (sub, exp, iss, aud) and why decoding is not verification.
Read guide →Try these tools
Color Converter
Convert colors between HEX, RGB/RGBA and HSL/HSLA with a live swatch preview. Paste any common CSS color format and get all equivalents plus copyable strings.
Open tool →CSS Formatter / Minifier
Beautify CSS with consistent indentation and one-declaration-per-line, or minify it by stripping comments and whitespace. Handles media queries and nested braces.
Open tool →