使用 Sass 根据背景颜色更改颜色 [英] Change color depending on background color with Sass

查看:23
本文介绍了使用 Sass 根据背景颜色更改颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一些 sass 颜色规则,它们会自动为我选择字体颜色变量.我希望文本颜色取决于父 div 的背景颜色是什么颜色.

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.

如果

div {background-color: #000; }

那么

div p { color: #fff; }

如何使用 sass 实现这一点?

How can this be achieved with sass?

推荐答案

background-color可以使用lightness()函数来判断颜色值,如下:

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); }

现场演示.

这篇关于使用 Sass 根据背景颜色更改颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