Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CSS combination of mix-blend-mode and isolation?

I have a parent element with a red background. I want an h2 element to blend just some words with the background, and other words, inside a span tag, no.

My example below is not working.
How to make it work?

.bg-red {
  background: red;
}

.blend {
  mix-blend-mode: difference;
  color: white;
}

.isolate {
  isolation: isolate;
}
<div class="bg-red">
  <div class="blend">
    <h2>This text should <span class="isolate">(This one shouldn't)</span> be blended 
    </h2>
  </div>
</div>

View on JSFiddle

like image 488
sigmaxf Avatar asked Apr 12 '18 22:04

sigmaxf


People also ask

What is the difference between Mix blend-mode difference and mix blend-mode exclusion?

difference : this subtracts the darker of the two colors from the lightest color. exclusion : similar to difference but with lower contrast. hue : creates a color with the hue of the content combined with the saturation and luminosity of the background.

What is CSS mix blend-mode?

The mix-blend-mode CSS property sets how an element's content should blend with the content of the element's parent and the element's background.

Why does mix blend-mode not work?

The reason why it doesn't work is that the white sections don't really have a white background. They have no background-color set, so they fall back to the default value of transparent . Visually this is manifested as a white color, but to the compositor it will still be transparent .

Does CSS have blend modes?

You can use most of the blend modes available in a design tool with CSS, using the mix-blend-mode or the background-blend-mode properties. The mix-blend-mode applies blending to a whole element and the background-blend-mode applies blending to the background of an element.


1 Answers

In order to make it working you need to specify the isolation before applying the mix-blend-mode. So between the background and the text on where you will apply mix-blend-mode you need to have a wrapper where isolation is set.

Here is an example where the background is applied on the h2 and inside we have many span. We apply mix-blend-mode on them and we wrap the non-needed one with another wrapper where we apply isolation:

h2 {
  background: red;
}

h2 span {
  mix-blend-mode: difference;
  color: white;
}

.isolate {
  isolation: isolate;
  display: inline-block;
}
<h2>
  <span>This text should</span>
  <div class="isolate"><span>(This one shouldn't)</span></div>
  <span> be blended</span>
</h2>

But if you apply mix-blend-mode on the h2 you won't be able to isolate any of its content:

.red {
  background: red;
}

h2 {
  mix-blend-mode: difference;
}

h2 span {
  color: white;
}

.isolate {
  isolation: isolate;
  display: inline-block;
}
<div class="red">
  <h2>
    <span>This text should</span>
    <div class="isolate"><span>(This one shouldn't)</span></div>
    <span> be blended</span>
  </h2>
</div>
like image 181
Temani Afif Avatar answered Sep 20 '22 03:09

Temani Afif