DEV Community

Cover image for How to overlay your background images

How to overlay your background images

selbekk on June 26, 2019

I learned two nice little tricks today, and thought I'd write a short article on them. The challenge Often, we have background images t...
Collapse
 
rmnvsl profile image
Roman Veselý

Nice usage of custom css properties! But I'm worried about the example with box-shadow - it could be really heavy for mobile devices.

I'd rather avoid that one, and keep it just as an example of how many possible ways we have ;)

Collapse
 
selbekk profile image
selbekk

Yeah I was wondering if it would cause any perf issues. I’ll add a note about it :-)

Collapse
 
larisho profile image
Gab • Edited

This is really great! I just spent waay too long trying to accomplish this using the "old way"--you're a life saver.
Just one question: where do you learn about these sorts of modern CSS approaches? There are a lot of mediocre resources out there (I'm looking at you, W3Schools) and it's hard to know what's worth reading through

Collapse
 
selbekk profile image
selbekk • Edited

Hi! I’m glad you found it useful!

To be honest, most of these techniques I’ve read about in blogs and other non-structured places of learning.

MDN is great as a reference, css-tricks.com is great for picking up techniques, and tympnus is great for inspiration.
and css.christmas is great for, well, small tips and tricks - that was our project for Christmas last year 😄

Collapse
 
larisho profile image
Gab

Thanks for the suggestions!
I have already started reading through css.christmas--very funny! I look forward to reading the rest of it.

Collapse
 
zephys profile image
Marcelo Rodrigues

Thank you. Save me.

Collapse
 
duncanmacgregor profile image
Duncan M. MacGregor

This is very clever! Thank you for sharing.

Just one question, how well supported is the var(--image-url) method?

I can't seem to find it on caniuse.com.

Cheers!

Collapse
 
selbekk profile image
selbekk

It’s not supported in IE, unfortunately, since it doesn’t support css dynamic properties. Otherwise, it’s well supported.

Collapse
 
duncanmacgregor profile image
Duncan M. MacGregor

Ahhh, that darn web browser! haha
That's great news it's supported by modern browsers.
Thanks again, really appreciate the knowledge :-)

Collapse
 
gsotelo23 profile image
G

Awesome! This really helped me a lot!

Collapse
 
selbekk profile image
selbekk

Thanks for letting me know! 🥳

Collapse
 
07zee07 profile image
07Zee07

Somehow it doesn't work for me with grid. I only get the invalid property value on the style="--........

Collapse
 
selbekk profile image
selbekk

sorry to hear! Are you using React or something? I'd love to help, but there's not a lot I can do with the information provided ☹️

Collapse
 
07zee07 profile image
07Zee07

hi!
truth be told this is my first website and I am a bit in the dark. I have nested grids applied and very minimal styles (just some font, plus the necessary for the grid to map out the grid areas)
I just wanted a nice overlay for the background pic so the text would be more readable (and it would look a bit fancier too)

Thread Thread
 
07zee07 profile image
07Zee07

I must apologize.
one word: TYPO
it doesn't matter how many times I read my code I could not find it. I made my partner read it, and bang!
lesson learned!!
people! make others read your code too!!
I'm sorry, it's working fine!!!

Collapse
 
angelorubin profile image
angelo rogerio rubin

What a wonderful article, THANK YOU!

Collapse
 
selbekk profile image
selbekk

Thanks for letting me know! 🎉

Collapse
 
hoffmann profile image
Peter Hoffmann

Could the same result be also archived by CSS filters?

Collapse
 
selbekk profile image
selbekk

Great question! Yeah, you'd think that by adding a filter: brightness(0.5), you'd end up with something similar. Unfortunately, that darkens everything, including the content in front of the background image.

Here's a codepen showing what I mean:

Collapse
 
aamangeldina profile image
aamangeldina

This is really helpful and working perfectly! Thank you so much!

Collapse
 
selbekk profile image
selbekk

I'm glad you found it helpful! :)

Collapse
 
ghersabilell profile image
Bilell Ghersa

Thnx dude that was helpfull