Conditional CSS based on background color variable

前端 未结 2 1132
北恋
北恋 2020-11-29 11:31

Is it possible to set a color based on the lightness/darkness of a less variable?

Currently I have the following code:

li {
    color: darken(@bodyBa         


        
相关标签:
2条回答
  • 2020-11-29 12:00

    There's contrast function, e.g.:

    li {
        color: contrast(@bodyBackground,
                lighten(@bodyBackground, 13%),
                 darken(@bodyBackground, 13%));
    }
    
    0 讨论(0)
  • 2020-11-29 12:08

    You can be much smarter than this. You can make a mixin to check the provided color, and perform either a darken or lighten depending on its color:

    .smart-text-color (@a) when (lightness(@a) >= 50%) {
      // Its a light color return a dark output
      color: darken(@a, 60%);
    }
    .smart-text-color (@a) when (lightness(@a) < 50%) {
      // Its a dark color return a dark output
      color: lighten(@a, 60%);
    }
    

    Here is an example mixin, that takes the background color as the variable, and works out what text color to use. Returning either a light or dark output.

    http://codepen.io/TristanBrotherton/pen/GwIgx?editors=110

    0 讨论(0)
提交回复
热议问题