Change color of PNG image via CSS?

后端 未结 16 1613
自闭症患者
自闭症患者 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

提交回复
热议问题