I have an icon in a webpage:
<div class='icon-container'>
<img src = "img/gavel3.png" class="gavel-icon" style='vertical-align:center;width:80px;'>
</div>
I’m trying to replace the black in this image with the color: #2a4f6c
using CSS only. I tried to replace the black with this color using Photoshop, and it looks awful and grainy. Is a solution possible using pure CSS?
Image in question below.
3
Answers
Your icon is a
png
image, so each pixel is defined separately to have its own color. There are no whole shapes that you can target with css and define a color, as you would for regular HTML elements.However, there I a few things I would say:
I would have thought that it should be possible to change the color of this shape in photoshop without making it look any more grainy or pixelated than before.
I would suggest making this into an
svg
. This is a vector file format, so it generally has a smaller file size compared to pixel images, has completely sharp and defined edges, and can be scaled up to any size without reducing its quality. And, most importantly for you, it’s very easy to change its color with CSS.You could try using CSS filters to change the appearance of your
png
. Check out this stackoverflow question to see if it helps: Change color of PNG image via CSS?As I’ve said, an
svg
would be my recommended option. I’ve created a code snippet below of what that would look like – you’ll find your color#2a4f6c
in the code, so just change that if you want the image to have a different color again..Use the image as mask and you can do it: