Skip to content

Instantly share code, notes, and snippets.

@CodeRecipez
Last active June 5, 2019 16:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CodeRecipez/5264442 to your computer and use it in GitHub Desktop.
Save CodeRecipez/5264442 to your computer and use it in GitHub Desktop.
Sass 101 - A newb's guide: Operations range of numbers

The @for directive repeatedly outputs a set of styles. For each repetition, a counter variable is used to adjust the output. The directive has two forms: @for $var from <start> through <end> and @for $var from <start> to <end>.

Note the difference in the keywords through and to. $var can be any variable name, like $i; <start> and <end> are SassScript expressions that should return integers.

The @for statement sets $var to each successive number in the specified range and each time outputs the nested styles using that value of $var. For the form from <start> through <end>, the range includes the values of <start> and <end>, but the form from <start> to <end> runs up to but not including the value of <end>.

from <start> through <end>

$cols: 2;

@for $i from 1 through $cols {
  .grid_#{$i}of#{$cols} {
    @include grid($i, $grid_context: $cols);
  }
}

from <start> to <end>

$cols: 2;

@for $i from 1 to $cols {
  .grid_#{$i}of#{$cols} {
    @include grid($i, $grid_context: $cols);
  }
}

Note: The grid mixin is being leveraged from the Stipe Compass extension for illustration of this exercise.

SassMeister Gist

// Stipe v0.0.5.7.4
$cols: 2;
@for $i from 1 through $cols {
.grid_#{$i}of#{$cols} {
@include grid($i, $grid_context: $cols);
}
}
// Update line 5 from 'through' to 'to' to see the difference in the CSS output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment