Skip to content

Instantly share code, notes, and snippets.

@timknight
Created May 5, 2014 12:48
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save timknight/03e6335b8816aa534cf7 to your computer and use it in GitHub Desktop.
Save timknight/03e6335b8816aa534cf7 to your computer and use it in GitHub Desktop.
A simple responsive breakpoint mixin that takes both attribute names and custom widths. See https://medium.com/p/889927b37740/
@mixin breakpoint($min: 0, $max: 0) {
$type: type-of($min);
@if $type == string {
@if $min == xs {
@media (max-width: 767px) { @content; } // Mobile Devices
}
@else if $min == sm {
@media (min-width: 768px) { @content; } // Tablet Devices
}
@else if $min == md {
@media (min-width: 992px) { @content; } // Desktops
}
@else if $min == lg {
@media (min-width: 1200px) { @content; } // Widescreen Desktops
}
// Otherwise pass a warning to the compiler as to the appropriate options
@else {
@warn "The breakpoint mixin supports the following attributes: xs, sm, md, lg";
}
}
@else if $type == number {
// Allow for custom parameters for min and max size
$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; }
}
}
@alienlebarge
Copy link

Great mixin ! Thx

@gebeer
Copy link

gebeer commented Jul 24, 2014

Thank you, this is great.
One suggestion:
When using it with bootstrap 3 you could substitute the px values for min-width and max-width with the default bs variables.
So the first part of the mixin would read

  @if $class == xs {
    @media (max-width: ($screen-sm - 1)) { @content; }
  }

  @else if $class == sm {
    @media (min-width: $screen-sm) { @content; }
  }

  @else if $class == md {
    @media (min-width: $screen-md) { @content; }
  }

  @else if $class == lg {
    @media (min-width: $screen-lg) { @content; }
  }

@timknight
Copy link
Author

If you've liked using this breakpoint mixin you might like my recent revision here which I talk about in my blog post.

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