Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can HTML and CSS only create an overlay which ignores transparent area on an image automatically?

Tags:

For example, it has an image with a transparent background:

Enter image description here

And I would like to add an gray overlay (for example, a div, or canvas...) on it:

Enter image description here

But I want the overlay ignores transparent area on an image automatically:

Enter image description here

Can pure HTML and CSS (for example, by div, canvas...) do this?

like image 246
ggrr Avatar asked Dec 16 '16 09:12

ggrr


People also ask

What is an overlay in CSS and HTML?

Overlay means to cover the surface of something with a coating. In other words, it is used to set one thing on the top of another. The overlay makes a web-page attractive, and it is easy to design.

Does HTML support transparent images?

Transparency is not done in HTML, but is a part of the image itself. The browser will see the image as a PNG and display it as a PNG automatically. To add transparency to the image, you will have to edit the file with a graphics editor like Photoshop.

How do I make an image transparent in HTML CSS?

First, we create a <div> element (class="background") with a background image, and a border. Then we create another <div> (class="transbox") inside the first <div>. The <div class="transbox"> have a background color, and a border - the div is transparent.

How do you put an overlay effect on an image in CSS?

In short, CSS overlay effects are achieved by using the following: background-image and background CSS properties to add image and linear-gradient overlay effect. position:absolute , top , bottom , right , left CSS properties to control the position of overlay image or text.


2 Answers

Try the overlay blend-mode. Or multiply! (Not supported by IE or Edge, thanks @Stilltorik)

.multiplied {    background-color: blue;    width:250px;    height: 100px;    margin-top: -100px;    mix-blend-mode: overlay;  }
<div>    <img src='https://i.stack.imgur.com/4yUEW.png'/>    <div class='multiplied'></div>  </div>
like image 86
Carele Avatar answered Sep 21 '22 13:09

Carele


A somewhat hacky way:

.alpha-mask {    mask-image: url(https://i.stack.imgur.com/FwTzE.png);    -webkit-mask-image: url(https://i.stack.imgur.com/FwTzE.png);    mask-mode: alpha;    -webkit-mask-mode: alpha;    mask-repeat: no-repeat;    -webkit-mask-repeat: no-repeat;  }    .overlay {    background-color: #000;    position: absolute;    top: 120px;    left: 0;    width: 100%;    height: 100%;    opacity: 0.2;  }
<article class="alpha-mask">    <img class="alpha-target" src="https://i.stack.imgur.com/FwTzE.png" />    <div class="overlay alpha-target">    </div>  </article>

See it working.

like image 45
Ivan Chaer Avatar answered Sep 25 '22 13:09

Ivan Chaer