Search code examples
htmlcsssassgrid-layout

Can I use multiple values for one property in Sass?


Slightly related to my question here.

Say in Sass, if I wish to use multiple values for one property, is there a way that I can achieve this using a mixin?

NB: I know that my usage of @each is wrong but this helps to illustrate what I'm trying to do.

So far I have the following

@mixin grid-fractions($grid-type, $args) {
  @supports (display:grid) {
    display: grid;    
    #{$grid-type}: @each $args #{$args}fr;
  }
}

It's usage is as follows: @include('grid-template-rows', 2, 1, 1); And I'd like to get the mixin to output something like this:

@supports (display:grid) {
    display: grid;    
    grid-template-rows: 2fr 1fr 1fr;
  }

I know that $args lets you include multiple values in a mixin but it's obviously not working as I'm probably not using it correctly!


Solution

  • Why not just @include('grid-template-rows', '2fr 1fr 1fr'); ?

    Or

    Include a mixin and pass parameters as an arglist. It can be done different ways depending on what you want.

      @mixin grid($template-rows:null,$template-cols:null,$template-areas:null){
        @supports (display:grid) {
          display:grid;
          $grid-rows:();
          $grid-cols:();
          $grid-areas:();
    
          @each $rows in $template-rows{
            $grid-rows: append($grid-rows, #{$rows}fr);
          }
          @each $cols in $template-cols{
            $grid-cols: append($grid-cols, #{$cols}fr);
          }
          @each $areas in $template-areas{
            $grid-areas: append($grid-areas, #{$areas}fr);
          }
          grid-template-rows:#{$grid-rows};
          grid-template-columns:#{$grid-cols};
          grid-template-areas:#{$grid-areas};
        }
      }
    

     

      html{
        @include grid(1 2 3, 4 5 6, 7 8 9 );
      }
    

     

            /*
              // Outputs:
              @supports (display: grid) {
                html {
                  display:grid;
                  grid-template-rows: 1fr 2fr 3fr;
                  grid-template-columns: 4fr 5fr 6fr;
                  grid-template-areas: 7fr 8fr 9fr;
                }
              }
            */