CSS background-blend-mode multiply with opacity - css

I'm porting a UI-based game from Unity to Cordova. In Unity, I was tinting predominately white images with various colors to reuse assets. The rough CSS equivalent seems to be using the [mostly standard] background-blend-mode property set to multiply and have the image in the background with the desired tint color as the bg color.
.tinted {
background-image: url('theimg.png');
background-size: 100% 100%;
background-color: #0f0;
background-blend-mode: multiply;
}
The problem is that it doesn't preserve the opacity of the image, namely the transparent parts become the tint color. The spec says something about blending from the top down so I thought it might relate to blending with the bg color, but it doesn't work if I layer a solid color (as a gradient) on top of the image either.
.tinted2 {
background-image: url('theimg.png'), linear-gradient(to bottom, #0f0, #0f0);
background-size: 100% 100%;
background-blend-mode: multiply;
}
Reversing the order of the background images or changing the blend mode to normal, multiply or multiply, normal doesn't work either. Any suggestions on how to do this correctly using CSS?
EDIT: As the answer mentions, the alpha aspect can be achieved using the mask property. I used a combination of the two techniques to get what I needed:
.tintedMasked {
background-image: url('theimg.png');
background-size: 100% 100%;
mask-image: url('theimg.png');
mask-size: 100% 100%;
background-color: #0f0;
background-blend-mode: multiply;
}

If I understand correctly what you're trying to do, then background blending is not the way, but masking.
div {
height: 200px;
background-image:linear-gradient(SlateBlue, Tomato);
-webkit-mask: url(https://cdn.pixabay.com/photo/2016/12/28/19/37/denied-1936877_960_720.png) top left no-repeat / contain;
mask: url(https://cdn.pixabay.com/photo/2016/12/28/19/37/denied-1936877_960_720.png) top left no-repeat / contain;
}
}
<div></div>
<h1>No stairway??</h1>
That's assuming your mask images are alpha transparent PNGs. you could also use luminance mask by setting mask-mode: luminance;

Related

Using a Jpeg to Mask another Image with pure CSS?

I need to use the black pixels of a Jpeg image as a "transparency layer" to mask another image.
When I use this PNG image as a mask, it works fine:
(it's a white square in an alpha background)
But I need to do the same with a Jpeg image, replacing the alphachannel with black ! (so without alpha channel).
Here's my CSS:
#selection-image {
width: 1000px;
height: 800px;
position: absolute;
-webkit-mask-image: urlhttps://image.noelshack.com/fichiers/2019/14/4/1554391365-output-onlinepngtools-2.png);
-webkit-mask-size: 1000px 800px;
mask-image: url(https://image.noelshack.com/fichiers/2019/14/4/1554391365-output-onlinepngtools-2.png);
mask-size: 1000px 800px;
mask-type: alpha;
background: red;
}
I cannot use canvas to delete the black pixel, as it takes too much time. I thought I could use the property mask-type: luminance, but it seems to work only with alpha channel too ..

How to optimize CSS background-image and background-position properties

I'm following a tutorial on front-end development, and I just wrote this ruleset for a header image:
header {
background-image: linear-gradient(#00000070, #00000070), url("../img/hero.jpg");
background-position: center;
background-size: cover;
height: 100vh;
}
PyCharm tells me the background-image and background-position properties can be optimized by transforming them to a shorthand form, but it doesn't provide specific advice on how. As I'm a beginner to CSS, I can't infer the problem, and w3schools.com didn't offer any shorthand. How else can I write this block to shorten it?
As defined by the W3C there should be a slash separating the backgound-size from the background-position and an comma to separate the image from your gradient:
background:
url("../img/hero.jpg") /* image */
center / cover, /* position / size */
linear-gradient(red, green) /* gradient */
;
In one line without comments:
background: url("../img/hero.jpg") center / cover, linear-gradient(red, green);
Try this
header {
background: linear-gradient(#00000070, #00000070), url("../img/hero.jpg"), center, cover}

Blend mode in Chrome not applying properly

Was working towards a repeated background that has a spotlight and decided to use blend modes to achieve this.
However, it appears as if blend modes did not apply in Chrome.
Replicated here: http://jsfiddle.net/pptn4f5v/7/
body {
background: url("https://dl.dropboxusercontent.com/u/10686242/testfreeimage.jpg"), url("https://dl.dropboxusercontent.com/u/10686242/background-blend%20-%20Copy.png") #030303;
background-repeat: repeat, no-repeat;
background-blend-mode: multiply, normal;
background-size: auto, contain;
}
Is this a limitation of Chrome? This works perfect in Firefox.
Probably it is a bug in Chrome
However, you can get this effect more easily, and it will work ok in both browsers
Use only 2 backgrounds, and create the spot with a gradient
.test {
background: url("https://dl.dropboxusercontent.com/u/10686242/testfreeimage.jpg"),
radial-gradient(circle at 250px 100px, transparent 50px, #606060 150px);
background-blend-mode: darken;
height: 400px;
}
<div class="test"></div>

background gradient with solid color

I have to do the following:
The top of the div is an image of a gradient, then in the bottom it continues as a solid color. Can I do this with simple CSS? I know the following is invalid.
{background: url(img/right_column_bg_top.png) no-repeat rgba(10,26,39,1) top 225px;
Note: the first 225px, which the image fills, should be without the background-color
As far as I know, you need to use a gradient for the solid color, so that you can set it correctly.
The CSS would be:
.imgbg {
width:255px;
height:355px;
background: url('http://blue2.hu/danone/nogravity/img/right_column_bg_top.png'), linear-gradient(90deg, #f7d8e8, #f7d8e8);
background-position: 0px 0px, 0px 112px;
background-repeat: no-repeat, no-repeat;
background-size: 255px 112px, 255px 233px;
}
Here is your updated fiddle
Basic suport should be fine for browsers supporting multiple backgrounds, the only problem would be with IE <= 8. Gradient background could be a problem with IE9, but I think that it should work (I can not test IE9). If it would be really a problem, see colozilla for a fix.
Check out this fiddle and tell me if this is what you want.
FIDDLE
HTML
<div class="imgbg"></div>
CSS
.imgbg {
width:255px;
height:355px;
background:#f7d8e8 url('http://placehold.it/255x255') no-repeat;
}
I would do the following:
#myDiv { background: #f7d8e8 url('/img/right_column_bg_top.png') repeat-x ; }
This will just put your background image on the top of the div; the rest of it, will be the color you selected for the entire background of the div.

Background images: how to fill whole div if image is small and vice versa

I have three problems:
When I tried to use a background image in a smaller size div, the div shows only part of image. How can I show the full or a specific part of image?
I have a smaller image and I want to use in a bigger div. But don't want to use repeat function.
Is there any way in CSS to manipulate the opacity of an image?
Resize the image to fit the div size.
With CSS3 you can do this:
/* with CSS 3 */
#yourdiv {
background: url('bgimage.jpg') no-repeat;
background-size: 100%;
}
How Do you Stretch a Background Image in a Web Page:
About opacity
#yourdiv {
opacity: 0.4;
filter: alpha(opacity=40); /* For IE8 and earlier */
}
Or look at CSS Image Opacity / Transparency
To automatically enlarge the image and cover the entire div section without leaving any part of it unfilled, use:
background-size: cover;
This worked perfectly for me
background-repeat: no-repeat;
background-size: 100% 100%;
Try below code segment, I've tried it myself before :
#your-div {
background: url("your-image-link") no-repeat;
background-size: cover;
background-clip: border-box;
}
Rather than giving background-size:100%;
We can give background-size:contain;
Check out this for different options avaliable: http://www.css3.info/preview/background-size/
This will work like a charm.
background-image:url("http://assets.toptal.io/uploads/blog/category/logo/4/php.png");
background-repeat: no-repeat;
background-size: contain;
I agree with yossi's example, stretch the image to fit the div but in a slightly different way (without background-image as this is a little inflexible in css 2.1). Show full image:
<div id="yourdiv">
<img id="theimage" src="image.jpg" alt="" />
</div>
#yourdiv img {
width:100%;
/*height will be automatic to remain aspect ratio*/
}
Show part of the image using background-position:
#yourdiv
{
background-image: url(image.jpg);
background-repeat: no-repeat;
background-position: 10px 25px;
}
Same as the first part of (1) the image will scale to the div so bigger or smaller will both work
Same as yossi's.

Resources