Change color based on background color with Sass

I want to configure some sass color rules that will automatically select a font color variable for me. I want the text color to depend on what color the background color of the parent div is.

If a

div {background-color: #000; }

Then

div p { color: #fff; }

How can this be achieved with sass?

+4
source share
2 answers

You can use the function lightness()for background-colorto determine the value coloras follows:

@function set-color($color) {
    @if (lightness($color) > 40) {
      @return #000;
    }
    @else {
      @return #FFF;
    }
}

Then use the above function as below:

div { background-color: black; // Or whatever else }
div p { color: set-color(black); }

LIVE DEMO .

+10
source

You can use control directives .

, , , Javascript.

0

Source: https://habr.com/ru/post/1527120/


All Articles