Change color of PNG image via CSS?

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

    Think I have a solution for this that's a) exactly what you were looking for 5 years ago, and b) is a bit simpler than the other code options here.

    With any white png (eg, white icon on transparent background), you can add an ::after selector to recolor.

    .icon {
        background: url(img/icon.png); /* Your icon */
        position: relative; /* Allows an absolute positioned psuedo element */
    }
    
    .icon::after{
        position: absolute; /* Positions psuedo element relative to .icon */
        width: 100%; /* Same dimensions as .icon */
        height: 100%;
        content: ""; /* Allows psuedo element to show */
        background: #EC008C; /* The color you want the icon to change to */
        mix-blend-mode: multiply; /* Only apply color on top of white, use screen if icon is black */
    }
    

    See this codepen (applying the color swap on hover): http://codepen.io/chrscblls/pen/bwAXZO

提交回复
热议问题