Skip to content

Instantly share code, notes, and snippets.

@chitru
Created October 24, 2023 06:17
Show Gist options
  • Select an option

  • Save chitru/9015c09ce89d84b9ab32c98518959f5e to your computer and use it in GitHub Desktop.

Select an option

Save chitru/9015c09ce89d84b9ab32c98518959f5e to your computer and use it in GitHub Desktop.
Creating tainwindcss like margin and padding with SCSS
/*
How to use:
m for margin: m-1, m-2, m-3, m-4, m-5
ml for margin-left: ml-1, ml-2, ml-3, ml-4, ml-5
mr from margin-right: mr-1, mr-2, mr-3, mr-4, mr-5
mt for margin-top: mt-1, mt-2, mt-3, mt-4, mt-5
mb for margin-bottom: mb-1, mb-2, mb-3, mb-4, mb-5
p for padding: p-1, p-2, p-3, p-4, p-5
pl for padding-left: pl-1, pl-2, pl-3, pl-4, pl-5
pr for padding-right: pr-1, pr-2, pr-3, pr-4, pr-5
pt for padding-top: pt-1, pt-2, pt-3, pt-4, pt-5
pb for padding-bottom: pb-1, pb-2, pb-3, pb-4, pb-5
mx for margin-left and margin-right: mx-1, mx-2, mx-3, mx-4, mx-5
my for margin-top and margin-bottom: my-1, my-2, my-3, my-4, my-5
px for padding-left and padding-right: px-1, px-2, px-3, px-4, px-5
py for padding-top and padding-bottom: py-1, py-2, py-3, py-4, py-5
*/
// Default spacing
$spacer: 14px !default;
// Spacing scale
$spacers: (
0: 0,
1: $spacer * 0.25,
2: $spacer * 0.5,
3: $spacer,
4: $spacer * 1.5,
5: $spacer * 3,
6: $spacer * 4,
7: $spacer * 5,
8: $spacer * 6,
9: $spacer * 7,
10: $spacer * 8,
auto: auto,
) !default;
@each $key, $value in $spacers {
// generate m-* classes
.m-#{$key} {
margin: #{$value} !important;
}
}
@each $key, $value in $spacers {
// generate m-* classes
.m-#{$key} {
margin: #{$value} !important;
}
// generate p-* classes
.p-#{$key} {
padding: #{$value} !important;
}
}
@each $key, $value in $spacers {
// generate m-* classes
.m-#{$key} {
margin: #{$value} !important;
}
// generate p-* classes excluding key = auto
@if $key != auto {
.p-#{$key} {
padding: #{$value} !important;
}
}
}
$sides: (top, bottom, left, right);
@each $key, $value in $spacers {
@each $side in $sides {
// generate m* classes
.m#{str-slice($side, 0, 1)}-#{$key} {
margin-#{$side}: #{$value} !important;
}
}
}
@each $key, $value in $spacers {
@each $side in $sides {
// generate m* classes
.m#{str-slice($side, 0, 1)}-#{$key} {
margin-#{$side}: #{$value} !important;
}
// generate p* classes excluding key = auto
@if $key != auto {
.p#{str-slice($side, 0, 1)}-#{$key} {
padding-#{$side}: #{$value} !important;
}
}
}
}
$axises: (x, y);
@each $key, $value in $spacers {
@each $axis in $axises {
@if $axis == x {
// generate classes for x axis
// generate mx-* classes
.m#{$axis}-#{$key} {
margin-left: #{$value} !important;
margin-right: #{$value} !important;
}
// generate px-* classes excluding key = auto
@if $key != auto {
.p#{$axis}-#{$key} {
padding-left: #{$value} !important;
padding-right: #{$value} !important;
}
}
} @else if $axis == y {
// generate classes for y axis
// generate my-* classes
.m#{$axis}-#{$key} {
margin-top: #{$value} !important;
margin-bottom: #{$value} !important;
}
// generate py-* classes excluding key = auto
@if $key != auto {
.p#{$axis}-#{$key} {
padding-top: #{$value} !important;
padding-bottom: #{$value} !important;
}
}
} @else {
@error "Unknown axis #{$axis}.";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment