Skip to main content
Inspiring
September 12, 2017
Answered

SCSS variables and operators

  • September 12, 2017
  • 1 reply
  • 2561 views

Hello.

I'm trying to organize a website using a responsive grid. I'm using a 12 column grid, and I need to create css classes for different column spans. For simplicity reasons, say both the column and gutter widths are 4%. This way I have 12 columns (12 x 4% = 48%) and 13 gutters (13 x 4% = 52%). The largest content will span 10 columns (and thus 9 inside gutters).

The classes I need are something like

$column-width: 4%;

$gutter-width: 4%;

.col-w1 {

     width: $column-width;

}

.col-w2 {

     width: $column-width * 2 + gutter-width;

}

(...)

.col-w10 {

     width: $column-width * 10 + gutter-width * 9;

}

So every class obeys the following rule: width = $column-width * n + $gutter-width * (n-1), where n is the number of columns.

Having read this great article on Advanced SCSS​,​ I could create these classes automatically by creating a function with arguments like the class prefix, the number of columns and the column and gutter width. Here's what I tried.

$column-width: 4%;

$gutter-width: 4%;

$colsnumbers1: 1 0, 2 1, 3 2, 4 3, 5 4, 6 5, 7 6, 8 7, 9 8, 10 9; // each pair is the number of columns and number of gutters

@mixin method1($prefix, $colwidth, $gutwidth, $colsnumbers1) {

     @each $i in $colsnumbers1 {

          .#{$prefix}#{nth($i, 1)} {

               width: $colwidth * nth($i, 1) + $gutwidth * nth($i, 2);

          }

     }

}

@include method1('col_w',

     $column-width,

     $gutter-width,

     $colsnumbers1

);

This works. However, having to create such a large $colsnumbers1 is not very practical. I tried to simplify it using something like this:

$colsnumbers2: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10; // number of columns. I imagine there's a better way to do this

@mixin method2($prefix, $colwidth, $gutwidth, $colsnumbers2) {

     @each $i in $colsnumbers2 {

          .#{$prefix}#{$i} { // this throws an error on the {$i} part

               width: $colw * $i + $gutw * ($i-1); // this throws an error on the ($i-1) part

          }

     }

}

@include method2('col_w',

     $column-width,

     $gutter-width,

     $colsnumbers2

);

So my questions are:

1. How to get .#($prefix)#($i) to output .col-w1, .col-w2... col-w10.

2. How to get $i-1 to work correctly.

I'm assuming what I want to achieve is possible. I appreciate all the help you can provide. Thanks.

This topic has been closed for replies.
Correct answer BenPleysier

Now that CSS GRID is widely supported, I would not go to the trouble of creating your own grid Have a look here for examples Realizing common layouts using CSS Grid Layout - CSS | MDN

There are ways to overcome the problems that you will get with browsers that are not grid-aware.

If you are interested in CSS GRID come back here for solutions to keep all browsers happy.

1 reply

BenPleysier
Community Expert
BenPleysierCommunity ExpertCorrect answer
Community Expert
September 12, 2017

Now that CSS GRID is widely supported, I would not go to the trouble of creating your own grid Have a look here for examples Realizing common layouts using CSS Grid Layout - CSS | MDN

There are ways to overcome the problems that you will get with browsers that are not grid-aware.

If you are interested in CSS GRID come back here for solutions to keep all browsers happy.

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
Inspiring
September 12, 2017

Thank you Ben. I will check out your suggestion.

In the meantime, I'm still trying to figure out if what I wish to do is doable, and how, because that will surely help me in future occasions.