Defining Variable Variables using LESS CSS

前端 未结 5 1526
野趣味
野趣味 2020-11-28 06:21

Say I have three separate color schemes that are used on various pages in a site. Each color has a a light, medium and dark tint defined, and the color scheme is defined by

5条回答
  •  孤城傲影
    2020-11-28 06:57

    If those values really follow a predictable format like that, seems like a perfect case for a parametric mixin:

    Less:

    @red:   #232;
    @green: #565;
    @blue:  #898;
    
    
    .theme (@color) {
      background-color: @color - #111;
      #container {
        background-color: @color;
        p { color: @color + #111; }
      }
    }
    
    body.red {
      .theme(@red);
    }
    

    Compiled CSS:

    body.red{background-color:#112211;}
    body.red #container{background-color:#223322;}
    body.red #container p{color:#334433;}
    

提交回复
热议问题