I know this is an old and answered thread, but it's the first result I got in google when searching for changing text opacity, so I will add my solution here for anyone else looking for this.
The answers here were not exactly satisfactory to me, using rgba
was not an option for me, I wanted the color of the text to be defined as a css variable, and alter it's opacity without changin the background opacity so opacity
property was not an option either.
However, I've figured out that there's a trick you can use with the new color-mix property. Simply mix the current text color with a completely transparent one and the mixing ration becomes the new color opacity:
:root {
--text-color: #fcba03;
}
.example {
color: var(--text-color);
background: black;
padding: 24px;
}
.opaque-text {
color: color-mix(in srgb, var(--text-color) 50%, rgba(255, 255, 255, 0));
}
<div class="example">
<span>
Regular Text
</span>
<br />
<span class="opaque-text">
Opaque Text
</span>
</div>
This can also be used with the currentColor
instead of var(--text-color)
.