Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <div class="inner">
    <h2>Blending in HTML &amp; CSS</h2>
    <p>
      On the graphicdesign stackexchange (GDSE for friends) someone posted a question about blend modes, saying <b>"Imagine If I use it for web and I can't use blending modes"</b>.<br>
      This seemed like a silly thing to say, so I posted <a href="https://graphicdesign.stackexchange.com/a/91198/41100" target="blank">this answer</a>. Then I thought, why not actually make this answer into a pen to illustrate the different approaches, so here that pen is.
    </p>
  </div>
</div>
<div class="container case0">
  <div class="inner">
    <h1>0. The original</h1>
    <p>The image produced in Photoshop using blend-mode 'multiply'</p>
    <img src="https://i.imgsafe.org/455a7e4d7a.jpg">
  </div>
</div>
<div class="container case1">
  <div class="inner">
    <h1>1. Divs positioned on top of each other</h1>
    <p>A PNG of the text, positioned on top of the background with negative <code>margin-top</code><br>(one could also use <code>position: absolute</code> or <code>transform: translateY(-100%)</code> in a wrapper).</p>
    <img class="bg" src="https://i.imgsafe.org/4559524a27.jpg">
    <img class="overlay" src="https://i.imgsafe.org/45770ec198.png">
  </div>
</div>
<div class="container case2">
  <div class="inner">
    <h1>2. Stacked background images</h1>
    <p>A PNG of the text on top of the background, both in <code>background-image</code>.</p>
    <div class="blend"></div>
  </div>
</div>
<div class="container case3">
  <div class="inner">
    <h1>3. Stacked divs + filters</h1>
    <p>A PNG of the text, positioned on top of the background with negative <code>margin-top</code><br>(one could also use <code>position: absolute</code> or <code>transform: translateY(-100%)</code>).</p>
    <img class="bg" src="https://i.imgsafe.org/4559524a27.jpg">
    <img class="overlay" src="https://i.imgsafe.org/4629d2f1a1.png">
  </div>
</div>
<div class="container case4">
  <div class="inner">
    <h1>4. Pseudo elements</h1>
    <p>A PNG of the text, positioned on top of the background with a <code>pseudo-element</code>. This technique has the advantage that one has more control over the positioning of the text.</p>
    <div class="blend"></div>
  </div>
</div>
<div class="container case5">
  <div class="inner">
    <h1>5. Background blend mode</h1>
    <p>Two images, both set as background image, blended using <code>background-blend-mode: multiply</code>. Results are much closer to the original Photoshop image.</p>
    <div class="blend"></div>
  </div>
</div>
<div class="container case6">
  <div class="inner">
    <h1>6. Mix blend mode</h1>
    <p>Two PNG images, positioned on top of the other blended using <code>mix-blend-mode: multiply</code>. My favourite by far, browser support is <a href="http://caniuse.com/#feat=css-mixblendmode">OK-ish</a> apart from IE and Edge.</p>
    <img class="bg" src="https://i.imgsafe.org/4559524a27.jpg">
    <img class="overlay" src="https://i.imgsafe.org/4629d2f1a1.png">
  </div>
</div>
<div class="container case7">
  <div class="inner">
    <h1>7. Mix blend mode</h1>
    <p>Live text positioned on top of a PNG, blended using <code>mix-blend-mode: multiply</code>. The favourite, with added bonus of editable text.</p>
    <img class="bg" src="https://i.imgsafe.org/4559524a27.jpg">
    <div class="overlay" contenteditable>Rural</div>
  </div>
</div>
<div class="container case8">
  <div class="inner">
    <h1>8. SVG filter</h1>
    <p>A PNG of the text on top of the background loaded through SVG, blended with <code>&lt;feBlend mode="multiply"&gt;</code>. Works pretty well, has <a href="http://caniuse.com/#search=svg%20filter">much better browser support</a> than <code>mix-blend-mode</code>.<br>I could not for the life of me get the images positioned correctly though. But as long as the blending result is clear, I'm not going to invest too much time into that.</p>
    <svg preserveAspectRatio="xMidYMid">
      <defs>
        <filter id="blendNorm" x="0" y="0" filterUnits="userSpaceOnUse">
          <feImage x="0" y="0" xlink:href="https://i.imgsafe.org/4559524a27.jpg" result="img1" />
          <feImage x="0" y="0" xlink:href="https://i.imgsafe.org/4629d2f1a1.png" result="img2" />
          <feBlend mode="multiply" in="img2" in2="img1"/>
        </filter>
      </defs>
      <g filter="url(#blendNorm)" transform="translate(0)"></g>
    </svg>
  </div>

