Lighten color from parent in Less

前端 未结 1 565
眼角桃花
眼角桃花 2021-02-11 02:22

I am starting out with Less and one of the reasons I wanted to is because of the ligthen() function. So my first attempt was to do something with that.

This is my HTML

相关标签:
1条回答
  • 2021-02-11 02:39

    I am not completely sure what your desired solution would be ... but maybe something like making a mixin would help you from having to write so much stuff out.

    LESS:

    .bgmixin(@color) {
        (~".@{color}") {
            background-color: @@color;
            .boxbar {
                background-color: lighten(@@color, 10%);
            }
        }
    }
    
    @blue: #468ACE;
    @green: #41A53D;
    @red: #9C2525;
    
    .bgmixin("blue");
    .bgmixin("green");
    .bgmixin("red");
    

    CSS:

    .blue{
      background-color: #468ace;
    }
    .blue .boxbar {
      background-color: #6ea3d9;
    }
    .green{
      background-color: #41a53d;
    }
    .green .boxbar {
      background-color: #59c055;
    }
    .red{
      background-color: #9c2525;
    }
    .red .boxbar{
      background-color: #c52f2f;
    }
    

    Update:

    In LESS>=1.4 you would want to use something like this to interpolate the class name from the color name:

    .bgmixin(@color) {
        @classname: ~"@{color}"; 
        .@{classname} {
            background-color: @@color;
            .boxbar {
                background-color: lighten(@@color, 10%);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题