Fancy Media Queries with some LESS Magic

后端 未结 8 1389
别跟我提以往
别跟我提以往 2020-12-04 05:23

It would be nice to wrap css-styles for different resolutions within some css-classes using less.

I\'d like to do something like:

footer {
  width: 1         


        
相关标签:
8条回答
  • 2020-12-04 06:06

    I am using these mixins & variables

    .max(@max; @rules){@media only screen and (max-width: (@max - 1)){@rules();}}
    .min(@min; @rules){@media only screen and (min-width: @min){@rules();}}
    .bw(@min; @max; @rules){@media only screen and (min-width: @min) and (max-width: (@max - 1)){@rules();}}
    
    @pad: 480px;
    @tab: 800px;
    @desktop: 992px;
    @hd: 1200px;
    

    So this

    footer{
        width: 100%;
        .bw(@tab,@desktop,{
            width: 768px;
        });
        .min(@desktop,{
            width: 940px;
        });
    }
    

    becomes

    footer {
      width: 100%;
    }
    @media only screen and (min-width: 800px) and (max-width: 991px) {
      footer {
        width: 768px;
      }
    }
    @media only screen and (min-width: 992px) {
      footer {
        width: 940px;
      }
    }
    
    0 讨论(0)
  • 2020-12-04 06:18

    I completely agree with Hai Nguyen's answer (which has been accepted) but you can clean it up a bit more by doing something like this:

    @desktop:   ~"only screen and (min-width: 960px) and (max-width: 1199px)";
    @tablet:    ~"only screen and (min-width: 720px) and (max-width: 959px)";
    
    footer{
      width: 100%;
      @media @tablet {
        width: 768px;
      }
      @media @desktop {
        width: 940px;
      }
    }
    

    It's essentially the same thing but lets you nest your media queries inside of the original selector. It keeps all css for a specific element together and makes your styles much more modular (vs the split breakpoint approach).

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