Reputation: 2743
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.
Upvotes: 14
Views: 14451
Reputation: 2007
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...
Upvotes: 12
Reputation: 4936
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;
}
}
Upvotes: 15