Skip to content

RGB to HEX Conversion Explained

What's actually being converted

A color on screen is three numbers — how much red, green and blue light to mix — each ranging from 0 to 255. RGB writes those three numbers in base 10, separated by commas: rgb(255, 87, 51). HEX writes the same three numbers in base 16, two digits each, glued together after a #: #FF5733. Nothing about the color changes between the two — it's a notation difference, the way '15' and 'F' are the same quantity in decimal and hexadecimal.

Converting one channel by hand

Take a single 0–255 value, say 87. Divide by 16: 87 ÷ 16 = 5 remainder 7. The quotient (5) becomes the first hex digit, the remainder (7) becomes the second, giving '57'. For remainders or quotients of 10 or above, hexadecimal uses letters: 10=A, 11=B, 12=C, 13=D, 14=E, 15=F. So 255 ÷ 16 = 15 remainder 15, giving 'FF' — the maximum value in either notation.

Repeat that for all three channels and concatenate them in order: red, then green, then blue. rgb(255, 87, 51) becomes FF (255), 57 (87), 33 (51) — #FF5733.

Reading a HEX code back into RGB

Split the six digits after the # into three pairs. Convert each pair from base 16 back to base 10: the first digit is multiplied by 16, the second is added directly. For '57': 5 × 16 + 7 = 87. Do the same for the other two pairs to recover the full rgb() triplet.

Frequently asked questions

Why do some HEX codes only have three digits, like #FFF?
That's shorthand for a code where each channel's two digits repeat — #FFF expands to #FFFFFF, #F53 expands to #FF5533. It only works when both digits in every pair match, so it's a compact form rather than a different kind of color code.
Does HEX support transparency the way RGBA does?
An 8-digit HEX code adds an alpha pair at the end (#FF5733CC), which most modern browsers support, but plain 6-digit HEX has no transparency channel — rgba() or the 8-digit form are needed for that.
Is one format better for CSS than the other?
They render identically, so it's a readability preference. HEX is more compact for stylesheets; rgb()/rgba() is easier to skim when you need to reason about individual red/green/blue values or adjust opacity.

Try the RGB to HEX Converter

Turn red, green and blue channel values into a HEX colour code you can paste into CSS.

Open tool

More guides