Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Darkening an image with CSS (In any shape)

Tags:

css

png

tint

So I have seen quite a few ways to darken images with CSS, including ones with rounded corners, but my problem is different.

Let's say I have an .png image that looks like a little dog (just go with it, I don't have any good examples), when I place it on my page, I give it dimensions of 100 x 100.

But I can't just overlay something on it, or tint the entire image, as it will cause the background of the dog to be tinted as well, which looks ugly.

Is it possible to tint an image of arbitrary shape with CSS?

(I'm assuming you understand my point, and useless code is not necessary)

Thanks!

like image 834
Josiah Avatar asked Apr 02 '13 13:04

Josiah


People also ask

How do you make an image darker in CSS?

The brightness() function can be used as a value to apply a linear multiplier to make it appear darker or lighter than the original. To make an image darker, any value below 100% could be used to darken the image by that percentage.

How do you make something darker in CSS?

CSS has a filter property that can be used with a variety of filter functions. One of them is the brightness() filter. By feeding a percentage less than 100% to brightness() , the target element will be made darker.

How do I darken an image on hover?

If you want to darken the image, use an overlay element with rgba and opacity properties which will darken your image...


2 Answers

Easy as

img {   filter: brightness(50%); } 
like image 75
The Whiz of Oz Avatar answered Nov 07 '22 20:11

The Whiz of Oz


You could always change the opacity of the image, given the difficulty of any alternatives this might be the best approach.

CSS:

.tinted { opacity: 0.8; } 

If you're interested in better browser compatability, I suggest reading this:

http://css-tricks.com/css-transparency-settings-for-all-broswers/

If you're determined enough you can get this working as far back as IE7 (who knew!)

Note: As JGonzalezD points out below, this only actually darkens the image if the background colour is generally darker than the image itself. Although this technique may still be useful if you don't specifically want to darken the image, but instead want to highlight it on hover/focus/other state for whatever reason.

like image 43
Sean Avatar answered Nov 07 '22 20:11

Sean