Change color of PNG image via CSS?

后端 未结 16 1386
自闭症患者
自闭症患者 2020-11-22 07:33

Given a transparent PNG displaying a simple shape in white, is it possible to somehow change the color of this through CSS? Some kind of overlay or what not?

相关标签:
16条回答
  • 2020-11-22 08:04

    Answering because I was looking for a solution for this.

    the pen in @chrscblls answer works well if you have a white or black background, but mine wasn't. Aslo, the images were generated with ng-repeat, so I couldn't have their url in my css AND you can't use ::after on img tags.

    So, I figured a work around and thought it might help people if they too stumble here.

    So what I did is pretty much the same with three main differences:

    • the url being in my img tag, I put it(and a label) in another div on which ::after will work.
    • the 'mix-blend-mode' is set at 'difference' instead of 'multiply' or 'screen'.
    • I added a ::before with exactly the same value so the ::after would do the 'difference' of the 'difference' made by the ::before and cancelled it-self.

    To change it from black to white or white to black the background color need to be white. From black to colors, you can choose whatever color. From white to colors tho, you'll need to choose the opposite color of the one you want.

    .divClass{
       position: relative;
       width: 100%;
       height: 100%;
       text-align: left;
    }
    .divClass:hover::after, .divClass:hover::before{
       position: absolute;
       width: 100%;
       height: 100%;
       background: #FFF;
       mix-blend-mode: difference;
       content: "";
    }
    

    https://codepen.io/spaceplant/pen/oZyMYG

    0 讨论(0)
  • 2020-11-22 08:06

    There's no need for a whole font set if you only need one icon, plus I feel it being more "clean" as an individual element. So, for this purpose, in HTML5 you can place a SVG directly inside the document flow. Then you can define a class in your .CSS stylesheet and access its background color with the fill property:

    Working fiddle: http://jsfiddle.net/qmsj0ez1/

    Note that, in the example, I've used :hoverto illustrate the behaviour; if you just want to change color for the "normal" state, you should remove the pseudoclass.

    0 讨论(0)
  • 2020-11-22 08:09

    Yes :)

    Surfin' Safari - Blog Archive » CSS Masks

    WebKit now supports alpha masks in CSS. Masks allow you to overlay the content of a box with a pattern that can be used to knock out portions of that box in the final display. In other words, you can clip to complex shapes based off the alpha of an image.
    [...]
    We have introduced new properties to provide Web designers with a lot of control over these masks and how they are applied. The new properties are analogous to the background and border-image properties that already exist.

    -webkit-mask (background)
    -webkit-mask-attachment (background-attachment)
    -webkit-mask-clip (background-clip)
    -webkit-mask-origin (background-origin)
    -webkit-mask-image (background-image)
    -webkit-mask-repeat (background-repeat)
    -webkit-mask-composite (background-composite)
    -webkit-mask-box-image (border-image)
    
    0 讨论(0)
  • 2020-11-22 08:12

    In most browsers, you can use filters :

    • on both <img> elements and background images of other elements

    • and set them either statically in your CSS, or dynamically using JavaScript

    See demos below.


    <img> elements

    You can apply this technique to a <img> element :

    #original, #changed {
        width: 45%;
        padding: 2.5%;
        float: left;
    }
    
    #changed {
        -webkit-filter : hue-rotate(180deg);
        filter : hue-rotate(180deg);
    }
    <img id="original" src="http://i.stack.imgur.com/rfar2.jpg" />
    
    <img id="changed" src="http://i.stack.imgur.com/rfar2.jpg" />

    Background images

    You can apply this technique to a background image :

    #original, #changed {
        background: url('http://i.stack.imgur.com/kaKzj.jpg');
        background-size: cover;
        width: 30%;
        margin: 0 10% 0 10%;
        padding-bottom: 28%;
        float: left;
    }
    
    #changed {
        -webkit-filter : hue-rotate(180deg);
        filter : hue-rotate(180deg);
    }
    <div id="original"></div>
    
    <div id="changed"></div>

    JavaScript

    You can use JavaScript to set a filter at runtime :

    var element = document.getElementById("changed");
    var filter = 'hue-rotate(120deg) saturate(2.4)';
    element.style['-webkit-filter'] = filter;
    element.style['filter'] = filter;
    #original, #changed {
        margin: 0 10%;
        width: 30%;
        float: left;
        background: url('http://i.stack.imgur.com/856IQ.png');
        background-size: cover;
        padding-bottom: 25%;
    }
    <div id="original"></div>
    
    <div id="changed"></div>

    0 讨论(0)
提交回复
热议问题