css image filters

I wanted to include a link to a quick tutorial on CSS image filters on my no ai webring page to allow applicants to style their own widgets, but found that no one has satisfactorily written this down anywhere. It's all too long, no one talks about inline CSS, there are no simple, CodePen-free examples. So I wrote my own quick and dirty guide.


Let's take this picture of a little Geranium flower:

a colour photograph of a pink Geranium flower against an out of focus background of greenery

and apply every CSS filter to it, one at a time. Below you'll see examples with a simple version of the code that produced it.

blur

Normal is 0px; a higher pixel number means more blur.

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

blurred, 2px

a colour photograph of a pink Geranium flower against an out of focus background of greenery

blurred, 5px

<img style="filter:blur(0px)" src="image.jpg" alt="an image">

<img style="filter:blur(2px)" src="image.jpg" alt="an image">

<img style="filter:blur(5px)" src="image.jpg" alt="an image">


brightness

Normal is 100%; lower is darker, higher is lighter.

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

50% brightness

a colour photograph of a pink Geranium flower against an out of focus background of greenery

250% brightness

<img style="filter:brightness(100%)" src="image.jpg" alt="an image">

<img style="filter:brightness(50%)" src="image.jpg" alt="an image">

<img style="filter:brightness(250%)" src="image.jpg" alt="an image">


contrast

Normal is 100%; higher is more contrast, lower is less.

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

50% contrast

a colour photograph of a pink Geranium flower against an out of focus background of greenery

250% contrast

<img style="filter:contrast(100%)" src="image.jpg" alt="an image">

<img style="filter:contrast(50%)" src="image.jpg" alt="an image">

<img style="filter:contrast(250%)" src="image.jpg" alt="an image">


drop-shadow

Define a drop shadow by its horizontal offset, vertical offset, blur and finally colour - in that order.

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

-6px -6px 8px green

a colour photograph of a pink Geranium flower against an out of focus background of greenery

5px 5px 2px #000

<img style="filter:drop-shadow(0px 0px 0px #000)" src="image.jpg" alt="an image">

<img style="filter:drop-shadow(-6px -6px 8px green)" src="image.jpg" alt="an image">

<img style="filter:drop-shadow(5px 5px 2px #000)" src="image.jpg" alt="an image">


grayscale

Normal is 0%, full grayscale is 100%.

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

50% grayscale

a colour photograph of a pink Geranium flower against an out of focus background of greenery

100% grayscale

<img style="filter:grayscale(0%)" src="image.jpg" alt="an image">

<img style="filter:grayscale(50%)" src="image.jpg" alt="an image">

<img style="filter:grayscale(100%)" src="image.jpg" alt="an image">


hue-rotate

Rotates the image through the colour wheel. 0 is normal and goes through the spectrum to a max of 360 (back to normal).

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

shifted 90 degrees

a colour photograph of a pink Geranium flower against an out of focus background of greenery

shifted 180 degrees

<img style="filter:hue-rotate(0deg)" src="image.jpg" alt="an image">

<img style="filter:hue-rotate(90deg)" src="image.jpg" alt="an image">

<img style="filter:hue-rotate(180deg)" src="image.jpg" alt="an image">


invert

Inverts (negates) the image; mostly makes sense to just use 100%, but it goes from 0% (normal) to 100% (fully inverted).

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

50% inverted

a colour photograph of a pink Geranium flower against an out of focus background of greenery

100% inverted

<img style="filter:invert(0%)" src="image.jpg" alt="an image">

<img style="filter:invert(50%)" src="image.jpg" alt="an image">

<img style="filter:invert(100%)" src="image.jpg" alt="an image">


opacity

100% is normal, 0% is fully transparent.

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

50% opacity

a colour photograph of a pink Geranium flower against an out of focus background of greenery

0% opacity

<img style="filter:opacity(100%)" src="image.jpg" alt="an image">

<img style="filter:opacity(50%)" src="image.jpg" alt="an image">

<img style="filter:opacity(0%)" src="image.jpg" alt="an image">


saturate

100% is normal; the higher, the more saturated.

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

50% saturated

a colour photograph of a pink Geranium flower against an out of focus background of greenery

250% saturated

<img style="filter:saturate(100%)" src="image.jpg" alt="an image">

<img style="filter:saturate(50%)" src="image.jpg" alt="an image">

<img style="filter:saturate(250%)" src="image.jpg" alt="an image">


sepia

Make an image sepia toned. 0% is normal, 100% is fully sepia.

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

50% sepia

a colour photograph of a pink Geranium flower against an out of focus background of greenery

100% sepia

<img style="filter:sepia(0%)" src="image.jpg" alt="an image">

<img style="filter:sepia(50%)" src="image.jpg" alt="an image">

<img style="filter:sepia(100%)" src="image.jpg" alt="an image">


using multiple filters

Apply multiple filters by stringing them together after filter:.

a colour photograph of a pink Geranium flower against an out of focus background of greenery

normal

a colour photograph of a pink Geranium flower against an out of focus background of greenery

250% saturation
drop shadow
2px blur

a colour photograph of a pink Geranium flower against an out of focus background of greenery

180° hue rotation
250% contrast
250% brightness

<img "src="image.jpg" alt="an image">

<img style="filter:saturate(250%) drop-shadow(5px 5px 2px #000) blur(2px)" src="image.jpg" alt="an image">

<img style="filter:hue-rotate(180deg) contrast(250%) brightness(250%)" src="image.jpg" alt="an image">