LESS CSS set variables in media query?

后端 未结 8 1880
孤独总比滥情好
孤独总比滥情好 2020-12-03 16:27

I\'m working on a iPad-specific website. To make my website work on both the retina display iPad and older versions of iPads, I want to set a variable in LESS CSS in media q

相关标签:
8条回答
  • 2020-12-03 17:25

    It would be nice, but this is impossible to do like this.

    LESS compiles your media queries to actual media queries, so at compile time there is no indication of which media query will be relevant to the browser.

    After compile time you just have CSS not less so you can't have variables anymore.

    You can do this instead but it isn't as elegant:

    @base_width: 100px;
    
    @media all and (max-width: 768px) {
        .something {
            width: @base_width;
        }
    }
    
    @media all and (min-width: 769px) {
        .something {
            width: @base_width * 2;
        }
    }
    
    0 讨论(0)
  • 2020-12-03 17:28

    LESS currently cannot do this, although it would be technically possible for it to do it. This feature has been requested in the GitHub issue entitled Less variables in media queries.

    See also this question: CSS pre-processor with a possibility to define variables in a @media query

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