0

how to pass color rgb inside rgba to decrease opacity?

:root {
  --bg: rgb(84, 15, 84);
}

p {
  background-color: var(--bg);
  color: #fff;
}
<p>
  hello world
</p>

I want to pass the --bg variable inside rgba so I can decrease the opacity without touching touching the --bg variable.

I have tried:

background-color: rgba( var(--bg), 0.4 );

but it doesn't work. Can anyone help?

  • https://stackoverflow.com/a/41265350/13405106 – Usama Apr 13 '22 at 08:25
  • You should use only the numbers of rgb in the var. https://stackoverflow.com/questions/40010597/how-do-i-apply-opacity-to-a-css-color-variable – Crisped Apr 13 '22 at 08:27

2 Answers2

1

You need to remove the rgb extension while passing it to the variable like so.

:root {
  --bg: 84, 15, 84;
}

p {
  background-color: rgba(var(--bg), 0.4);
  color: #fff
}
<p>
  hello world
</p>
Amini
  • 1,620
  • 1
  • 8
  • 26
1

:root {
  --bg: 84, 15, 84;
}

p {
  background-color: rgba(var(--bg),0.4);
  color: #fff;
}
<p>
  hello world
</p>
Muhammad Usama
  • 307
  • 1
  • 8