Created
October 12, 2017 11:53
-
-
Save davidpollet/ccb1e28e595687f3b51b337964e5edd6 to your computer and use it in GitHub Desktop.
Sass Mixin - Media queries generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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