Skip to content

Instantly share code, notes, and snippets.

@jbrz0
Last active April 18, 2020 03:26
Show Gist options
  • Save jbrz0/6cce23d0fcf71bf57f4d01fba547fc43 to your computer and use it in GitHub Desktop.
Save jbrz0/6cce23d0fcf71bf57f4d01fba547fc43 to your computer and use it in GitHub Desktop.
Custom Sass mixin for using media queries
// Define the breakpoints
$breakpoint-small: 600px;
$breakpoint-med-small: 960px;
$breakpoint-med: 1175px;
@mixin screen($size, $type: max, $pixels: $breakpoint-small) {
@if $size == 'small' {
@media screen and ($type + -width: $breakpoint-small) {
@content;
}
}
@else if $size == 'med-small' {
@media screen and ($type + -width: $breakpoint-med-small) {
@content;
}
}
@else if $size == 'med' {
@media screen and ($type + -width: $breakpoint-med) {
@content;
}
}
@else if $size == 'large' {
@media screen and ($type + -width: $breakpoint-med) {
@content;
}
}
@else if $size == 'custom' {
@media screen and ($type + -width: $pixels + px) {
@content;
}
}
@else {
@content;
}
}
// Using the mixin
.foo {
@include screen(large) {
width: 20%;
}
@include screen(med) {
width: 40%;
}
@include screen(med-small) {
width: 60%;
}
@include screen(small) {
width: 80%;
}
@include screen(custom, max, 400) {
width: 100%;
}
}
@Mosharush
Copy link

Some better option:

$breakpoints: (
  'small'  : 767px,
  'medium' : 992px,
  'large'  : 1200px
);
@mixin screen($size, $type: max) {
  $width: map-get($breakpoints, $size);

  // If the key exists in the map
  @if $width == null {
    $width: $size;
  }

  @media screen and ($type + -width: $breakpoint-small) {
      @content;
  }
}

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