Change text color to white on any non-white background

Here is an idea that rely on background coloration and not mix-blend-mode. The trick is to have a gradient with the same dimension as the image that you move the same way to simulate the blend mode:

const box = document.querySelector(".box");
const h1 = document.querySelector("h1");
window.addEventListener('mousemove', function(event) { = `${event.pageX - 50}px`; = `${event.pageY - 50}px`; = `${event.pageX - 50}px ${event.pageY - 50}px`;
.wrapper {
  background-color: white;
h1 {
  position: relative;
  z-index: 2;
  color: white;
    /*gradient                   position   /    size  */
    linear-gradient(#fff,#fff) -100px -100px/100px 100px fixed no-repeat,
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;

.box {
  width: 100px;
  height: 100px;
  position: absolute;
  z-index: 1;
  background-image: url("")
<div class="wrapper">
  <h1>Lorem Ipsum</h1>
<div class="box"></div>

I have considered background-attachment:fixed to place the gradient relatively to the viewport since your element is position:absolute with no ancestor positioned so it’s also positioned relatively to the viewport.

Leave a Comment