Setting Background Image Opacity #3617
-
Hello! The Tailwind docs clearly show how to set the opacity of a background colour but this does not seem to work when applying it to a background image. For example:
Doesn't seem to change the opacity of the example image here for me and it seems that you need workarounds to mimic a transparent background image in CSS in general Is there a way to achieve this in Tailwind? Or would I need to use custom css like this solution |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Hey! Currently it's not possible to add transparency to a background image since the I'd achieve it by having an "overlay" div with transparent background over the top of the image. Not exactly the same effect, but an overlayed image works quite well in many situations 👍 Here's an example in Tailwind Play: https://play.tailwindcss.com/RnQTIA1FS1 Hope it helps! |
Beta Was this translation helpful? Give feedback.
-
Why is it currently not possible? because there's no background image opacity in css? |
Beta Was this translation helpful? Give feedback.
-
This worked fine for me: https://stackoverflow.com/questions/64663368/background-image-with-opacity-in-tailwindcss <header
class="relative bg-fixed bg-center bg-cover bg-no-repeat"
style="background-image:linear-gradient(rgba(135, 80, 156, 0.9), rgba(135, 80, 156, 0.9)), url(img/hero-bg.jpg)">
</header> With some tweaks: <div class="relative bg-fixed bg-center bg-cover bg-no-repeat"
:style="`background-image:linear-gradient(hsl(var(--b1) / 0.9), hsl(var(--b2) / 0.5)), url(${favlist.backgroundImage})`"> |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Hey!
Currently it's not possible to add transparency to a background image since the
bg-opacity-{n}
updates the--tw-bg-opacity
CSS variable, which affects background color classes - not background images.I'd achieve it by having an "overlay" div with transparent background over the top of the image. Not exactly the same effect, but an overlayed image works quite well in many situations 👍
Here's an example in Tailwind Play:
https://play.tailwindcss.com/RnQTIA1FS1
Hope it helps!