Search code examples
cssbackground-imagebackground-colormix-blend-mode

Exclude some part from background blend mode


I try to achieve effect similar to Photoshop multiply via code on my project. At the same time i try to exclude some elements that are in child div to be affected by it (like text layers) - and i hit in the wall. Tried already adding additional div's setting different z-index's or go with absolute position.

Here you can find pen with example of the problem:

HTML

<div class="main">
  <div class="inner">
    <h1>Some header</h1>
    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quia eligendi est eius unde autem dolore adipisci perspiciatis laboriosam reiciendis placeat! Eveniet, quam? Vitae sit saepe quam delectus fugiat, dolores necessitatibus.</p>
  </div>
</div>

CSS

.inner {
  text-align: center;
  padding: 50px 0;
  background: #0079ff;
     mix-blend-mode: multiply;
    backface-visibility: hidden;
  color: white;
}

.main {
  background-repeat: no-repeat;
  background-image: url('https://preview.ibb.co/fMY2f9/Bg1.jpg');
  background-size: cover;
}

Summary: Child element's of 'child div' are affected by multiply. Is there a way to prevent that?

Screenshots:

Multiply has been applied but it also affect text layers.

enter image description here


Solution

  • Use pseudo element for the background to avoid this:

    .inner {
      text-align: center;
      padding: 50px 0;
      color: white;
    }
    
    .main {
      background-repeat: no-repeat;
      background-image: url('https://preview.ibb.co/fMY2f9/Bg1.jpg');
      background-size: cover;
      position: relative;
      z-index: 0;
    }
    
    .main::before {
      content: "";
      position: absolute;
      z-index: -1;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: #0079ff;
      mix-blend-mode: multiply;
    }
    <div class="main">
      <div class="inner">
        <h1>Some header</h1>
        <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quia eligendi est eius unde autem dolore adipisci perspiciatis laboriosam reiciendis placeat! Eveniet, quam? Vitae sit saepe quam delectus fugiat, dolores necessitatibus.</p>
      </div>
    </div>