Skip to content

Instantly share code, notes, and snippets.

@ezos86
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezos86/e2d83f41ca9c7d4de11f to your computer and use it in GitHub Desktop.
Save ezos86/e2d83f41ca9c7d4de11f to your computer and use it in GitHub Desktop.
Sass Margin/Padding Mixin
<div class="container">
<img src="http://sass-lang.com/assets/img/logos/logo-b6e1ef6e.svg" />
<h1>Sass Mixin for Margin/Padding and all your Frustrating space needs</h1>
<div class="box default">
<p>Default:<br> @include space();</p>
</div>
<div class="box padding-example">
<p>Padding Example:<br>@include space(padding, all, $large);</p>
</div>
<div class="box margin-example">
<p>Margin Example:<br>@include space(margin, all, $small);</p>
</div>
<div class="box hover-example">
<p>hover Example:<br>@include space(margin, all, $small);</p>
</div>
</div>

Sass Margin/Padding Mixin

This Sass mixin will help you eliminate frustrating padding-left, etc with a formula based padding/margin to help keep spacing also consistency.

A Pen by ezos86 on CodePen.

License.

@import "compass/css3";
$xs : 10px;
$small : 25px;
$medium : 50px;
$large : 75px;
$xlarge : 100px;
$xxlarge: 150px;
@mixin space($type:margin, $direction:all, $amount:$medium){
@if $type == padding {
@if $direction == all{
padding: $amount;
} @else if $direction == top {
padding-top:$amount
} @else if $direction == left {
padding-left:$amount
} @else if $direction == right {
padding-right:$amount
} @else {
padding-bottom:$amount
}
} @else {
@if $direction == all{
margin: $amount;
} @else if $direction == top {
margin-top:$amount
} @else if $direction == left {
margin-left:$amount
} @else if $direction == right {
margin-right:$amount
} @else {
margin-bottom:$amount
}
}
}
html{
background-color:#333;
}
.container{
margin:0 auto;
display:block;
text-align:center;
img{
width:20%;
@include space(margin, top, $small);
}
}
h1{
font-size:42px;
text-align:center;
width:80%;
margin-left:10%;
line-height:50px;
color:#fff;
}
.box{
background-color:#eee;
display:inline-block;
height:100px;
width:200px;
p{
line-height:20px;
}
}
//Start of box Examples
.default{
@include space();
}
.padding-example{
@include space(padding, all, $large);
}
.margin-example{
@include space(margin, top, $small);
}
.hover-example{
@include space(margin, top, $large);
@include transition();
&:hover{
@include space(padding, bottom, $large);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment