Assuming an element is at 100% saturation, opacity, etc... how can I have its background become slightly lighter when it is hovered?
The use case is that I'm allowing a user to hover over any element on a page. I don't want to go around determining each colors equivalent at 80% opacity.
One method is to change the opacity: 0.4
but I only want the background to change.
Here's an easy way to do it:
It's a long time ago but you can do something like this:
You can change the 100px into a number you want. I took a large one to cover the whole element.
It isn't a very beautiful solution but it works!
Here an example: http://jsfiddle.net/6nkh3u7k/5/
you should use the RGBa method (
background-color:rgba(R,G,B,alpha);
) to do this:FIDDLE
AND if you strongly need to make it work in IE8 or lower too here is how it comes:
note that the
startColorstr
andendColorstr
values are built like this#AARRGGBB
(where AA is the Alpha channel) and must be the same if you don't want a gradient effect from a color to another.You can do this with only CSS using
filter: brightness();
but it is only currently supported in WebKit browsers. See http://jsfiddle.net/jSyK7/You want to change the
background-color
lightness of any element that is hovered without using opacity. Unfortunately. I don't think this is possible without setting specificbackground-color
values for your hovers.There is one alternative that I can think of but it would require a translucent PNG overlay on the entire element, which will also cover any of the element's contents. Thereby not solving your problem.
Related Question: Dynamically change color to lighter or darker by percentage CSS (Javascript)
I would use a
:after
pseudo-element instead of a conventional background. It's supported in IE8, wherergba()
isn't.HTML:
CSS:
or something like that.
http://caniuse.com/#search=%3Aafter
For a smoother result, add a CSS3 transition:
The previous snippet was copied and pasted from http://css3please.com
http://jsfiddle.net/ghodmode/6sE9E/