Skip to content

Instantly share code, notes, and snippets.

@jbrz0
Last active April 17, 2020 18:15
Show Gist options
  • Save jbrz0/705a6dd97e53ba04307e401b66204de7 to your computer and use it in GitHub Desktop.
Save jbrz0/705a6dd97e53ba04307e401b66204de7 to your computer and use it in GitHub Desktop.
Sass mixin for quickly adding padding and margins
//Padding mixin
@mixin padding($top, $right, $bottom, $left) {
padding-top: $top;
padding-right: $right;
padding-bottom: $bottom;
padding-left: $left;
}
//Margin mixin
@mixin margin($top, $right, $bottom, $left) {
margin-top: $top;
margin-right: $right;
margin-bottom: $bottom;
margin-left: $left;
}
//Usage definition
@include padding(top, right, bottom, left);
@include margin(top, right, bottom, left);
//Usage 1
@include padding(1px, 2px, 3px, 4px,);
@include margin(1px, 2px, 3px, 4px);
//Output 1
// padding: 1px 2px 3px 4px;
// margin: 1px 2px 3px 4px;
//Usage 2 (with null properties)
@include padding(1px, null, 3px, 4px);
@include margin(1px, 2px, null, 4px);
//Output 2
// padding-top: 1px;
// padding-bottom: 3px;
// padding-left: 4px;
// margin-top: 1px;
// margin-right: 2px;
// margin-left: 4px;
@ganar
Copy link

ganar commented Jan 23, 2018

This code is too verbose: I would advice you to write margin and padding like so:

margin: $top $right $bottom $left
padding: $top $right $bottom $left

and for that you don't need a mixin…

@vbaimas
Copy link

vbaimas commented Aug 27, 2018

@include padding(1px, 2px, 3px, 4px,); should be @include padding(1px, 2px, 3px, 4px);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment