0

I want to generate media queries grids having the below format:

I use a first large/desktop approach, with floats. It is intentional used instead of mobile-first approach.

@media (max-width: 36em) {
  .col-1@sm {
    width: 25%; }
  .col-2@sm {
    width: 50%; }
  .col-3@sm {
    width: 75%; }
  .col-4@sm {
    width: 100%; } 
}

starting from the following grid-config map:

$grid-config: (

        lg: (
                width: em(960px),
                columns: 16,
                gutter-horizontal: rem(8px)
        ),
        md: (
                width: em(768px),
                columns: 8,
                gutter-horizontal: rem(8px)
        ),
        sm: (
                width: em(568px),
                columns: 4,
                gutter-horizontal: rem(8px)
        ),
);

For the first element in the map(lg) I don't want to add a media query.

The first element can change, so I don't want to do a string check (if bp !=='lg') if possible(not like in my code)

I have the following mixin to generate media-query:

@mixin media-breakpoint($bp) {

  $columns: get-grid($bp, columns) !global;

  @if $bp != 'lg2' {
    @media (max-width: get-grid($bp, width)) {
      @content
    }
  } @else {
    @content
  }
}

and another mixin to generate grid:

@mixin grid-generator {
  @each $key, $value in $grid-config {
    $bp: $key !global;
    &--#{$key} {
      @content;
    }
  }

}

Then I use:

.col {
  @include grid-generator {
    @include media-breakpoint($bp) {
      $grid-columns: get-grid($bp, 'columns');
      @for $i from 1 through $grid-columns {
        &-#{$i} {
          width: 100%/$grid-columns * $i;
        }
      }
    }
  }
}

but this generates the following format col--sm-1 and not col-1@sm.

The problems I have:

  • keep the cols inside media queryset, and add the media at the end.
  • compare to first in grid-config dynamic, check if $bp == first in map, instead of lg
user3541631
  • 3,686
  • 8
  • 48
  • 115
  • Your outer loop prints both `.col*` and `sm` so you won't be able to insert a number generated by your inner loop in-between (I guess). Maybe with `@at-root` but this is overcomplicated; I'd invert those loops. Also `@` in a selector won't work, you must escape it: `.col-1\@sm {}` (see [Mathias Bynens on the subject](https://mathiasbynens.be/notes/css-escapes)) – FelipeAls Aug 27 '18 at 09:23
  • I know that I need to escap @, inverting the loops as there are doesn't keep the media query at first line, this is my issue – user3541631 Aug 27 '18 at 10:29

0 Answers0