Fancy Media Queries with some LESS Magic
CssResponsive DesignLessMedia QueriesCss Problem Overview
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: 100%;
}
.tablet {
footer {
width: 768px;
}
}
.desktop {
footer {
width: 940px;
}
}
At the end something like this should be the result:
footer {
width: 100%;
}
@media screen and (min-width: 768px) {
footer {
width: 768px;
}
}
@media screen and (min-width: 992px) {
footer {
width: 940px;
}
}
.tablet could look somehow like this:
@media screen and (min-width: 768px) {
.tablet {
}
}
Hope somebody has a nice idea!
Css Solutions
Solution 1 - Css
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. :)
Solution 2 - Css
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).
Solution 3 - Css
+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;
}
}
Solution 4 - Css
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...
}
Solution 5 - Css
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.
Solution 6 - Css
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;
}
}
Solution 7 - Css
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;
}
}
Solution 8 - Css
@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)";