color
8-Digit Hex Colors: CSS #RRGGBBAA vs Android #AARRGGBB
The fourth pair in an 8-digit hex color is alpha. CSS puts it last, Android and Flutter put it first. What each order means, what goes wrong when you mix them, and a percent-to-hex table.
By Kyle Hurst ·
A designer hands you #80FF0000 for “50% red”. You paste it into your CSS and the element vanishes. Or it goes the other way: your stylesheet has #FF000080, you copy it into an Android colors.xml, and a solid navy blue appears where a soft red should be. Both strings are valid 8-digit hex colors with an alpha (opacity) pair. CSS reads the alpha from the last two digits and Android reads it from the first two, so the same eight characters mean two different colors. The fix is to move one pair from one end to the other. The rest of this post shows where each order comes from, what you get when you mix them up, and how to turn an opacity percentage into the right two hex digits.
The fourth pair is alpha
A 6-digit hex color is three bytes: red, green, blue, each from 00 to FF. An 8-digit color adds a fourth byte for alpha, where 00 is fully transparent and FF is fully opaque. CSS Color Module Level 4 defines it like this:
The first 6 digits are interpreted identically to the 6-digit notation. The last pair of digits, interpreted as a hexadecimal number, specifies the alpha component of the color, where 00 represents a fully transparent color and ff represent a fully opaque color.
The spec’s own example is #0000ffcc, which “represents the same color as rgb(0 0 100% / 80%)”. CC is 204, and 204 / 255 is exactly 0.8. Case doesn’t matter; MDN’s hex-color page notes that “#00ff00 is the same as #00FF00”.
So in CSS the layout is #RRGGBBAA:
#6366F180
RR GG BB AA
63 66 F1 80 -> rgb(99 102 241 / 50.2%)
Android and Flutter put alpha first
Android’s color resource documentation says a color value “always begins with a pound (#) character, which is followed by the Alpha-Red-Green-Blue information in one of the following formats”, and lists #RGB, #ARGB, #RRGGBB and #AARRGGBB. The page’s own example is a 50% red:
<color name="translucent_red">#80ff0000</color>
The same order runs through the code APIs. Color.parseColor lists two supported formats, #RRGGBB and #AARRGGBB, and “If the string cannot be parsed, throws an IllegalArgumentException exception.” The reason for the order is how Android stores a color as one 32-bit integer. The Color class gives the encoding as:
int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
Alpha sits in the top byte, so when you write the integer in hex, alpha comes out first. The same page calls these “ARGB colors” and gives opaque blue as 0xff0000ff.
Flutter uses the same integer layout. The Color constructor documentation says “Bits 24-31 are the alpha value”, that a color “can be expressed as Color(0xAARRGGBB)”, and gives const Color(0xFFFF9000) for a fully opaque orange. A Flutter Color(0x80FF0000) is the same 50% red as Android’s #80ff0000.
| Where | Format | 50% red |
|---|---|---|
| CSS | #RRGGBBAA | #FF000080 |
Android XML resource, Color.parseColor | #AARRGGBB | #80FF0000 |
| Flutter | Color(0xAARRGGBB) | Color(0x80FF0000) |
When the alpha is FF the difference still matters. #FF6366F1 is an opaque indigo on Android, and in CSS it reads as red FF, green 63, blue 66 at alpha F1, a 94.5%-opaque pinkish red.
What you get when you paste one into the other
Neither side rejects the other’s format. Eight hex digits are valid in both, so you get a wrong color with no error. Here are three cases, each run through our Color Converter in both modes:
| You meant | Pasted into | Read as | Result |
|---|---|---|---|
Android #80FF0000 (red at 50%) | CSS | R 80, G FF, B 00, A 00 | rgba(128, 255, 0, 0): fully transparent, the element disappears |
CSS #FF000080 (red at 50%) | Android | A FF, R 00, G 00, B 80 | rgb(0, 0, 128): opaque navy |
Android #CC2196F3 (a blue at 80%) | CSS | R CC, G 21, B 96, A F3 | rgba(204, 33, 150, 0.953): nearly opaque magenta |
The first case is the nastiest because nothing is visible to look at. A color whose last pair is 00 is invisible in CSS no matter what the first six digits say. If an Android color you copied into CSS shows nothing at all, check whether it started with a low alpha byte.
The fix in both directions is to move the alpha pair:
CSS -> Android: #RRGGBBAA -> #AARRGGBB #FF000080 -> #80FF0000
Android -> CSS: #AARRGGBB -> #RRGGBBAA #80FF0000 -> #FF000080
The short forms swap the same way, one digit at a time. CSS #F008 is red at alpha 88; Android reads the same four characters as #ARGB, alpha F, red 0, green 0, blue 8, which expands to #FF000088, an opaque dark navy.
Opacity percentage to hex alpha
To write “60% opacity” as hex, multiply by 255, round to a whole number, and write that number in hex. The table below was generated by the same code as our converter, which rounds halves up. The last column is what the byte really represents, because 255 steps can’t hit every percentage exactly.
| Opacity | × 255 | Byte | Hex | Byte is really |
|---|---|---|---|---|
| 0% | 0 | 0 | 00 | 0% |
| 5% | 12.75 | 13 | 0D | 5.1% |
| 10% | 25.5 | 26 | 1A | 10.2% |
| 15% | 38.25 | 38 | 26 | 14.9% |
| 20% | 51 | 51 | 33 | 20% |
| 25% | 63.75 | 64 | 40 | 25.1% |
| 30% | 76.5 | 77 | 4D | 30.2% |
| 35% | 89.25 | 89 | 59 | 34.9% |
| 40% | 102 | 102 | 66 | 40% |
| 45% | 114.75 | 115 | 73 | 45.1% |
| 50% | 127.5 | 128 | 80 | 50.2% |
| 55% | 140.25 | 140 | 8C | 54.9% |
| 60% | 153 | 153 | 99 | 60% |
| 65% | 165.75 | 166 | A6 | 65.1% |
| 70% | 178.5 | 179 | B3 | 70.2% |
| 75% | 191.25 | 191 | BF | 74.9% |
| 80% | 204 | 204 | CC | 80% |
| 85% | 216.75 | 217 | D9 | 85.1% |
| 90% | 229.5 | 230 | E6 | 90.2% |
| 95% | 242.25 | 242 | F2 | 94.9% |
| 100% | 255 | 255 | FF | 100% |
Only 0%, 20%, 40%, 60%, 80% and 100% land exactly on a byte. 50% is the one people argue about: 127.5 rounds up to 128, which is 80. If a table or a tool gives 7F for 50%, it rounded 127.5 down, usually by truncating, instead of up. 7F is 49.8% and 80 is 50.2%, a difference of 1/255 that you won’t see on screen. What matters more is being consistent, so the same percentage always produces the same string in your design tokens.
Going the other way, divide by 255: B3 is 179, and 179 / 255 is 70.2%.
The 4-digit shorthand
CSS also has #RGBA, which the spec calls “a shorter variant of the 8-digit notation, “expanded” in the same way as the 3-digit notation is”. Each digit is doubled:
#F008 -> #FF000088 red at 136/255 = 53.3%
#0008 -> #00000088 black at 53.3%
#FFFC -> #FFFFFFCC white at 80%
Because each digit is doubled, the shorthand can only express alpha bytes like 00, 11, 22 … FF, which are steps of 1/15. There’s no 4-digit spelling for exactly 50%. #F008 is 53.3% and #F007 is 46.7%. Android’s #ARGB resource format is the same idea with the alpha digit first.
Browser support, and what an old browser does
caniuse lists #rrggbbaa (and the 4-digit form) as supported from Chrome 62, Edge 79, Firefox 49, Safari 10 and Safari on iOS 10, and not supported in any version of Internet Explorer. Current versions of Chrome, Edge, Firefox and Safari all handle it; caniuse still lists Opera Mini as unsupported.
A browser that doesn’t recognise the syntax treats the value as invalid, and CSS has a fixed rule for that. CSS 2.1 says “User agents must ignore a declaration with an illegal value.” The whole color or background line is dropped, not just the alpha. If you still have to support such a browser, put a fallback declaration first:
.banner {
background: rgb(255, 0, 0); /* older parsers use this */
background: #ff000080; /* and skip this one */
}
Outside browsers, check before you rely on it. A color parser written for 3- or 6-digit hex may reject 8 digits, or quietly read only the first six and drop the alpha.
Alpha in the color vs the opacity property
An 8-digit hex color makes only that one color transparent. The opacity property fades the element and everything inside it. MDN’s opacity page puts it this way: “opacity applies to the element as a whole, including its contents, even though the value is not inherited by child elements.” To make only a card’s background see-through while its text stays solid, use a color with alpha, such as background: #ffffff99, not opacity: 0.6.
Converting with our Color Converter
Our Color Converter takes #RGB, #RGBA, #RRGGBB and #RRGGBBAA (with or without #), rgb()/rgba() and hsl()/hsla() in both the comma syntax and the space syntax with / alpha, and CSS color names. Type rgb(99 102 241 / 50%) and you get:
| Row | Value |
|---|---|
| HEX | #6366f180 |
| RGB | rgba(99, 102, 241, 0.5) |
| HSL | hsla(239, 84%, 67%, 0.5) |
| Android (#AARRGGBB) | #806366f1 |
| Alpha | 50.2% (0x80 = 128/255) |
When you type a 4- or 8-digit hex, a toggle appears: Read 8-digit hex as: CSS #RRGGBBAA / Android #AARRGGBB. It starts on CSS. Paste #80FF0000 with Android selected and the HEX row gives you the CSS version, #ff000080; leave it on CSS and the swatch shows only its checkerboard, because CSS reads that value as fully transparent. The swatch sits on a checkerboard so you can see how transparent the color is. Alpha values above 1 or 100% are clamped to fully opaque, as CSS does, and a 5- or 7-digit hex is flagged as invalid.
To check that a translucent text color is still readable on its background, use the contrast checker with the opaque color you see on screen, and see how WCAG contrast ratios work.
Quick fix
- Count the digits. Six means no alpha. Eight means alpha is in there somewhere.
- Find out where the value came from. CSS, SVG and most web tools put alpha last. Android XML,
Color.parseColorand Flutter’sColor(0x…)put it first. - Moving it between the two? Cut the alpha pair from one end and paste it on the other.
- Turning a percentage into hex? Multiply by 255, round, convert. 50% is
80. - If you aren’t sure which order a value is in, paste it into the Color Converter and flip the toggle. Whichever reading gives the color you expected tells you the order.