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?
相关问题
- Views base64 encoded blob in HTML with PHP
- How to get the background from multiple images by
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
The simplest one line that worked for me:
You can adjust opacity from 0 to 1 to make color lighter or darker.
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:
I've been able to do this using SVG filter. You can write a filter that multiplies the color of source image with the color you want to change to. In the code snippet below, flood-color is the color we want to change image color to (which is Red in this case.) feComposite tells the filter how we're processing the color. The formula for feComposite with arithmetic is (k1*i1*i2 + k2*i1 + k3*i2 + k4) where i1 and i2 are input colors for in/in2 accordingly. So specifying only k1=1 means it will do just i1*i2, which means multiplying both input colors together.
Note: This only works with HTML5 since this is using inline SVG. But I think you might be able to make this work with older browser by putting SVG in a separate file. I haven't tried that approach yet.
Here's the snippet:
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)
In most browsers, you can use filters :
on both
<img>
elements and background images of other elementsand set them either statically in your CSS, or dynamically using JavaScript
See demos below.
<img>
elementsYou can apply this technique to a
<img>
element :Background images
You can apply this technique to a background image :
JavaScript
You can use JavaScript to set a filter at runtime :
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
:hover
to illustrate the behaviour; if you just want to change color for the "normal" state, you should remove the pseudoclass.