Did you know there are 4 and 8 digit hex codes (#11223344?) for colours? 🤯

Did you know there are 4 and 8 digit hex codes (#11223344?) for colours? 🤯

Teaching an old dog new (CSS) tricks!

10 years as a developer and somehow I have missed that there are 4 digit (#1234) and 8 digit (#11223344) hex codes for colours⁉ Here is what I learned about what they are and how we use them!

Before we start though, a big shout out to ChallengesCss (the CSS guru!) who was the one who opened my eyes to this when discussing how to make an SVG "fade out" from one side to the other!

Prefer to consume content on Twitter?

Just as a very quick aside, if you prefer to read twitter threads rather than long form content then I have already created one about 4 and 8 digit hex codes here:


What do 8 digit Hexadecimal colour codes do?

In a nutshell they are similar to RGBA, essentially they let you add opacity with the last 2 characters.

So instead of:

#RRGGBB

As you would do in a normal 6 digit colour hex code you can do

#RRGGBBAA

and add an opacity value using the last 2 characters!

I have put together a quick demo showing this here where I use the same colour code (#336699) and then add bb to it to make it semi transparent (#336699bb):

With 8 digit colour hex codes there are a couple of "gotchyas"

Moving from a RGBA mindset to a 8 digit hex code system takes a tiny bit of getting used to!

The first 6 characters are just the same as they always are, but opacity is now on a scale of 0-255 ("00" to "ff")

So 00 still represents 0% opacity, but 255 (ff) now represents 100% opacity!

A few examples

Just to make it a bit more clear, here are a few examples:

Opacity8 digit Hex Code equivalentexplanation
0%#RRGGBB0000 in hexadecimal is still 0
10%#RRGGBB1A1A in hexadecimal is 26. 26 is ~10% of 255 (so 26/255 =~ 10%)
50%#RRGGBB801A in hexadecimal is 128. 128 is ~50% of 255 (so 128/255 =~ 50%)
90%#RRGGBBE6E6 in hexadecimal is 230. 230 is ~90% of 255 (so 230/255 =~ 90%)
100%#RRGGBBFFFF in hexadecimal is 255. 255 is 100% of 255 (so 255/255 = 100%)

Hopefully it is becoming clearer now, but just in case...


A mini tool to help understand 8 digit hex codes!

This tool will hopefully help it sink in (if it hasn't already)

Explanation

Enter the original colour you want to work with, either using the colour picker, hex colour or RGB colour (I had an issue with HSL so I have disabled those inputs 😪).

Then move the opacity slider or enter an opacity value.

Below that you will see a section with your final colour code and the resulting colour displayed on a striped background (assuming you lower the opacity of course!)

Below that there is a further explanation about the hex code it generated. It will also tell you if you can use a 4 digit hex code (more on that after the demo!)

8 digit hex code demonstration!

Have a play, there isn't much error checking though so check you enter valid numbers or...🔥💻🚒 could be the result! 🤣


4 digit hexadecimal colour codes

If you played with the above example long enough you may have noticed that occasionally you get a valid 4 digit hex code in the "results" or "details" section.

So how the heck does that work?

The rules are quite simple:

R1 = R2 G1 = G2 B1 = B2 A1 = A2

If all of the above are true then you can turn your 8 digit hex code into the short hand 4 digit hex code by taking R1, G1, B1 and A1 and combining them!

4 digit hex code examples:

#22446688 ✅
This can be shortened as 2==2 and 4==4 and 6==6 and 8==8. It will become #2468
#22456688 ❌
This can't be shortened as our G1 and G2 are not the same (4!==5). You would have to use the full 8 digit hex code #22456688
#AA4488DD ✅
Yet again R1==R2 (A==A), G1==G2(4==4) etc. So this can be turned into a 4 digit hex code (#A48D)

Making it a bit easier!

Trying to remember that "BF" is 191 in decimal which is 75% of 255 is hard work!

So I created this super handy table with all of the codes in it (or you can use the hex opacity demo I linked earlier)

Opacity to hex code conversion table

You can skip this long table if you want, but it might be worth bookmarking this article so you can use it for reference!

Opacity PercentEquivalent on 0-255 scale)Hex Code
100%255FF
99%252FC
98%250FA
97%247F7
96%245F5
95%242F2
94%240F0
93%237ED
92%235EB
91%232E8
90%230E6
89%227E3
88%224E0
87%222DE
86%219DB
85%217D9
84%214D6
83%212D4
82%209D1
81%207CF
80%204CC
79%201C9
78%199C7
77%196C4
76%194C2
75%191BF
74%189BD
73%186BA
72%184B8
71%181B5
70%179B3
69%176B0
68%173AD
67%171AB
66%168A8
65%166A6
64%163A3
63%161A1
62%1589E
61%1569C
60%15399
59%15096
58%14894
57%14591
56%1438F
55%1408C
54%1388A
53%13587
52%13385
51%13082
50%12880
49%1257D
48%1227A
47%12078
46%11775
45%11573
44%11270
43%1106E
42%1076B
41%10569
40%10266
39%9963
38%4931
37%945E
36%925C
35%8959
34%8757
33%8454
32%8252
31%794F
30%774D
29%744A
28%7147
27%6945
26%6642
25%6440
24%613D
23%593B
22%5638
21%5436
20%5133
19%4830
18%462E
17%432B
16%4129
15%3826
14%3624
13%3321
12%311F
11%281C
10%261A
9%2317
8%2014
7%1812
6%150F
5%130D
4%100A
3%808
2%505
1%303
0%000

Wrapping up on 8 digit hex codes!

So there you have it, a quick introduction to 4 and 8 digit hex codes!

If you want to read more about it the following resource might be useful: W3 CSS Color Module level 4


If you liked this article...

I hope you enjoyed this article.

If you did could you do me a quick favour and retweet the twitter thread I wrote about this?

It would really help me out and would be massively appreciated as I am trying to grow my tiny twitter following this year!

Thanks in advance and thank you for reading!

Final bit of silliness

Oh and because I didn't know this it is time to give back my developer card according to Alvaro Montoro, always good to know you can rely on friends to support you! 🤣