奇妙的媒体查询与一些较少的魔术

在一些 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;
}
}

。平板电脑可以看起来有点像这样:

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


}
}

希望有人有好主意!

67205 次浏览

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. :)

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

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

And you can also combine media queries like this

 @desktop:   ~"only screen and (min-width: 960px) and (max-width: 1199px)";
@tablet:    ~"only screen and (min-width: 720px) and (max-width: 959px)";


@media @desktop, @tablet, @ipad{
    

//common styles...
    

}

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

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.

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