RGBA to HEX With Alpha: Convert Transparency for CSS
Learn how to convert rgba to hex with alpha for CSS, including the 8 digit hex format and browser support. Try the free RGBA to HEX converter now.
If you need to convert rgba to hex with alpha, the part that trips people up is the fourth value. Red, green, and blue map cleanly to two hex digits each, but alpha arrives as a decimal between 0 and 1 and has to be scaled to a byte first. Here is the exact math, the 8 digit hex format that stores it, the browser support worth caring about, and the cases where the conversion is not lossless.
The short version: multiply alpha by 255, round it, convert to hex, append it. rgba(255, 99, 71, 0.5) becomes #ff634780.
What Is an 8 Digit Hex Color
A standard hex color is six digits: two for red, two for green, two for blue. Each pair is a byte, so each channel holds a value from 0 to 255.
An 8 digit hex color adds a fourth pair for the alpha channel. CSS Color Module Level 4 formalized the format: #RRGGBBAA.
/* These two declarations are identical */
.overlay { background: rgba(0, 0, 0, 0.6); }
.overlay { background: #00000099; }
The alpha pair works exactly like the color pairs. 00 is fully transparent, ff is fully opaque, and everything in between is partial transparency.
There is also a 4 digit shorthand, #RGBA, which expands each digit by doubling it. #f00c expands to #ff0000cc. It is convenient but only usable when every channel happens to have a repeating pair, so most converted values will not fit it.
How to Convert RGBA to HEX With Alpha by Hand
The RGB portion is base 10 to base 16 on each channel. The alpha portion needs one extra step because CSS expresses it as a fraction, not a byte.
Take rgba(255, 99, 71, 0.5):
- Red: 255 becomes
ff - Green: 99 becomes
63 - Blue: 71 becomes
47 - Alpha: 0.5 times 255 is 127.5, which rounds to 128, which becomes
80
Result: #ff634780.
The rounding in that last calculation is where the conversion stops being exact. More on that below.
Here is the same logic in JavaScript, which is useful if you are generating a CSS alpha channel hex value inside a build script or a design token pipeline:
function rgbaToHex(r, g, b, a = 1) {
const toHex = n => Math.round(n).toString(16).padStart(2, '0');
return `#${toHex(r)}${toHex(g)}${toHex(b)}${toHex(a * 255)}`;
}
rgbaToHex(255, 99, 71, 0.5); // "#ff634780"
rgbaToHex(0, 0, 0, 0.6); // "#00000099"
rgbaToHex(30, 144, 255); // "#1e90ffff"
The padStart(2, '0') matters. Without it, a channel value under 16 produces a single digit and silently corrupts the string. rgb(5, 5, 5) would come out as #555 instead of #050505, which is a completely different color.
If you are parsing rgba strings out of an existing stylesheet rather than working with numbers, a capture pattern like rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+))?\s*\) handles both rgb() and rgba() forms. You can test that pattern against your real stylesheet content in a regex tester before wiring it into a script.
Why RGBA to HEX With Alpha Is Not Always Lossless
CSS alpha is a floating point value. An 8 digit hex color stores alpha as one byte, which means only 256 possible values.
That gap creates rounding. rgba(0, 0, 0, 0.6) converts to #00000099, but 0x99 is 153, and 153 divided by 255 is 0.6 exactly, so that one is clean. Try 0.7 instead: 0.7 times 255 is 178.5, which rounds to 179, and 179 divided by 255 is 0.70196. Close, but not the same number.
Nobody will notice the difference in a background tint. The drift matters more in an animation that interpolates alpha across many steps, or when you're comparing colors for equality in a test.
Common alpha values and their hex equivalents:
| Alpha | Hex | Exact |
|---|---|---|
| 0 | 00 |
yes |
| 0.1 | 1a |
no |
| 0.2 | 33 |
yes |
| 0.25 | 40 |
yes |
| 0.5 | 80 |
no |
| 0.6 | 99 |
yes |
| 0.75 | bf |
no |
| 0.8 | cc |
yes |
| 1 | ff |
yes |
Note that 0.5 is not exact, which surprises people. Half of 255 is 127.5, and there is no byte between 127 and 128. Rounding up to 80 gives you 0.50196.
RGBA vs HEX Color CSS: Which One to Use
Both formats produce identical rendered output when the values match, so the choice comes down to how you use the value.
Use rgba() when the alpha needs to change independently of the color, or when the color comes from a CSS variable. Theming systems commonly need exactly that:
:root {
--brand-rgb: 59 130 246;
}
.card {
background: rgb(var(--brand-rgb) / 0.08);
border: 1px solid rgb(var(--brand-rgb) / 0.24);
}
You cannot do that with hex. There is no way to swap just the alpha pair of a hex string in plain CSS without preprocessing.
Use an 8 digit hex code with opacity when you need a single opaque token that travels between tools. Design tools, JSON theme files, and most component libraries store colors as one string, and #00000099 survives that round trip better than a split RGB triple plus a separate alpha number.
Hex is also shorter in a stylesheet, which is marginal for one rule and adds up across a large design system.
If your theme tokens live in a JSON file, running them through a JSON formatter makes it much easier to spot a malformed hex value that is 7 characters instead of 8.
Browser Support for the CSS Alpha Channel Hex Value
Every current browser supports 8 digit hex. Chrome, Edge, Firefox, and Safari have all shipped it for years, and it works in mobile Safari and Chrome for Android.
The one real gap is Internet Explorer 11, which ignores the declaration entirely. An 8 digit hex value does not degrade gracefully to a 6 digit color if IE11 is still in your support matrix: IE11 drops the whole declaration, so the element keeps whatever value an earlier rule already set.
The fallback pattern is a plain hex declaration followed by the transparent one:
.notice {
background: #eff6ff; /* IE11 sees this, solid fallback */
background: #3b82f61a; /* modern browsers override with alpha */
}
Email clients are the other place to be careful. Several still parse only 6 digit hex, so for HTML email you generally want a solid color or a background image rather than an alpha value.
Outside CSS, be aware that 8 digit hex is a CSS convention, not a universal one. Some APIs and older Android tooling expect #AARRGGBB with alpha first rather than last. If you are passing a color into a non-web system, confirm the channel order before assuming your converted value is correct.
Converting Transparent Colors to HEX in Practice
A few patterns come up repeatedly when you convert a transparent color to hex code across a real codebase.
Handle a one-off value by hand or with a converter. A whole stylesheet calls for a script instead: the parsing pattern above plus the rgbaToHex function will handle a bulk pass, though you should review the diff rather than trusting it blindly, since rgba() can also appear inside gradients and shadow values where the surrounding syntax matters.
Design tokens that need to work in both CSS and a native app should store the raw channels and generate both formats at build time. Storing only the hex string means every consumer has to parse it back apart.
To sanity check any converted value, paste it into DevTools. Open the Elements panel, find a rule with a color swatch, and click the swatch. Chrome and Firefox both cycle between hex, rgba, and hsl representations of the same color when you shift-click it, which gives you an immediate second opinion on whether your conversion is right.
You can also verify by setting the converted hex on an element and comparing it against the original rgba side by side. If the two swatches are indistinguishable at full size, the rounding is within tolerance for visual work.
Converting RGBA to HEX With Alpha Without the Manual Math
The math is simple enough to do in your head for round numbers, but a converter is faster and does not make padding mistakes. A good rgba to hex converter online should show a live preview swatch so you can confirm the transparency looks right against a real background rather than trusting the digits.
If you are handling color values that arrive base64 encoded inside a data URI or a config blob, decode them first with a base64 tool so you are converting the actual color string and not the encoding.
To convert rgba to hex with alpha instantly, with a live preview and correct byte rounding on the alpha channel, use the RGBA to HEX Converter on Toolblip. Paste any rgba value and copy the 8 digit hex code straight into your stylesheet.
Ready to try it yourself?
Browse Free Tools →