Skip to main content

CSS filters for background images

18 Oct 2022 - css, filter, csshacks
There is a pretty simple trick to enable CSS image filters for background images without changing the markup for the original code. This is an example built from ideas from others, but doesn't move the background image to another element in the markup. It's a pretty convenient way to add filters to background images.

So let's say you have this code:

Now lets say you want the background in greyscale for instance. Now most CSS filters aren't available to target the background layer only, so when you add the filter, this happens:

Now there are hacks to introduce filters to the background by creating extra elements and moving the background image to that one. The drawback to this is that it is usually done with fixed sized items. And if you're in a scenario where you don't have total control over the code produced and want to solve this in CSS alone, there is a pretty simple trick:

What we did was change the parent element to position: relative and then add a ::before element to that one, that is positioned absolutely and inherits the background image, and is then manipulated with the filter. Using z-index we can assure that it is positioned between the actual background element and the content

Blurring

Due to the added element, blurring the background image this way is slightly different than blurring the original element, the blur towards the edges fade out in a way that may not be desirable:

This can be remedied by positioning the new element "outside" the original, like this:

We've also added overflow: hidden to the original element. Something to keep in mind is that the new background image is slightly larger than the original one in this instance.
Mer i Tutorials
PHP och BankID
Create snow in Photoshop
Modellera URLar
PHP Formatera telefonnummer
Skapa ett Netflixprogram
Expandera korta URLar
Parsea hashtaggar
Nyckelord: css, filter or csshacks
Hur nya iPaden stller till det p webben
CSSruta igen nu med skugga
Give up and use tables
Skuggor med CSS
Skapa rutor med CSS
Erstta tabeller med CSS
CSS eller tabeller
Ostkupa
Stranger Things, säsong 5
Första december!
Nordic: The Musical
Livets träd
Myst Book
Web versions of the journals
Predator: Badlands
Myst Book
Selenitic Age
Ny kamera, gammal glöd
Tekoppen
Årets Halloweenfest var mycket lyckad!
Porträtt av t-o-m-u-s-a
Porträtt av u/arielgirle
Profilporträtt
GameConnect
Ny PC!
50 år
Hemma Bäst
Garageuppfart för husbil: Uppfarten är klar!
Ny kamera: Nikon Zf
Hemma Bäst
Garageuppfart för husbil: Massa grus!
The lightsabers are done!
Hemma Bäst
Garageuppfart för husbil: Lagt ut plattor och skyfflat makadam
Hemma Bäst
Garageuppfart för husbil: Grävt och klart!
Hemma Bäst
Pooltak
Nordic: The Musical
Valkyrior
Tekoppen
Kräftskiva och eldfest på Tekoppen!
Lord of the Rings timeline
Alien Timeline
Borta Bra
Snart är det Medeltidsveckan!
Hemma Bäst
Utebar: Pergola
Hemma Bäst
Utebar: Refrigerator and bar stools
Hemma Bäst
Utebar: Cupboard doors
Nordic: The Musical
Lokes Vrede
Hemma Bäst
Garageuppfart för husbil: Garageuppfart för husbil!
Arkad- och flipperkabinett
Robotar
Nordic: The Musical
Blodsbröder
Nordic: The Musical
Midgård
Nordic: The Musical
Himlen brinner
Nordic: The Musical
Orosmoln i Asgård
Nordic: The Musical
Gudarnas spel
Nordic: The Musical
Oändlig kärlek
Update on the lightsaber project
Hemma Bäst
Skåp till Ute-TV
Thåström i Globen
Bröllopspresent
Födelsedagspresent
Borta Bra
Recension: Jacy's
Thåström
Nordic: The Musical
Kunskapens pris
Nordic: The Musical
Allting börjar alltid någonstans
Borta Bra
Recension: Bohusgården Hotell & Konferens
Borta Bra
Recension: Elite Palace