Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Use Multiple @include in SCSS?

I want to use multiple include in the same SCSS. For example:

.section-ptb {
    padding-top: 130px;
    padding-bottom: 130px;
    @include desktop {
        padding-top: 80px;
        padding-bottom: 80px;
    }
    @include tablet {
        padding-top: 80px;
        padding-bottom: 80px;
    }
    @include mobole {
        padding-top: 80px;
        padding-bottom: 80px;
    }
}

it's very Boring to Multiple @include frequently. Have Any Way to Reduce The Code, I want to use:

.section-ptb {
    padding-top: 130px;
    padding-bottom: 130px;
    @include desktop , @include tablet, @include mobole {
        padding-top: 80px;
        padding-bottom: 80px;
    }
}

But It's Not Valid SCSS. Please Tell Me Another Way To Reduce The Code.

like image 485
Rubel Hossain Avatar asked Apr 27 '19 15:04

Rubel Hossain


2 Answers

As mentioned by @karthick dynamic includes are not supported (yet). In your case I think it would make sense to have a single mixin to handle all media queries – like:

SCSS

//  map holding breakpoint values
$breakpoints: (
  mobile : 0px, 
  tablet : 680px, 
  desktop: 960px
);

//  mixin to print out media queries (based on map keys passed) 
@mixin media($keys...){
  @each $key in $keys { 
    @media (min-width: map-get($breakpoints, $key)){
      @content
    } 
  }
}



.section-ptb {
  padding-top: 130px;
  padding-bottom: 130px;

  //  pass the key(s) of the media queries you want to print  
  @include media(mobile, tablet, desktop){
    padding-top: 80px;
    padding-bottom: 80px;
  }
}

CSS Output

.section-ptb {
  padding-top: 130px;
  padding-bottom: 130px; 
}
@media (min-width: 0px) {
  .section-ptb {
    padding-top: 80px;
    padding-bottom: 80px; 
  } 
}
@media (min-width: 680px) {
  .section-ptb {
    padding-top: 80px;
    padding-bottom: 80px; 
  } 
}
@media (min-width: 960px) {
  .section-ptb {
    padding-top: 80px;
    padding-bottom: 80px; 
  } 
}

like image 102
Jakob E Avatar answered Oct 16 '22 16:10

Jakob E


You can use something like this:

SCSS

@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...
like image 24
EzioMercer Avatar answered Oct 16 '22 16:10

EzioMercer