Skip to content

Instantly share code, notes, and snippets.

@davidpollet
Created October 12, 2017 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidpollet/ccb1e28e595687f3b51b337964e5edd6 to your computer and use it in GitHub Desktop.
Save davidpollet/ccb1e28e595687f3b51b337964e5edd6 to your computer and use it in GitHub Desktop.
Sass Mixin - Media queries generator
// Demo & Documentation : https://codepen.io/davidpollet/pen/QKbQzB
$base-font-size: 16px !default;
// Mobile first media queries
@mixin respond-from($value, $respond-direction: width, $orientation: null) {
// Convert pixel in em
$value-em: ($value / $base-font-size) + 0em;
@if $orientation !=null {
@media (min-#{$respond-direction}: $value-em) and (orientation: $orientation) {
@content;
}
} @else {
@media (min-#{$respond-direction}: $value-em) {
@content;
}
// Prevent Error
@if $respond-direction !="width" and $respond-direction !="height" {
@error "Wrong direction : must be width or height";
}
@elseif $orientation != null and $orientation !="portrait" and $orientation !="landscape" {
@error "Wrong orientation : must be portrait or landscape";
}
}
}
// Desktop first media queries
@mixin respond-until($value, $respond-direction: width, $orientation: null) {
// Convert pixel in em
$value-em: ($value / $base-font-size) + 0em;
@if $orientation !=null {
@media (max-#{$respond-direction}: $value-em) and (orientation: $orientation) {
@content;
}
} @else {
@media (max-#{$respond-direction}: $value-em) {
@content;
}
}
// Prevent error
@if $respond-direction !="width" and $respond-direction !="height" {
@error "Wrong direction : must be width or height";
}
@elseif $orientation != null and $orientation !="portrait" and $orientation !="landscape" {
@error "Wrong orientation : must be portrait or landscape";
}
}
// two media queries
@mixin respond-between($value1, $value2, $respond-direction: width, $orientation: null) {
$value1-em: ($value1 / $base-font-size) + 0em;
$value2-em: ($value2 / $base-font-size) + 0em;
@if $orientation !=null {
@media (min-#{$respond-direction}: $value1-em) and (max-#{$respond-direction}: $value2-em) and (orientation: $orientation) {
@content;
}
} @else {
@media (min-#{$respond-direction}: $value1-em) and (max-#{$respond-direction}: $value2-em) {
@content;
}
}
// Prevent error
@if $respond-direction !="width" and $respond-direction !="height" {
@error "Wrong direction : must be width or height";
}
@elseif $orientation != null and $orientation !="portrait" and $orientation !="landscape" {
@error "Wrong orientation : must be portrait or landscape";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment