8 Digit Hex Colors

By  on  

One of the most requested capabilities in my early days of web development was the ability to set opacity on elements and even PNG images without the need for browser-specific CSS or hacks.  Eventually we got native opacity support and even enjoyed rgba(), the ability to cite an opacity level with a color.

While I loved the ability to provide an opacity with color using rgba(), the use of hexadecimal color had always been ingrained in me, which made the sudden need for rgba conversions annoying.  Luckily these days we have 8 digit hex values with allow us to cite an opacity level as the last two characters of the hex value!

The CSS

Hex implies "six" but the last two digits of the 8 character hex value are the opacity value:

.myElement {
    /* { red/50% opacity } */
    /* pink of white background */
    background: #ff000080;
}

I love that we can use hexidecimal colors with the additional opacity value; needing to convert to rgba to take advantage of opacity was a needless frustration.  I owe something to the creator of this API!

Recent Features

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

  • By
    Introducing MooTools LinkAlert

    One of my favorite Firefox plugins is called LinkAlert. LinkAlert shows the user an icon when they hover over a special link, like a link to a Microsoft Word DOC or a PDF file. I love that warning because I hate the surprise...

  • By
    HTML5 Placeholder Styling with CSS

    Last week I showed you how you could style selected text with CSS. I've searched for more interesting CSS style properties and found another: INPUT placeholder styling. Let me show you how to style placeholder text within INPUTelements with some unique CSS code. The CSS Firefox...

Discussion

  1. Charles

    OK. This is good to know, not because I will ever use an 8 digit Hex colour, but because I need to build this into all my Hex colour validation routines!

  2. Sebastian

    Hi, thanks for that post, I haven’t heard of this before. I would like to point out that hex does not imply ‘six’ but is short for hexadecimal which are number to the base of 16. Because of that also your example has a problem. The alpha channel’s value is also in hex so 50 does not convert to 50% but instead to 80. Since the same logic from the color channels, minimum is 00 (0)maximum is ff (255), needs to be applied, a hex value of 50 (80) equals to 80/255 = ~31.3 %

    best regards
    Sebastian

  3. Aleksandar

    It means that you can set opacity to 255%, right?

  4. Koen

    I’d like to point out that the last two digits are not percentages but also hexadecimal numbers (16 values, from 0 to F). If you want 50% red you’ll need #FF000080, #FF000000 is 0% red and #FF0000FF is 100% red.

  5. Marcel

    Very nice! Unfortunately not the best browser support if you want to include IE or Edge: https://caniuse.com/#feat=css-rrggbbaa

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!