Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic darken color in Sass / Compass

Tags:

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 ?

like image 679
enguerranws Avatar asked Jan 08 '14 18:01

enguerranws


People also ask

How do I darken colors in SCSS?

You can't have a solution for something that is impossible. 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.

How do you use opacity in Sass?

With Sass, just wrap your color with rgba() and the opacity value as you would with normal CSS. Sass will take care of breaking down the color value into the correct RGB values.

How do I use rgba in Sass?

Converting a dark gray hex to rgba black with 50 percent opacity would like this: background-color: rgba(#333333, 0.5); That Sass line will compile to this in CSS: background-color: rgba(51, 51, 51, 0.5);


2 Answers

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 :)

like image 180
enguerranws Avatar answered Oct 20 '22 20:10

enguerranws


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

.button:hover {   filter: brightness(85%); } 
like image 39
Merovex Avatar answered Oct 20 '22 21:10

Merovex