Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Last active June 6, 2018 08:51
Show Gist options
  • Save andreasvirkus/5ab5a6bf482ec20b1da32fbed6961a49 to your computer and use it in GitHub Desktop.
Save andreasvirkus/5ab5a6bf482ec20b1da32fbed6961a49 to your computer and use it in GitHub Desktop.
A loop to generate util classes for paddings and margins
// ----
// Sass (v3.4.25)
// Compass (v1.0.3)
// ----
$spaceamounts: (auto, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 80, 100);
$sides: (
t: top,
b: bottom,
l: left,
r: right,
a: all,
s: sides
);
@each $prefix, $side in $sides {
@each $space in $spaceamounts {
$unit: if($space == 0 or $space == auto, $space, #{$space}px);
$property: if($prefix == '', '', -#{$side});
@if $prefix == a {
.m-#{$space} {
margin: $unit;
}
@if $unit != auto {
.p-#{$space} {
padding: $unit;
}
}
} @else if $prefix == s {
.m-#{$prefix}-#{$space} {
margin-left: $unit;
margin-right: $unit;
}
@if $unit != auto {
.p-#{$prefix}-#{$space} {
padding-left: $unit;
padding-right: $unit;
}
}
} @else {
@if $space == auto {
.m-#{$prefix}-a {
margin-#{$side}: auto;
}
} @else if $unit != auto {
.m-#{$prefix}-#{$space} {
margin-#{$side}: $unit;
}
.p-#{$prefix}-#{$space} {
padding-#{$side}: $unit;
}
}
}
}
}
// Create classes to break out of wrapper gutters via negative margins
.nm {
@for $i from 1 through 4 {
$level: #{$i * 10};
$unit: -#{$level}px;
@each $prefix, $side in $sides {
@if $prefix == a {
&-#{$level} {
margin: $unit;
}
} @else if $prefix == s {
&-#{$prefix}-#{$level} {
margin-left: $unit;
margin-right: $unit;
}
} @else {
&-#{$prefix}-#{$level} {
margin-#{$side}: $unit;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment