Using tailwindcss, I have a navbar component that is sticky at the top of the page, and uses backdrop-blur
to blur the content that scrolls underneath it:
That's with this configuration:
<div className="bg-slate-100">
<header className="sticky top-0 left-0 z-[1100] w-full backdrop-blur">
<div className="mx-auto">
<div className="py-4 px-8 mx-4">
<nav>
<a href="/">
<div>My Site</div>
</a>
<ul className="flex justify-between ml-auto">
<li>Products</li>
<li>
<a
className="bg-emerald-700 hover:bg-emerald-600 text-white py-2 px-4 rounded-sm shadow-lg shadow-emerald-800"
href="/"
>
About
</a>
</li>
</ul>
</nav>
</div>
</div>
</header>
</div>
The problem I have is, it works great when the underlying content is light; however, when it's dark, the black text in the navbar become unreadable:
I've been fiddling for hours with various combinations of mix-blend
on both the header
component itself and the lowest level descendants (e.g., the "My Site" div
) to no avail.
There's a pretty similar question here, but it was asked and answered with JavaScript (in fact, there are several others in that same category). But this seems like something that should be doable without JS.
I've made some progress by following this answer, which clarifies that you need to set the mix-blend-mode
on the parent element (the header
in this case), and then change the color in the lowest level descendent.
This almost works. If I set it to mix-blend-difference bg-black
on the header, and explicitly set the text to text-white
, it is serviceable on the dark backgrounds:
I would prefer if the text were white, instead of the yellow, which I guess happens from differencing against the slate lowest z-index background color, but could live with this. However, there are two remaining problems: when over a light background, the green around the button becomes pink:
and the blur effect stops working:
This last state is available for fiddling in a Codesandbox demo.
How can I get the text to go to a contrasting color based on the underlying colors, while simultaneously keeping the button colors the whole time, and keeping the blur effect?