</div>
<div class="container case9">
  <div class="inner">
    <h1>9. Canvas</h1>
    <p>A PNG of the text on top of the background loaded through SVG, blended with <code>ctx.globalCompositeOperation = 'multiply'</code>. Has better <a href="http://caniuse.com/#feat=canvas-blending">support</a> than <code>mix-blend-mode</code>, but worse than SVG.<br>Haven't bothered with step-wise loading to remove the jagged edges as the blending result is clear enough, reference <a href="http://stackoverflow.com/a/17862644/2895454" target="_blank">here</a> for more info.</p>
    <canvas id="canvas-case9"></canvas>
    <script type="text/javascript">
      var canvas = document.getElementById('canvas-case9');
			var ctx = canvas.getContext('2d');
			ctx.globalCompositeOperation = 'multiply';
      ctx.imageSmoothingEnabled = true;
      
      function drawImageScaled(img, ctx) {
         var canvas = ctx.canvas;
         var hRatio = canvas.width  / img.width;
         var vRatio =  canvas.height / img.height;
         var ratio  = Math.min(hRatio, vRatio);
         ctx.drawImage(img, 0,0, img.width, img.height,0,0,img.width*ratio, img.height*ratio);  
      }
      
      var img = new Image();
      img.src = 'https://i.imgsafe.org/4559524a27.jpg';
      img.onload= drawImageScaled.bind(null, img, ctx);
      
      var img2 = new Image();
      img2.src = 'https://i.imgsafe.org/4629d2f1a1.png';
      img2.onload= drawImageScaled.bind(null, img2, ctx);
    </script>
  </div>
</div>
              
            
!

CSS

              
                $font: 'Josefin Sans', sans-serif;
$imgheight: 30vw;
$red: #ff0000;

div{
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  overflow: hidden;
  &:before, &:after{
    content: '';
    position: absolute;
    display: block;
    box-sizing: border-box;
  }
}

body{
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  font-family: $font;
  font-size: 2vw;
  line-height: 1.5em;
  color: white;
  text-align: center;
  background: #333;
  overflow-x: hidden;
  
  & .container{
    position: relative;
    width: 100vw;
    padding: 2vw 1vw;
    text-align: center;
    border-top: 1px solid #444;
    
    & .inner{
      position: relative;
      left: 0;
      right: 0;
      width: 90%;
      height: auto;
      margin: 0 auto;
      
      & h1, & h2, & p{
        width: 100%;
      }

      & h2{
        font-size: 6vw;
        color: $red;
      }

      & code{
        font-size: 0.85em;
        background:#444;
      }

      & a{
        color:inherit;
        text-decoration: none;
        border-bottom: 1px solid rgba(255,255,255,0.15);
        transition: border 1s ease;
        &:hover{
          border-bottom: 1px solid rgba(255,255,255,0.75);
        }
      }
      
      & img, & .blend{
        position: relative;
        width: auto;
        height: $imgheight;
        background-position: center center;
        background-size: contain;
        background-repeat: no-repeat;
      }
      
    }
    
  }
  
  & .case1{
    & .overlay{
      margin-top: -$imgheight;
    }
  }
  
  & .case2{
    & .blend{
      background-image: url(https://i.imgsafe.org/45770ec198.png), url(https://i.imgsafe.org/4559524a27.jpg);
    }
  }
  
  & .case3{
    & .overlay{
      margin-top: -$imgheight;
      opacity: 0.7;
      filter: brightness(0.6);
    }
  }
  
  & .case4{
    & .blend{
      background-image: url(https://i.imgsafe.org/4559524a27.jpg);
      &:after{
        left: 0;
        right: 0;
        margin: 0 auto;
        width: 93%;
        height:100%;
        background: url(https://i.imgsafe.org/45770ec198.png) center /contain no-repeat;
      }
    }
  }
  
  & .case5{
    & .blend{
      background-image: url(https://i.imgsafe.org/4629d2f1a1.png), url(https://i.imgsafe.org/4559524a27.jpg);
      background-blend-mode: multiply;
    }
  }
  
  & .case6{
    & .overlay{
      margin-top: -$imgheight;
      mix-blend-mode: multiply;
    }
  }
  
  & .case7{
    & .overlay{
      height: $imgheight;
      font-size: $imgheight * 0.8;
      color: $red;
      margin-top: -$imgheight;
      mix-blend-mode: multiply;
      line-height: 1em;
    }
  }
  
  & .case8{
    & svg{
      height: $imgheight;
      width: $imgheight * 2.5;
    }
  }
  
  & .case9{
    & canvas{
      height: $imgheight;
      width: $imgheight * 2;
    }
  }
  
}
              
            
!

JS

              
                /*
This Codepen is in the Public Domain.
You can use it for whatever purpose you like, except evil.
*/
              
            
!
999px

Console