Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css filter to make elements one color

Tags:

The following CSS filter:

filter: brightness(0) invert(1);

makes elements all-white (source). Neat, but is there a way to make them another color instead, e.g. blue? This is for situations where there is a transparent background, e.g. an icon. I want to make the icon one arbitrary color.

like image 733
mahemoff Avatar asked Jan 17 '15 12:01

mahemoff


People also ask

How do I get filter color in CSS?

So using the earlier example the final solution (if your image is not black) would be: filter: brightness(0) saturate(100%) invert(8%) sepia(97%) saturate(7488%) hue-rotate(12deg) brightness(92%) contrast(114%);

What is Webkit filter in CSS?

The filter CSS property applies graphical effects like blur or color shift to an element. Filters are commonly used to adjust the rendering of images, backgrounds, and borders. Included in the CSS standard are several functions that achieve predefined effects.

How do you do saturation in CSS?

CSS | saturate() FunctionThe saturate() function is an inbuilt function in CSS which is used to super-saturate or desaturates the input image. Parameters: This function accepts single parameter amount, which holds the amount of conversion. The value of the parameter is set in terms of number or percentage.


2 Answers

The hue-rotate() filter affects all colors however so it won't turn a full color image into a one color image. Rather it will move all the colors around the color wheel.

However you can hack a solution by converting to grayscale, adding sepia and then rotating the hue This make an image look like a single hue shaded green:

filter: grayscale(100%) sepia(100%) hue-rotate(90deg);

If you care dealing with vector work like an icon that you will use a lot, you might consider converting it to SVG, then you can color it with plain css. https://icomoon.io can help with this.

like image 173
Mark Avatar answered Sep 17 '22 15:09

Mark


If you reference an SVG filter from your CSS, then you can get a specific color. SVG filter snippet below. For your specific color replace the .7/.4/.5 with the unitized value of your RGB values.

<filter id="colorme" color-interpolation-filters="sRGB">
  <feColorMatrix type="matrix" values="0 0 0 0 .7  0 0 0 0 .4  0 0 0 0 .5  0 0 0 1 0"/>
</filter>

hue-rotate() is a very impaired filter since it doesn't operate in HSL space. It's an RGB approximation which tends to clip saturated colors badly.

like image 30
Michael Mullany Avatar answered Sep 18 '22 15:09

Michael Mullany