← HTML practicum

Lesson 12 · 10 steps

Filters

The last file of the practicum. filter.html runs the same photographs and diagrams through ten CSS filters — shadow, blur, brightness, black and white, contrast, hue, inversion, transparency, saturation — none of which touches the files themselves.

Hand in filter.html
What the browser makes of it open on its own ↗
02A shadowThe task: create filter.html and the im_filter folder; add a shadow to the text with a filter.code ↓
<!-- 2) тінь тексту -->
<h2> Тінь </h2>
<div style="font-size:28px;color:#f0a500;width:100%">
  <span style="filter:drop-shadow(30px 10px 4px #4444dd)"> Counter-Strike </span>
  <span style="filter:drop-shadow(0 -6mm 4mm rgb(160, 0, 210))"> тримається </span>
  <span style="filter:drop-shadow(0 0 0.75rem crimson)"> на трьох речах: </span>
  <span style="filter:drop-shadow(16px 16px 10px black)"> прицілі, знанні карти </span>
  <span style="filter:drop-shadow(.5rem .5rem 1rem #e23)"> і довірі до команди. </span>
</div>

drop-shadow takes an offset across, an offset down, a blur radius and a colour. Any unit will do — px, mm, rem — which is what the five spans here are showing.

Today

There is also text-shadow, which does the same for text and nothing else. The difference shows on a picture with transparency: drop-shadow follows the actual shape of the thing, box-shadow only ever outlines its rectangle.

03BlurThe task: blur both an image and some text.code ↓
<!-- 3) розмиття -->
<h2> Розмиття </h2>
<div style="font-size:24px;color:#8bd450">
  <span style="filter:blur(4px)"> Димова граната закриває огляд на вісімнадцять секунд. </span>
  <img src="im_filter/navi.jpg" width="200" style="filter:blur(3px)">
  <img src="im_filter/navi.jpg" width="200" style="filter:blur(0.1rem)">
</div>

The number is the blur radius: how far each pixel is smeared. Four pixels makes text unreadable, which is exactly what it is used for — a preview nobody should be able to read yet.

04BrightnessThe task: change the brightness of text with a filter.code ↓
<h2> Яскравість </h2>
<div style="font-size:24px;color:#e6e6e6">
  <span style="filter:brightness(1)"> Світлошумова граната </span>
  <span style="filter:brightness(50%)"> засліплює на кілька секунд, </span>
  <span style="filter:brightness(200%)"> якщо дивитися прямо на неї. </span>
</div>

brightness(1) is unchanged, below one is darker, above one lighter. The value can be written as a number or a percentage — 0.5 and 50% are the same thing.

05Black and whiteThe task: turn an image and some text black and white.code ↓
<h2> Чорно-біле </h2>
<div style="font-size:24px;color:#f0a500">
  <span style="filter:grayscale(100%)"> Турнірне фото стає архівним, щойно з нього прибрати колір. </span>
  <img src="im_filter/player.jpg" width="150" style="filter:grayscale(100%)">
  <img src="im_filter/player.jpg" width="150" style="filter:grayscale(60%)">
  <img src="im_filter/player.jpg" width="150" style="filter:grayscale(0.2)">
</div>

grayscale(100%) removes all colour; anything in between gives a partly washed-out picture. Three copies of the same photo at three strengths make the scale obvious.

06ContrastThe task: change the contrast of an image and of text.code ↓
<h2> Контрастність </h2>
<div style="font-size:24px;color:#e6e6e6">
  <span style="filter:contrast(50%)"> Схема карти читається інакше, якщо підкрутити контраст. </span>
  <img src="im_filter/map.png" width="200" style="filter:contrast(50%)">
  <img src="im_filter/map.png" width="200" style="filter:contrast(1.75)">
  <img src="im_filter/map.png" width="200" style="filter:contrast(3)">
</div>

Contrast is the distance between light and dark. Below 1 everything slides towards grey; above 1 the picture hardens until only black and white are left.

07Rotating the hueThe task: shift the colours of an image around the colour wheel.code ↓
<h2> Поворот відтінку </h2>
<div>
  <img src="im_filter/navi.jpg" width="180" style="filter:hue-rotate(90deg)">
  <img src="im_filter/navi.jpg" width="180" style="filter:hue-rotate(-0.25turn)">
  <img src="im_filter/navi.jpg" width="180" style="filter:hue-rotate(3.142rad)">
</div>

hue-rotate turns every colour the same distance around the wheel: 90deg, -0.25turn and 3.142rad are three ways of writing an angle.

08InversionThe task: produce an inverted version of a picture.code ↓
<h2> Інверсія </h2>
<div>
  <img src="im_filter/c4.png" width="110" style="filter:invert(0.30)">
  <img src="im_filter/c4.png" width="110" style="filter:invert(50%)">
  <img src="im_filter/c4.png" width="110" style="filter:invert(70%)">
  <img src="im_filter/c4.png" width="110" style="filter:invert(1)">
</div>

invert(1) swaps every colour for its opposite — black becomes white. Half-way, invert(50%), everything collapses into flat grey, because every colour meets its opposite exactly in the middle.

09TransparencyThe task: change the transparency of an image with a filter.code ↓
<h2> Прозорість </h2>
<div>
  <img src="im_filter/c4.png" width="110" style="filter:opacity(0.20)">
  <img src="im_filter/c4.png" width="110" style="filter:opacity(0.20) invert(1)">
</div>

Filters can be combined by listing them with a space: opacity(0.20) invert(1) applies both, in that order.

Today

There is a plain opacity property too, and it does the same to a single element. The filter version exists so transparency can be mixed into a chain with the others.

10SaturationThe task: change the colour saturation of a picture.code ↓
<h2> Насиченість </h2>
<div>
  <img src="im_filter/map.png" width="180" style="filter:saturate(4)">
  <img src="im_filter/map.png" width="180" style="filter:saturate(20)">
  <img src="im_filter/map.png" width="180" style="filter:saturate(50%)">
  <img src="im_filter/map.png" width="180" style="filter:saturate(0)">
</div>

The last of them: saturate(0) is black and white again, saturate(20) is colour pushed well past anything real.

All ten filters run on the graphics card, every frame, which is why they can be animated without the page stuttering. A decade ago the same effects meant editing the picture in Photoshop and uploading a second copy.