For example, it has an image with a transparent background:
And I would like to add an gray overlay (for example, a div, or canvas...) on it:
But I want the overlay ignores transparent area on an image automatically:
Can pure HTML and CSS (for example, by div, canvas...) do this?
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.
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.
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.
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With