Search code examples
htmlcsscss-filtersmix-blend-mode

Why can't I apply css filters after mix-blend-mode?


I have some html text and would like to apply some css to do the following to the pixels of the text:

  1. Apply the Difference Blend Mode with the pixels underneath.
  2. Apply a Greyscale Filter.
  3. Apply a Contrast Filter with a very high setting.

This would allow to have UI text elements be positioned over images while still being fairly readable.

I made different attempts, but remained unsuccessful. Here is one where I tried wrapping the text in a container. The blend mode is applied to the actual text element, while the filters are applied to the wrapper:

/* Filters and Blend Mode */

.h1Wrapper {
  filter: grayscale(1) contrast(200);
}

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

/* SETUP */

.container {
  width: 600px;
  height: 200px;
  position: relative;
}

img {
  margin: auto;
  display: block;
  max-width: 100%;
  max-height: 100%;
}

.h1Wrapper {
  position: absolute;
  top: 0px;
  width: 100%;
  height: 100%;
}

h1 {
  text-align: center;
  position: absolute;
  top: 0px;
  width: 100%;
}
<div class="container">

  <img src="https://upload.wikimedia.org/wikipedia/commons/f/f9/Phoenicopterus_ruber_in_S%C3%A3o_Paulo_Zoo.jpg">

  <div class="h1Wrapper">
    <h1>hello there, good morning, booboo sousa</h1>
  </div>

</div>

However, if you run this you can see it did not work. In this case the blend mode seems to be ignored.

When I tried to put the filter and blend mode css both on the h1 element, the filters where both applied before the blend mode, which lead to a different effect that I am trying to achieve.

For clarity, here is a photoshop version of the effect I am trying to achieve:

Photoshop version of the effect I am trying to achieve Layers of the Photoshop version


Solution

  • I managed to solve this issue by stacking 3 layers of text over each other.

    .container {
      text-align: center;
      position: relative;
      z-index: 100;
    }
    
    .split-text-wrap {
      position: relative;
      display: inline-block;
      margin: 0 auto;
    }
    
    .split-text {
      mix-blend-mode: color;
      color: #000;
    }
    
    .split-text1 {
      top: 0;
      position: absolute;
      mix-blend-mode: overlay;
      color: #fff;
    }
    
    .split-text2 {
      top: 0;
      position: absolute;
      color: #fff;
      opacity: .37;
    }
    <div class="container">
      <div class="split-text-wrap">
        <h2 class="split-text">
          Popuplar searches
        </h2>
        <h2 class="split-text1">
          Popuplar searches
        </h2>
        <h2 class="split-text2">
          Popuplar searches
        </h2>
      </div>
    </div>

    1st layer mixed using the colour 2nd layer made grey parts white 3rd layer changed blacks into greys

    This was the result

    I hope it's going to help.