291

I know you can write ...

background-color: #ff0000;

... if you want something that is red.

And you can write ...

background-color: rgba(255, 0, 0, 0.5);

... if you want something red and translucent.

Is there any terse way of writing partially transparent colors in hexadecimal? I want something like:

background-color: #ff000088; <--- the 88 is the alpha

... or ...

background-color: #ff0000 50%;

I am getting all my colors in hexadecimal, and having to convert them all to the decimal 0-255 scale is annoying.

Mahmoud Turki
  • 107
  • 12
Li Haoyi
  • 15,330
  • 17
  • 80
  • 137
  • 2
    You may experiment with different notations, but I didn't succedd. `rgb(0xff,\x80,#44)` astonishingly Octal representation seem to york a little bit `rgb(0100, 0200,0300)` is `#4080C0` – yunzen Nov 23 '11 at 09:50
  • You may want to use ColorPic, which shows the decimal figures for a picked color http://www.iconico.com/colorpic/ – yunzen Nov 23 '11 at 09:52
  • 2
    As Slomojo suggested, in LESS you can do background-color: #ff0000 + rgba(0, 0, 0, .8); which will convert the hex for you and still apply the transparency but this can't be done using native CSS. – fl3x7 Oct 06 '13 at 10:01
  • Using the `opacity` css property works for me, as Charming Prince suggest below. – here Dec 31 '13 at 02:40
  • 3
    For completeness, as Slomojo and fl3x7 discuss, [this can be done in Sass easily](http://sass-lang.com/documentation/file.SASS_REFERENCE.html): `background-color: opacify(#ff0000, 0.5);` or `background-color: transparentize(#ff0000, 0.5);` You can supply Sass hex color variables to those Sass functions as well. – Adam Caviness Jun 23 '15 at 01:57
  • @fl3x7 I tried #ff0000 + rgba(0,0,0,0.8); using LESS but it didn't work. The result was still hex. Maybe a browser issue. However, in LESS there is a fade function that works. See [my answer](http://stackoverflow.com/a/31145242/179412) below – Julian Mann Jun 30 '15 at 18:07
  • 2
    Possible duplicate of [Hex representation of a color with alpha channel?](http://stackoverflow.com/questions/1419448/hex-representation-of-a-color-with-alpha-channel) – thelem Nov 23 '15 at 15:51
  • PostCSS library, but I believe this goes outside of future-proof standards. Still gets the notation with a preprocessor... https://www.npmjs.com/package/postcss-hexrgba – Jake Oct 23 '17 at 23:49
  • This tool is perfect for hexadecimal to rgba conversion : https://www.htmlcsscolor.com/hex/ECECEC Check the CMYK line to get infos. – djibe Jul 25 '19 at 22:16
  • See which browsers support #rrggbbaa notation... https://caniuse.com/css-rrggbbaa – god_is_love Aug 29 '23 at 18:13

15 Answers15

167

The CSS Color Module Level 4 will probably support 4 and 8-digit hexadecimal RGBA notation!

Three weeks ago (18th of December 2014) the CSS Color Module Level 4 editor's draft was submitted to the CSS W3C Working Group. Though in a state which is heavily susceptible to change, the current version of the document implies that in the somewhat near future CSS will support both the 4 and 8-digit hexadecimal RGBA notation.

Note: the following quote has irrelevant chunks cut out and the source may have been heavily modified by the time you read this (as mentioned above, it's an editor's draft and not a finalised document).
If things have heavily changed, please leave a comment letting me know so I can update this answer!

§ 4.2. The RGB hexadecimal notations: #RRGGBB

The syntax of a <hex-color> is a <hash-token> token whose value consists of 3, 4, 6, or 8 hexadecimal digits. In other words, a hex color is written as a hash character, "#", followed by some number of digits 0-9 or letters a-f (the case of the letters doesn’t matter - #00ff00 is identical to #00FF00).

8 digits

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 channel of the color, where 00 represents a fully transparent color and ff represent a fully opaque color.

Example 3
In other words, #0000ffcc represents the same color as rgba(0, 0, 100%, 80%) (a slightly-transparent blue).

4 digits

This is a shorter variant of the 8-digit notation, "expanded" in the same way as the 3-digit notation is. The first digit, interpreted as a hexadecimal number, specifies the red channel of the color, where 0 represents the minimum value and f represents the maximum. The next three digits represent the green, blue, and alpha channels, respectively.

What does this mean for the future of CSS colours?

This means that assuming this isn't completely removed from the Level 4 document, we'll soon be able to define our RGBA colours (or HSLA colours, if you're one of those guys) in hexadecimal format in browsers which support the Color Module Level 4's syntax.

Example

elem {
    background: rgb(0, 0, 0);           /* RGB notation (no alpha). */
    background: #000;                   /* 3-digit hexadecimal notation (no alpha). */
    background: #000000;                /* 6-digit hexadecimal notation (no alpha). */
    background: rgba(0, 0, 0, 1.0);     /* RGBA notation. */

    /* The new 4 and 8-digit hexadecimal notation. */
    background: #0000;                  /* 4-digit hexadecimal notation. */
    background: #00000000;              /* 8-digit hexadecimal notation. */
}

When will I be able to use this in my client-facing products?

Tumble weed is the only real response...

All jokes aside: it's currently only the start of 2015, so these will not be supported in any browser for quite some time yet - even if your product is only designed to work on the most up-to-date of browsers you'll probably not be seeing this in action in a production browser any time soon.

View current browser support for #RRGGBBAA color notation

However, that said, the way CSS works means that we can actually start using these today! If you really want to start using them right now, as long as you add a fall back any non-supporting browsers will simply ignore the new properties until they are deemed valid:

figure {
  margin: 0;
  padding: 4px;
  
  /* Fall back (...to browsers which don't support alpha transparency). */
  background: #FEFE7F;
  color: #3F3FFE;
  
  /* Current 'modern' browser support. */
  background: rgba(255, 255, 0, 0.5);
  color: rgba(0, 0, 255, 0.75);
  
  /* Fall... foward? */
  background: #ffff007F; /* Or, less accurately, #ff08 */
  color: #0000ffbe;      /* Or #00fc */
}
<figure>Hello, world!</figure>

As long as you're viewing this answer on a browser which supports the background and color properties in CSS, the <figure> element in result of the above snippet will look very similar to this:

Code Snippet Result Image

Using the most recent version of Chrome on Windows (v39.0.2171) to inspect our <figure> element, we'll see the following:

Element Inspector Example

The 6-digit hexadecimal fall back is overridden by the rgba() values, and our 8-digit hexadecimal values are ignored as they are currently deemed invalid by Chrome's CSS parser. As soon as our browser supports these 8-digit values, these will override the rgba() ones.

UPDATE 2018-07-04: Firefox, Chrome and Safari are support this notation now, Edge still missing but will probably follow (https://caniuse.com/#feat=css-rrggbbaa).

Blackbam
  • 17,496
  • 26
  • 97
  • 150
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • 2
    I remember seeing this brought up in the mailing lists a year or so ago at the latest (the idea of having 4-/8-digit hex is nothing new). Glad to see it's entered Colors 4 for now. – BoltClock Jan 06 '15 at 15:55
  • @BoltClock hopefully it'll stay here, too! – James Donnelly Jan 06 '15 at 16:02
  • 1
    [Firefox Nightly](https://twitter.com/ziyunfei/status/729896688534441988) has just gotten this implemented :) – Nebula May 20 '16 at 13:10
  • `/* Fall... foward? */` » I suggest jumpforward. Fallback and jumpforward :^p – WoodrowShigeru Mar 01 '18 at 10:42
  • Just adding a useful link. Link contains opacity value list in hex. https://gist.github.com/lopspower/03fb1cc0ac9f32ef38f4 – Ashish Kumar May 01 '18 at 12:05
  • What was the image that appeared here? It's been replaced by an "iStock" placeholder. – Alex Reinking Feb 13 '19 at 20:25
  • @AlexReinking for some stupid reason my work blocks Stack Overflow's image host. It should, off the top of my head, be an image of tumbleweed. – James Donnelly Feb 14 '19 at 09:53
  • Note that the `rgba()` function is now considered legacy. They will standardize on `rgb()`. Here's the modern way to add the alpha value: `rgb(0 0 0 / 1)`. [Reference](https://drafts.csswg.org/css-color/#rgb-functions) – thdoan Jun 11 '23 at 21:23
107

I found the answer after posting the enhancement to the question. Sorry!

MS Excel helped!

simply add the Hex prefix to the hex colour value to add an alpha that has the equivalent opacity as the % value.

(in rbga the percentage opacity is expressed as a decimal as mentioned above)

Opacity %   255 Step        2 digit HEX prefix
0%          0.00            00
5%          12.75           0C
10%         25.50           19
15%         38.25           26
20%         51.00           33
25%         63.75           3F
30%         76.50           4C
35%         89.25           59
40%         102.00          66
45%         114.75          72
50%         127.50          7F
55%         140.25          8C
60%         153.00          99
65%         165.75          A5
70%         178.50          B2
75%         191.25          BF
80%         204.00          CC
85%         216.75          D8
90%         229.50          E5
95%         242.25          F2
100%        255.00          FF
T9b
  • 3,312
  • 5
  • 31
  • 50
  • 1
    Don't relly understand. Do you mean `#FF00000C` is equivalent to `rgba(255,0,0,0.05)`? – yunzen Nov 25 '11 at 09:15
  • @PointedEars wrong on both counts. The OP does not specifically mention CSS3, and this question I would have thought is likely to be more related to IE specific issues which we all know are difficult. None-the-less IE uses both CSS and Hex transparency values in it's CSS. – T9b Nov 28 '11 at 08:49
  • 8
    @T9b Nonsense. The OP has a **CSS** question about RGBA values. There is no RGBA support in CSS before CSS3. If IE/MSHTML supports your notation, then that is **proprietary**, not compatible, and inherently unreliable. And surely you are not suggesting to use client-side scripting to make that notation work, are you? For that would not be compatible and would be unreliable as well. – PointedEars Nov 28 '11 at 11:24
  • @T9b Of course, you can make that work reliably, with reduced compatibility, server-side. If so, the OP should state their environment and programming language as there are far too many possibilities. – PointedEars Nov 28 '11 at 11:28
  • Firefox rounds color channels to integers and the alpha channel to thousandths. If I enter something as `rgba(127.5,127.5,127.5,0.5)`, I get `rgba(128,128,128,0.5)` and that's the same as `#80808080` in modern browsers that support rgba hex colors. `#7f7f7f7f` is translated to `rgba(127,127,127,0.498)`. This chart is otherwise correct, say at 20% (`#3333` = `#33333333` = `rgba(51,51,51,0.2)`) and at 60% (`#9999` = `#99999999` = `rgba(153,153,153,0.6)`). – Adam Katz Oct 14 '21 at 20:45
  • 3
    Just for reference RGBA is now supported on most browsers as of 2022-08-01 – ocodo Aug 08 '22 at 12:00
45

#rrggbbaa notation is fully supported in Chrome 62+ and all other evergreen browsers:

background: #56ff0077;
Tamlyn
  • 22,122
  • 12
  • 111
  • 127
Oded Breiner
  • 28,523
  • 10
  • 105
  • 71
  • 4
    I was excited to see this, and then tried it in devtools on Chrome 55. No dice, it errors as an invalid property value... `¯\_(ツ)_/¯` – ericsoco Jan 18 '17 at 21:04
  • 1
    Looks like it has had various issues [here's](https://www.chromestatus.com/feature/5685348285808640) the status page for the feature. – Billy Jan 31 '17 at 22:20
  • Chrome has removed support, Safari and Firefox have support. Do not use this. – Case Jan 31 '17 at 22:33
  • 10
    As of July 2019, there are 84.5% of all browsers globally supporting this feature. https://caniuse.com/#search=rrggbbaa – André Kuhlmann Jul 21 '19 at 20:00
  • 2
    I think it is once again working in latest chrome: Version 95.0.4638.54 (Official Build) (x86_64) – Zargold Oct 27 '21 at 20:54
27
RGB='#ffabcd';
A='0.5';
RGBA='('+parseInt(RGB.substring(1,3),16)+','+parseInt(RGB.substring(3,5),16)+','+parseInt(RGB.substring(5,7),16)+','+A+')';
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
James Alarie
  • 271
  • 2
  • 2
  • `var rgb = '#ffabcd'; var a = '0.5'; var matches = rgb.match(/#([\da-f]{2})([\da-f]{2})([\da-f]{2})/i); var rgba = 'rgba(' + matches.slice(1).map(function(m) { return parseInt(m, 16); }).concat(a) + ')';` – PointedEars Nov 27 '11 at 14:47
  • 5
    If you are coming from a world of React: ```
    ```
    – atulmy Aug 09 '17 at 10:04
  • You can use this tool to convert - https://kilianvalkhof.com/2016/css-html/css-hexadecimal-colors-with-transparency-a-conversion-tool/ – Vadim Kotov Dec 22 '20 at 14:23
23

Sass has red, green, blue to extract to channels from a hex color:

background-color: rgba(red($hex_color), green($hex_color), blue($hex_color), 0.2);

Sass also has rgba($hex_color, $alpha)

ocodo
  • 29,401
  • 18
  • 105
  • 117
Jeremy Lynch
  • 6,780
  • 3
  • 52
  • 63
16

In Sass we can write:

background-color: rgba(#ff0000, 0.5);

as it was suggested in Hex representation of a color with alpha channel?

iwis
  • 1,382
  • 1
  • 13
  • 26
  • This is for Sass – Steven Vachon Apr 21 '22 at 18:27
  • 1
    The disadvantage is, that the original color - `#ff0000` - is modified and **the result** is then put in place in the final CSS file. This makes it impossible to control the alpha channel via e.g. a CSS3 variable. – Igor Oct 26 '22 at 23:20
14

See here http://www.w3.org/TR/css3-color/#rgba-color

It is not possible, most probably because 0xFFFFFFFF is greater than the maximum value for 32bit integers

Griffin
  • 13,184
  • 4
  • 29
  • 43
  • 28
    `#ffffff` (six hex-digits) is actually a **24-bit** color value; 0xFFFFFFFF is actually 2³²−1, and can be represented as *unsigned* 32-bit integer. I think the reason why `#ffffffff` (eight hex-digits) is not specified is both that CSS colors are rooted in sRGB color space and that an eight-hex-digit notation would be ambiguous with a display device that supports a color depth of 32 bit. Remember that there is still `#fff` so that it means `rgb(100%, 100%, 100%)` – white – both with a color depth of 8 bit (256 colors) and 16 bit (65536 or 64K colors). – PointedEars Nov 27 '11 at 14:24
  • 5
    Actually that's exactly what 32bit integer is capable to hold. 2 hex digits can hold as many values as 8 binary digits. Or... 8 hex digits can hold as many values as 32 binary digits. – Pijusn May 11 '13 at 07:33
  • Though there's no major reason the CSS function shouldn't have been designed this way from the start (as well as rgba(0.0, 0.5, 1.0) floats for normals to match the other color functions). As for 32-bit display color support, this doesn't exist with 6-digit RGB to begin with and is really an edge case. I don't have an issue with learning different formats, but there are cases where you may get a dynamic color in hex and need to create CSS from it at runtime. This requires the extra step of converting from hex to decimal if you want to add alpha to it, which is clunky. – Beejor May 05 '15 at 15:17
8

I'm afraid that's not possible. the rgba format you know is the only one.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
7

If you have all your colors in HEX variables, you can use the following SCSS code:

First, copy this mapping from rgba - opacity values to hex codes:

    $opacity-to-hex: (
    0: '00',
    0.05: '0C',
    0.1: '19',
    0.15: '26',
    0.2: '33',
    0.25: '3F',
    0.3: '4C',
    0.35: '59',
    0.4: '66',
    0.45: '72',
    0.5: '7F',
    0.55: '8C',
    0.6: '99',
    0.65: 'A5',
    0.7: 'B2',
    0.75: 'BF',
    0.8: 'CC',
    0.85: 'D8',
    0.9: 'E5',
    0.95: 'F2',
    1: 'FF'
)

Then copy the following mixin that uses the mapping:

@mixin color-opacity($property, $color, $opacity) {
    #{$property}: unquote($color + map-get($opacity-to-hex, $opacity));
}

Last but not least, you can use this mixin with the mapped opacity on all color-properties, e.g. like this:

@include color-opacity('background-color', $your_hex_color, 0.8);
@include color-opacity('color', $your_hex_color, 0.5);
metal maniac
  • 119
  • 1
  • 3
6

If you can use LESS, there is a fade function.

@my-opaque-color: #a438ab;
@my-semitransparent-color: fade(@my-opaque-color, 50%);

background-color:linear-gradient(to right,@my-opaque-color, @my-semitransparent-color); 

// result: 
background-color: linear-gradient(to right, #a438ab, rgba(164, 56, 171, 0.5));
Julian Mann
  • 6,256
  • 5
  • 31
  • 43
2

Charming Prince:

Only internet explorer allows the 4 byte hex color in the format of ARGB, where A is the Alpha channel. It can be used in gradient filters for example:

filter  : ~"progid:DXImageTransform.Microsoft.Gradient(GradientType=@{dir},startColorstr=@{color1},endColorstr=@{color2})";

Where dir can be: 1(horizontal) or 0(vertical) And the color strings can be hex colors(#FFAAD3) or argb hex colors(#88FFAAD3).

Lajos Mészáros
  • 3,756
  • 2
  • 20
  • 26
1

In scss you can use the following:

background-color: rgba($color: #000088, $alpha: 0.5);
Vahe Aslanyan
  • 76
  • 1
  • 4
0

Well, different color notations is what you will have to learn.
Kuler gives you a better chance to find color and in multiple notations.
Hex is not different from RGB, FF = 255 and 00 = 0, but that's what you know. So in a way, you have to visualize it.
I use Hex, RGBA and RGB. Unless mass conversion is required, manually doing this will help you remember some odd 100 colors and their codes.
For mass conversion write some script like one given by Alarie. Have a blast with Colors.

-3

how about using an :after pseudo-element?

#myDiv{
  position: relative;
  width: 128px;
  height: 128px;
  background-color: #ccc;
}

#myDiv:after{
  content: '';
  position: absolute;
  left: 0; 
  top: 0;
  width: 100%; 
  height: 100%;
  pointer-events: none;
  
  /* here you go */
  margin: -8px;
  border: solid 8px #00f;
  opacity: 0.2;
}
<div id="myDiv">hello world!</div>
  • The question is "Is there any terse way of writing partially transparent colors in hexadecimal?". This doesn't seem even vaguely in the ballpark of answering that. It is much, much more verbose then the CSS 2 way the question mentioned: `background-color: rgba(255, 0, 0, 0.5);` – Quentin Apr 26 '22 at 14:02
-4

why not use background-color: #ff0000; opacity: 0.5; filter: alpha(opacity=50); /* in IE */

if you target a color for a text or probably an element, this should be a lot easier to do.

Charming Prince
  • 491
  • 3
  • 11
  • 11
    -1 opacity affect all of the content, not just the background-color – yunzen Nov 27 '11 at 21:39
  • then the object can be doubled, one should be the containing element, the other should be the one inside the container that has opacity. because from the question the user wants another method than the conventional rgba. – Charming Prince Nov 27 '11 at 22:01
  • 1
    He still wants the same result, and I'd better use rgba() or any other notation than modifying HTML. – FelipeAls Nov 27 '11 at 23:21
  • this is the answer I was looking for, leave the hex colors alone and change the opacity using the `opacity` css property. – here Dec 31 '13 at 02:38