32

I'm setting up a CSS for a website where all the links, in :hover state, are darker than in normal state.

I'm using Sass/Compass so I looked to the darken Sass method, here : http://sass-lang.com/documentation/Sass/Script/Functions.html#darken-instance_method

Here's the use : darken($color, $amount)

My question is : how can I make this "automatic" to set all my <a> elements to be 80% darker ?

What I'm trying to do is (Sass syntax) :

a
   background: $blue
   &:hover
      background: darken(this element background-color, 80%)

What's the best solution to do this ?

enguerranws
  • 8,087
  • 8
  • 49
  • 97
  • 1
    possible duplicate of [SASS - Manipulate inherited color?](http://stackoverflow.com/questions/14920801/sass-manipulate-inherited-color) – cimmanon Jan 08 '14 at 18:23
  • 1
    You can't have a solution for something that is impossible. – cimmanon Jan 08 '14 at 18:39
  • 1
    This is not possible in SASS/SCSS. However, you could consider using the "darker" color as the link color to begin with, and set it to 80% opacity and then 100% opacity in the hover state. Similar effect, less repetition. The mixin you wrote hardly saves time. – Ennui Jan 08 '14 at 19:26
  • Wouldn't `background: darken($blue, 80%)` work? – Katie Kilian Jan 08 '14 at 19:59
  • 1
    A dirty hack™ would be make the element sit on a black background, and change the opacity on hover – Mike Causer Mar 19 '15 at 05:39
  • Yes that could work too I guess. – enguerranws Mar 19 '15 at 09:49

2 Answers2

43

I thought about this.

The only way I found is by creating a mixin :

@mixin setBgColorAndHover($baseColor)
    background-color: $baseColor
    &:hover
          background-color: darken($baseColor, 5%)

And then :

.button
    +setBgColorAndHover($green) // as $green is a color variable I use.

Not the best, but that will do the job :)

enguerranws
  • 8,087
  • 8
  • 49
  • 97
22

By now, better to use a filter in native CSS:

.button:hover {
  filter: brightness(85%);
}
Merovex
  • 921
  • 9
  • 10