Skip to content

Instantly share code, notes, and snippets.

@niaal
Created January 8, 2015 02:40
Show Gist options
  • Save niaal/741f948ed707221bcd6c to your computer and use it in GitHub Desktop.
Save niaal/741f948ed707221bcd6c to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.4.9)
// Compass (v1.0.1)
// ----
// mixin will accept:
// - min AND max pixel value, comma separated
// - min OR max pixel value, comma separated, with 0 replacing the open-ended value
// - single string value for predetermined breakpoints
@mixin breakpoint($min: 0, $max: 0) {
// check to see if first variable is string or number
$type: type-of($min);
// if it's a string, run predefined breakpoint declarations
@if $type == string {
@if $min == x-small { @media (max-width: 479px) { @content; } }
@else if $min == small { @media (min-width: 480px) { @content; } }
@else if $min == medium { @media (min-width: 768px) { @content; } }
@else if $min == large { @media (min-width: 960px) { @content; } }
@else if $min == x-large { @media (min-width: 1280px) { @content; } }
@else {
@warn "Check your variable name - must be one of x-small, small, medium, large, x-large";
}
}
// if it's a number, check if min, max or both are required. write new breakpoint using these boundaries.
// default to 'all' if nothing specified.
@else if $type == number {
$query: "all" !default;
@if $min != 0 and $max != 0 { $query: "(min-width: #{$min}) and (max-width: #{$max})"; } // set both min and max
@else if $min != 0 and $max == 0 { $query: "(min-width: #{$min})"; } // set just min
@else if $min == 0 and $max != 0 { $query: "(max-width: #{$max})"; } // set just max
@media #{$query} { @content; }
}
}
// example usage...
div {
@include breakpoint(320px, 480px) {
color: magenta;
}
@include breakpoint(768px, 0) {
color: lime;
}
@include breakpoint(0, 767px) {
color: goldenrod;
}
@include breakpoint(large) {
color: plum;
}
}
@media (min-width: 320px) and (max-width: 480px) {
div {
color: magenta;
}
}
@media (min-width: 768px) {
div {
color: lime;
}
}
@media (max-width: 767px) {
div {
color: goldenrod;
}
}
@media (min-width: 960px) {
div {
color: plum;
}
}
@jackarmley
Copy link

@niaal Like this man! Reckon you may be able to roll some o' that @if $type == string logic into a function that calls a map, so you can store your predefined breaks in a easy-to-edit format. Seems like it will work really well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment