Change color depending on background color with Sass [duplicate]

与世无争的帅哥 提交于 2019-12-23 10:04:14

问题


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?


回答1:


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.




回答2:


You could use control directives.

It depends on the purpose, but there might be easier ways to change a background color depending on a certain condition, such as Javascript.



来源:https://stackoverflow.com/questions/21802803/change-color-depending-on-background-color-with-sass

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!