How to Convert RGB to HEX in CSS
Published April 2025 · 3 min read
The Formula
Each RGB value (0-255) converts to a 2-digit hex number (00-FF):
rgb(37, 99, 235) → #2563EB
37 → 25 (hex)
99 → 63 (hex)
235 → EB (hex)JavaScript Function
const rgbToHex = (r, g, b) =>
'#' + [r, g, b].map(x => x.toString(16).padStart(2, '0')).join('');
rgbToHex(37, 99, 235); // "#2563eb"Common Conversions
| RGB | HEX | Color |
|---|---|---|
| rgb(255,0,0) | #FF0000 | Red |
| rgb(0,128,0) | #008000 | Green |
| rgb(0,0,255) | #0000FF | Blue |
| rgb(255,255,255) | #FFFFFF | White |
| rgb(0,0,0) | #000000 | Black |
Use Our Tool
Our Color Picker converts between RGB, HEX, and HSL instantly.