Change color depending on background color with Sass

css, frontend, sass

Solution

You could use `lightness()` function for the `background-color` to determine the `color` value, as 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.

Problem

I want to set up some sass color rules that will automatically choose the font color variable for me. I want the text color to be dependent on what color the background color of the parent div is. If ``` div {background-color: #000; } ``` Then ``` div p { color: #fff; } ``` How can this be achieved with sass?

Original source

Related problems