87

使用 less 在一些 css 类中包装不同分辨率的 css 样式会很好。

我想做类似的事情:

footer {
  width: 100%;
}

.tablet {
  footer {
    width: 768px;
  }
}

.desktop {
  footer {
    width: 940px;
  }
}

最后应该是这样的结果:

footer {
  width: 100%;
}

@media screen and (min-width: 768px) {
  footer {
    width: 768px;
  }
}

@media screen and (min-width: 992px) {
  footer {
    width: 940px;
  }
}

.tablet 可能看起来像这样:

@media screen and (min-width: 768px) {
  .tablet {

  }
}

希望有人有一个好主意!

4

8 回答 8

245

这是我在项目中所做的:

@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;
  }
}

这允许您只定义一次媒体查询,并且可以在您的 less 文件中使用它。也更容易阅读。:)

于 2013-04-05T19:32:34.410 回答
121

我完全同意 Hai Nguyen 的回答(已被接受),但您可以通过执行以下操作进一步清理它:

@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;
  }
}

它本质上是相同的,但允许您将媒体查询嵌套在原始选择器中。它将特定元素的所有 css 保存在一起,并使您的样式更加模块化(与拆分断点方法相比)。

于 2013-11-19T03:27:42.913 回答
46

+1 为 Nguyen 和 Yancey - 再加上一个。

如果您想要明确定义宽度,您可以使用string interpolation断点和变量来完成此操作。这里以引导程序为例 - 严格的规则是为了防止定义重叠。

@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;
    }
}
于 2014-10-17T20:02:26.120 回答
11

你也可以像这样组合媒体查询

@media @desktop, @tablet, @ipad{ 

//common styles... 

}
于 2015-01-07T04:24:12.260 回答
7

我们使用这样的设置:

@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(); } };

你只需要设置变量,mixin 处理其余的,所以它很容易维护但仍然很灵活:

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

值得一提的是,虽然这种技术非常简单,但如果使用不当,您的 CSS 输出将充满媒体查询。我尝试将媒体查询限制为每个断点、每个文件 1 个。文件在哪里是 header.less 或 search.less。

注意 除非您使用 javascript 编译器,否则此方法可能无法编译。

于 2015-11-02T15:23:32.130 回答
4

我正在使用这些 mixins 和变量

.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;

所以这

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

变成

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;
  }
}
于 2015-11-01T18:42:31.177 回答
0

这就是我在项目中使用的:

    @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;
      }
    }
于 2019-04-04T07:18:37.443 回答
0
@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)";
于 2019-08-10T09:58:36.953 回答