Fancy Media Queries with some LESS Magic

后端 未结 8 1388
别跟我提以往
别跟我提以往 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 05:51

    +1 for Nguyen and Yancey - and one more addition.

    If you want explicit definition of the widths, you can accomplish that with string interpolation and variables for your breakpoints. Here for example with those of bootstrap - the strict rules are to prevent definition overlapping.

    @screen-xs-min:     480px;
    @screen-sm-min:     768px;
    @screen-md-min:     992px;
    @screen-lg-min:     1200px;
    
    @screen-xs-max:     (@screen-sm-min - 1);
    @screen-sm-max:     (@screen-md-min - 1);
    @screen-md-max:     (@screen-lg-min - 1);
    
    @phone:             ~"only screen and (max-width: @{screen-xs-min})";
    @phone-strict:      ~"only screen and (min-width: @{screen-xs-min}) and (max-width: @{screen-xs-max})";
    @tablet:            ~"only screen and (min-width: @{screen-sm-min})";
    @tablet-strict:     ~"only screen and (min-width: @{screen-sm-min}) and (max-width: @{screen-sm-max})";
    @desktop:           ~"only screen and (min-width: @{screen-md-min})";
    @desktop-strict:    ~"only screen and (min-width: @{screen-md-min}) and (max-width: @{screen-md-max})";
    @large:             ~"only screen and (min-width: @{screen-lg-min})";
    
    footer{
        width: 100%;
        @media @tablet {
            width: 768px;
        }
        @media @desktop {
            width: 940px;
        }
    }
    
    0 讨论(0)
  • 2020-12-04 05:52

    And this is what I have used for my project:

        @mobile:   ~"only screen and (min-width: 320px) and (max-width: 767px)";
        @tablet:    ~"only screen and (min-width: 768px) and (max-width: 991px)";
        @ipad:    ~"only screen and (min-width: 992px) and (max-width: 1024px)";
    
        @media @mobile {
          .banner{
            width: auto;
          }
        }
    
        @media @tablet {
          .banner{
            width: 720px;
          }
        }
    
      @media @ipad {
          .banner{
            width: 920px;
          }
        }
    
    0 讨论(0)
  • 2020-12-04 05:54
    @highdensity:  ~"only screen and (-webkit-min-device-pixel-ratio: 1.5)",
                   ~"only screen and (min--moz-device-pixel-ratio: 1.5)",
                   ~"only screen and (-o-min-device-pixel-ratio: 3/2)",
                   ~"only screen and (min-device-pixel-ratio: 1.5)";
    
    @mobile:        ~"only screen and (max-width: 750px)";
    @tab:       ~"only screen and (min-width: 751px) and (max-width: 900px)";
    @regular:        ~"only screen and (min-width: 901px) and (max-width: 1280px)";
    @extra-large:  ~"only screen and (min-width: 1281px)";
    
    0 讨论(0)
  • 2020-12-04 05:58

    Here is what I've done in my projects:

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

    This allows you to only define your media queries once and you can use it throughout your less files. Also a little easier to read. :)

    0 讨论(0)
  • 2020-12-04 05:59

    We use a setup like this:

    @vp_desktop:    801px;
    @vp_tablet:     800px;
    @vp_mobile:     400px;
    
    .OnDesktop(@rules) { @media screen and (min-width:@vp_desktop){ @rules(); } };
    .OnTablet(@rules) { @media screen and (max-width:@vp_tablet){ @rules(); } };
    .OnMobile(@rules) { @media screen and (max-width:@vp_mobile){ @rules(); } };
    

    You only need to set variables, the mixins handle the rest so it's very easy to maintain yet still flexible:

    div {
      display: inline-block;
      .OnTablet({
        display: block;
      });
    }
    

    It is worth mentioning that while this technique is very easy, if used badly your CSS output will be full of media queries. I try to limit my media queries to 1 per-breakpoint, per-file. Where a file would be header.less or search.less.

    N.B. This method probably won't compile unless you're using the javascript compiler.

    0 讨论(0)
  • 2020-12-04 06:01

    And you can also combine media queries like this

    @media @desktop, @tablet, @ipad{ 
    
    //common styles... 
    
    }
    
    0 讨论(0)
提交回复
热议问题