Change color of PNG image via CSS?

后端 未结 16 1387
自闭症患者
自闭症患者 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 07:58

    When changing a picture from black to white, or white to black the hue rotate filter does not work, because black and white are not technically colors. Instead, black and white color changes (from black to white or vice-versa) must be done with the invert filter property.

    .img1 { filter: invert(100%); }

    0 讨论(0)
  • 2020-11-22 07:59

    I found this while googling, I found best working for me...

    HTML

    <div class="img"></div>
    

    CSS

    .img {
      background-color: red;
      width: 60px;
      height: 60px;
       -webkit-mask-image: url('http://i.stack.imgur.com/gZvK4.png');
    }
    

    http://jsfiddle.net/a63b0exm/

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

    The img tag has a background property just like any other. If you have a white PNG with a transparent shape, like a stencil, then you can do this:

    <img src= 'stencil.png' style= 'background-color: red'>
    
    0 讨论(0)
  • 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

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

    The simplest one line that worked for me:

    filter: opacity(0.5) drop-shadow(0 0 0 blue);
    

    You can adjust opacity from 0 to 1 to make color lighter or darker.

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

    I required a specific colour, so filter didn't work for me.

    Instead, I created a div, exploiting CSS multiple background images and the linear-gradient function (which creates an image itself). If you use the overlay blend mode, your actual image will be blended with the generated "gradient" image containing your desired colour (here, #BADA55)

    .colored-image {
            background-image: linear-gradient(to right, #BADA55, #BADA55), url("https://i.imgur.com/lYXT8R6.png");
            background-blend-mode: overlay;
            background-size: contain;
            width: 200px;
            height: 200px;        
        }
    <div class="colored-image"></div>

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