1

How can I easily convert a hex color value to a transparent one in CSS?

For example, I have the color #ECC040. How can I add opacity to it by adding a suffix #ECC040xx where xx is the alpha value.

This question has been asked before long ago and similar here but I'm asking again since I can see that if I add an alpha value of 33 or 66 (#ECC04033 or #ECC04066) then the hex color becomes transparent accordingly in CSS but most of the other values don't seem to work.

According to this source the #rrggbbaa notation is supported by most of the modern browsers.

Update: I managed to solve my problem. It was fairly simple in the end.

.background{
background-color:red;
}
.container1{
  height: 30px;
  width: 100%;
  background-color: #ECC0401A; /*Opacity 10%*/
}
.container2{
  height: 30px;
  width: 100%;
  background-color: #ECC04033; /*Opacity 20%*/
}
.container3{
  height: 30px;
  width: 100%;
  background-color: #ECC04066; /*Opacity 40%*/
}
.container4{
  height: 30px;
  width: 100%;
  background-color: #ECC04099; /*Opacity 60%*/
}
.container4{
  height: 30px;
  width: 100%;
  background-color: #ECC04099; /*Opacity 60%*/
}
.container5{
  height: 30px;
  width: 100%;
  background-color: #ECC040CC; /*Opacity 80%*/
}
.container6{
  height: 30px;
  width: 100%;
  background-color: #ECC040FF; /*Opacity 100%*/
}
<div class="background">
  <div class="container1"></div>
  <div class="container2"></div>
  <div class="container3"></div>
  <div class="container4"></div>
  <div class="container5"></div>
  <div class="container6"></div>
  <div class="container7"></div>
</div>
Barnabas
  • 139
  • 10
  • What do you mean by 'but most of the other values don't seem to work'. ? Which other values? – IcyIcicle Oct 22 '21 at 22:52
  • Could you put some examples in your question because I am not seeing a problem. – A Haworth Oct 22 '21 at 22:52
  • It seems that the other values I was trying were wrong. @AHaworth I have added a snippet that shows the solution that it works. Shall I keep this question since it is a duplicate one? – Barnabas Oct 23 '21 at 21:07

2 Answers2

2

There’s no way to do like you want, you need to add the 2 letter at the start of Hex color.

This is another post with many answers to your question.

Hex transparency in colors

Table transparent value for HEX

100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8
90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF
80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5
70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C
60% — 99
59% — 96
58% — 94
57% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82
50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69
40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F
30% — 4D
29% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36
20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C
10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00
Barnabas
  • 139
  • 10
Jo V
  • 25
  • 4
  • Thank you for the table above. However, in CSS I must add the letters for opacity at the end of the code, not at the beginning. – Barnabas Oct 23 '21 at 21:10
1

100% is FF or 255, so 50% is 7F or 127.

The color value you need is simply #ECC0407F, or if you’re having difficulty with that you can use rgba(236, 192, 64, 0.5).

If you still find that something’s not working, create a snippet to demonstrate your problem.

Brett Donald
  • 6,745
  • 4
  • 23
  • 51
  • Thank you Brett, I have just done this. I was using the wrong values at the beginning that's why it was not working. – Barnabas Oct 23 '21 at 21:11
  • @Brett Donald my issue is, that I cannot write something like `background-color: rgba(#ff0000, 0.2);` I believe. I really wish I could, because I've learned to calculate RGB colors in my head and would like to adjust a color specified using e.g. a CSS3 variable by the given alpha value, which also might be a CSS3 variable (e.g. `background-color: rgba(--my-color, --my-color-opacity);`). I think it's not possible yet though. – Igor Oct 26 '22 at 23:28