Calculating width from percent to pixel then minus by pixel in LESS CSS

后端 未结 4 1644
[愿得一人]
[愿得一人] 2020-12-22 16:42

I will calculate width in some element from percent to pixel so I will minus -10px via using LESS and calc(). It´s possible?



        
4条回答
  •  [愿得一人]
    2020-12-22 17:43

    You can escape the calc arguments in order to prevent them from being evaluated on compilation.

    Using your example, you would simply surround the arguments, like this:

    calc(~'100% - 10px')
    

    Demo : http://jsfiddle.net/c5aq20b6/


    I find that I use this in one of the following three ways:

    Basic Escaping

    Everything inside the calc arguments is defined as a string, and is totally static until it's evaluated by the client:

    LESS Input

    div {
        > span {
            width: calc(~'100% - 10px');
        }
    }
    

    CSS Output

    div > span {
      width: calc(100% - 10px);
    }
    

    Interpolation of Variables

    You can insert a LESS variable into the string:

    LESS Input

    div {
        > span {
            @pad: 10px;
            width: calc(~'100% - @{pad}');
        }
    }
    

    CSS Output

    div > span {
      width: calc(100% - 10px);
    }
    

    Mixing Escaped and Compiled Values

    You may want to escape a percentage value, but go ahead and evaluate something on compilation:

    LESS Input

    @btnWidth: 40px;
    div {
        > span {
            @pad: 10px;
            width: calc(~'(100% - @{pad})' - (@btnWidth * 2));
        }
    }
    

    CSS Output

    div > span {
      width: calc((100% - 10px) - 80px);
    }
    

    Source: http://lesscss.org/functions/#string-functions-escape.

提交回复
热议问题