Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text color to white on any non-white background

Changing the text color to white when the background color is black works great using mix-blend-mode: difference. Move the mouse to the text to see the effect:

const blackBox = document.querySelector(".black-box");
window.addEventListener('mousemove', function(event) {
  blackBox.style.left = `${event.pageX - 50}px`;
  blackBox.style.top = `${event.pageY - 50}px`;
});
.wrapper {
  background-color: white;
}
h1 {
  position: relative;
  z-index: 2;
  color: white;
  mix-blend-mode: difference;
}

.black-box {
  width: 100px;
  height: 100px;
  position: absolute;
  z-index: 1;
  background-color: black;
}
<div class="wrapper">
  <h1>Lorem Ipsum</h1>
</div>
<div class="black-box"></div>

This understandably doesn't result in white text if the background is anything other than black:

const box = document.querySelector(".box");
window.addEventListener('mousemove', function(event) {
  box.style.left = `${event.pageX - 50}px`;
  box.style.top = `${event.pageY - 50}px`;
});
.wrapper {
  background-color: white;
}
h1 {
  position: relative;
  z-index: 2;
  color: white;
  mix-blend-mode: difference;
}

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

Is there any way to make it so the text changes color from black to white as soon as the background differs from white?

like image 688
Tholle Avatar asked Mar 29 '19 20:03

Tholle


People also ask

Is it easier to read white text on a black background?

While white text on a black background provides very high value contrast, it is less readable and causes greater eye fatigue than black text on a white background. All light-colored text on dark backgrounds causes eye fatigue.

How do you get a white background on black text?

Select the paragraphs where you want to make the change. (Highlight all the text.) Choose Borders and Shading on the Format menu, click the Shading tab at the top of the dialog box, and set the paragraph shading to black. Choose Font on the Format menu and set the Font to white.

What color text looks good on black background?

Black backgrounds: High-chroma colors are best for black backgrounds because they offer a good contrast ratio without the eye fatigue (which occurs with white text on a black background).


1 Answers

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) {
  box.style.left = `${event.pageX - 50}px`;
  box.style.top = `${event.pageY - 50}px`;
  
  h1.style.backgroundPosition = `${event.pageX - 50}px ${event.pageY - 50}px`;
});
.wrapper {
  background-color: white;
}
h1 {
  position: relative;
  z-index: 2;
  color: white;
  background: 
    /*gradient                   position   /    size  */
    linear-gradient(#fff,#fff) -100px -100px/100px 100px fixed no-repeat,
    #000;
  -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("https://placekitten.com/100/100")
}
<div class="wrapper">
  <h1>Lorem Ipsum</h1>
</div>
<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.

like image 116
Temani Afif Avatar answered Nov 15 '22 11:11

Temani Afif