CSS // blog
All blog postsHow to Convert RGB to HEX (and HEX to RGB) With Color Math
RGB to HEX Converter / HEX to RGB ConverterEvery front-end dev hits this wall eventually. Figma hands you #3B82F6, your CSS already uses rgb(59, 130, 246), or someone pastes a hex code into Slack asking what RGB that is. Suddenly you are doing base-16 division in your head like a calculator on low battery.
Skip the mental gymnastics. The RGB to HEX Converter / HEX to RGB Converter flips colors between RGB and HEX in this tab. We never see what you paste.
Convert RGB to HEX and HEX to RGB instantly
Type or paste an RGB color (255, 99, 71 or rgb(255, 99, 71)) to get #FF6347. Paste a hex value (#FF6347, shorthand #F63, or 8-digit hex with alpha) to unpack Red, Green, Blue, and alpha. The same page has a WCAG color contrast checker for the converted color.
Building broader styles? Jump to the CSS to Tailwind Converter, the Tailwind to CSS Converter, or the CSS Gradient Generator. Need to check a hex digit in isolation? The Number Base Converter does decimal, hex, binary, and octal. Those tools also run on your computer.
How hex channels (00-FF) and alpha work
#HEX and rgb() are the same sRGB color, written in different bases:
| Form | What it means |
|---|---|
| RGB 0-255 | Decimal Red, Green, Blue. 0 is off, 255 is full. |
| HEX 00-FF | Two hex digits per channel. A-F are 10-15. |
| #RGB | Shorthand when each pair repeats, like #336699 to #369. |
| #RRGGBBAA | Eight-digit hex. Last pair is alpha (00 to FF). |
One hex pair covers 0 (00) through 255 (FF), because 15 x 16 + 15 = 255. CSS also accepts modern space-separated syntax such as rgb(59 130 246 / 50%), or the older rgba(59, 130, 246, 0.5).
How to convert RGB to HEX and HEX to RGB by hand
The converter is the fast path when you are in the zone. The math still helps when you are reading a spec, writing a script, or debugging a color bug when nobody else is awake.
1. RGB to HEX: divide by 16
Each RGB channel (0-255) becomes two hex digits. Divide by 16. The whole-number quotient is the first digit (0-F). The remainder is the second. Walk through rgb(255, 99, 71):
- Red 255: 255 / 16 = 15 remainder 15. Both 15s are F, so
FF. - Green 99: 99 / 16 = 6 remainder 3, so
63. - Blue 71: 71 / 16 = 4 remainder 7, so
47.
Put a # in front: #FF6347. Pad single digits with a leading 0. Ten is 0A, not A.
2. HEX to RGB: multiply the first digit by 16
Going back is (first digit x 16) + second digit. Take #1E90FF (dodger blue):
1E: (1 x 16) + 14 (E) = 3090: (9 x 16) + 0 = 144FF: (15 x 16) + 15 = 255
Result: rgb(30, 144, 255).
3. JavaScript helpers for scripts and DevTools
Need the conversion in a front-end script or the browser console?
JavaScript
function rgbToHex(r, g, b) {
const clamp = (n) => Math.max(0, Math.min(255, Math.round(n)));
const toHex = (n) => clamp(n).toString(16).padStart(2, "0");
return "#" + toHex(r) + toHex(g) + toHex(b);
}
function hexToRgb(hex) {
const clean = hex.replace(/^#/, "").trim();
const expanded =
clean.length === 3
? clean
.split("")
.map((ch) => ch + ch)
.join("")
: clean;
if (!/^[0-9a-fA-F]{6}$/.test(expanded)) {
throw new Error("Invalid HEX color");
}
const num = parseInt(expanded, 16);
return {
r: (num >> 16) & 255,
g: (num >> 8) & 255,
b: num & 255,
};
}
console.log(rgbToHex(255, 99, 71)); // "#ff6347"
console.log(hexToRgb("#3B82F6")); // { r: 59, g: 130, b: 246 }These helpers cover 3-digit and 6-digit hex. Eight-digit hex with alpha needs a fourth pair. The RGB to HEX converter already unpacks that.
4. Practical CSS examples
People usually pick HEX for compact tokens and rgb() / rgba() when they want numeric channels or a translucent overlay:
CSS
:root {
--brand-primary: #3b82f6;
--surface-tint: rgba(59, 130, 246, 0.12);
}
.callout-banner {
background-color: var(--surface-tint);
border-left: 4px solid var(--brand-primary);
color: rgb(15 23 42);
}
.badge-translucent {
/* #3B82F640 is about 25% alpha */
background-color: #3b82f640;
}5. Common gotchas
- Missing #.
color: 3b82f6;is invalid CSS. The browser drops the declaration and your text stays black. - Out-of-range RGB. Channels live between 0 and 255. Clamp inputs so the math cannot wander off.
- Alpha is not the percentage in hex. 50% opacity is not 50 in hex. 0.5 x 255 = 127.5, which rounds to 128 (
80), so 50% in 8-digit hex is#RRGGBB80.
Convert a swatch or check an RGB triplet in the RGB to HEX Converter / HEX to RGB Converter. Save the base-16 arithmetic for when you actually want it.
Share