Manipulate Images Dynamically With CSS Filters
July 21, 2018 by Andreas Wik
With CSS filters we can easily manipulate images, such as turning them black and white, make them blurry, adjust brightness, contrast and colors, and more.
Check out this demo (CodePen below) I put together where you can play around with filter values and see the result straight away.
Let’s have a look at the different filters’ syntax and how they work.
This one I like a lot – easily turn the image partially of completely black and white.
Default value is 0% and 100% will turn it completely black and white.
Default value is 0px and the higher the value the more blurry the image will be.
Default value is 100% and 0% will make the image completely black. The higher the percent value, the brighter the image will be.
Default value is 100% and 0% will make the image completely gray. The higher the percent value, the more contrast the image will have.
Default value/max value is 100%. The lower the value, the more transparent the image will be.
Add a hue rotation to the image. You pass the filter a degree value which is the degree of the rotation. Default value is 0deg.
invert() flips the colors of an image. Pass it a value between 0% and 100%. If no value is passed then 100% is applied.
Saturation is used to describe the intensity of colors in an image. Default value is 0% and 100% is a complete saturation.
Sepia is kind of a reddish-brown color, and this filter will turn your convert your image into a sepia. Default value is 0% which will leave the image unchanged, while 100% will be a full conversion of the image. Anything inbetween will ease the conversion.
The drop-shadow filter adds a shadow behind the image. It takes 5 values, only the 2 first being required.
These values are:
<offset-x> (required) which defines the horizontal offset of the shadow. A negative value will position the shadow to the left of the image while a positive value will put it to the right. Pixel value.
<offset-y> (required) which defines the vertical offset of the shadow. A negative value will position the shadow to the above the image while a positive value. Pixel value.
<blur-radius> (optional) defines how blurry you want the shadow to be. The larger the value, the bigger more blurry it’ll be. Pixel value.
<spread-radius> (optional) defines how big of a spread the
<color> (optional) defines the color of the shadow.
filter: drop-shadow(5px 5px 8px 7px #333333);
As always, have a look at the browser support to make sure it’s acceptable for your project.