Skip to content

Instantly share code, notes, and snippets.

@YozhEzhi
Forked from vestman/media-queries.less
Last active March 2, 2017 09:53
Show Gist options
  • Save YozhEzhi/a8a6a8241c56026734c7acf5773d0aa6 to your computer and use it in GitHub Desktop.
Save YozhEzhi/a8a6a8241c56026734c7acf5773d0aa6 to your computer and use it in GitHub Desktop.
Less: Mixin for building media queries
// Live example: https://goo.gl/xJblcx
.Q(@breaks; @rules;) {
// If there's only one breakpoint:
& when (length(@breaks) = 1) {
@query: ~"(min-width: @{breaks}px)";
@media screen and @query {@rules();};
}
// If there's two breakpoints:
& when (length(@breaks) = 2) {
@min: extract(@breaks, 1);
@max: extract(@breaks, 2);
// If first breakpoint is not number, we build just a max-width query:
& when not (isnumber(@min)) {
@maxWidth: (@max - 1);
@query: ~"(max-width: @{maxWidth}px)";
@media screen and @query {@rules();};
}
// If the two breakpoints are specified,
// then we build a min-width AND max-width:
& when (isnumber(@min)) {
@query: ~"(min-width: @{min}px) and (max-width: @{max}px)";
@media screen and @query {@rules();};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment