Vannkorn

Full Stack Web Developer

Close

Multiple media queries in SASS

When it comes to screen size logic SASS plays a big role in tidying up functions with conditional logic. Here’s an example to use that logic. Usage:

Ads: Register now via this link to receive $300 credit from Vultr

When it comes to screen size logic SASS plays a big role in tidying up functions with conditional logic. Here’s an example to use that logic.

@mixin phone {
  @media /*conditions*/ {
    @content
  }
}

@mixin tablet {
  @media /*conditions*/ {
    @content
  }
}

@mixin desktop {
  @media /*conditions*/ {
    @content
  }
}

@mixin media($keys...) {
  @each $key in $keys {
    @if ($key == phone) {
      @include phone {
        @content
      }
    } @else if ($key == tablet) {
      @include tablet {
        @content
      }
    } @else if ($key == desktop) {
      @include desktop {
        @content
      }
    }
  }
}

Usage:

@include media(phone, tablet, desktop) {
  // your scss code
}

@include media(tablet, desktop) {
  // your scss code
}

@include media(phone) {
  // your scss code
}

// and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *