Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color depending on background color with Sass [duplicate]

Tags:

css

sass

frontend

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?

like image 935
whatsnewsaes Avatar asked Feb 15 '14 19:02

whatsnewsaes


People also ask

How do I change colors in SCSS?

Sass Set Color Functions An RGB color value is specified with: rgb(red, green, blue). Each parameter defines the intensity of that color and can be an integer between 0 and 255, or a percentage value (from 0% to 100%). Sets a color using the Red-Green-Blue-Alpha (RGBA) model.

Which CSS property is used to change the background color of an element?

The background-color CSS property sets the background color of an element.

Can we change background color in CSS?

It can be given by using the color name, rgb() value, or the hexadecimal value. The transparent value of this property is the default value, which specifies the transparent background color.

How do I change the color of a value in CSS?

Simply add the appropriate CSS selector and define the color property with the value you want. For example, say you want to change the color of all paragraphs on your site to navy. Then you'd add p {color: #000080; } to the head section of your HTML file.


1 Answers

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.

like image 94
Hashem Qolami Avatar answered Oct 02 '22 14:10

Hashem Qolami