Skip to content

Instantly share code, notes, and snippets.

@bugzbrown
Last active December 6, 2019 13:58
Show Gist options
  • Save bugzbrown/8b8d242ed4a6858810c8fab89b5a7d65 to your computer and use it in GitHub Desktop.
Save bugzbrown/8b8d242ed4a6858810c8fab89b5a7d65 to your computer and use it in GitHub Desktop.
SCSS Breakpoint Mixin
@mixin respond-to($breakpoint) {
// If the key exists in the map
@if map-has-key($breakpoints, $breakpoint) {
// Prints a media query based on the value
@media #{inspect(map-get($breakpoints, $breakpoint))} {
@content;
}
}
// If the key doesn't exist in the map
@else {
@warn "Unfortunately, no value could be retrieved from `#{$breakpoint}`. "
+ "Available breakpoints are: #{map-keys($breakpoints)}.";
}
}
/** BREAK POINTS FOR MEDIA QUERIES mobile first **/
$breakpoints: (
'tiny': only screen and ( max-width: 375px ),
'small': only screen and ( min-width: 415px ),
'medium': only screen and ( min-width: 768px ),
'large': only screen and ( min-width: 1024px ),
'xlarge': only screen and ( min-width: 1281px ),
) !default;
@bugzbrown
Copy link
Author

To use this, import it into your SCSS file and then whenever you want to add a breakpoint do as follows:

/** import the mixin **/
@import "breakpoints";
/** Mobile version of element **/
.element{
  width:100%;
  /** Add a breakpoint to width for large (min-width:1024px) displays **/
  respond-to('large'){
    width: 50%;
  }
}

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