Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace color of PNG image using CSS? [duplicate]

Tags:

html

css

image

png

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.

enter image description here

like image 391
DiamondJoe12 Avatar asked Apr 22 '20 16:04

DiamondJoe12


2 Answers

Use the image as mask and you can do it:

.img {
  width:150px;
  height:150px;
  display:inline-block;
  background:red;
  -webkit-mask:url("https://i.ibb.co/FhZb3Xs/CJcLK.png") center/contain;
          mask:url("https://i.ibb.co/FhZb3Xs/CJcLK.png") center/contain;
}

img {
  width:150px;
  height:150px;
}
<div class="img"></div>
<div class="img" style="background:#2a4f6c"></div>
<img src="https://i.ibb.co/FhZb3Xs/CJcLK.png" >
like image 116
Temani Afif Avatar answered Sep 28 '22 00:09

Temani Afif


You can't change the image color directly in css. (svg, icons can be possible) Use various filter to change color, change hue-rotate value in code to get various color;

.gavel-icon{
  filter: saturate(500%) contrast(800%) brightness(500%) 
      invert(80%) sepia(50%) hue-rotate(120deg); 
}
like image 21
Yadab Sd Avatar answered Sep 27 '22 23:09

Yadab Sd