Last active
August 29, 2015 14:15
-
-
Save jasonkmccoy/408b7c7ca356899f1045 to your computer and use it in GitHub Desktop.
Sass positioning mixin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.element { | |
top: 0; | |
left: 1em; | |
position: relative; | |
} | |
.other-element { | |
top: 1em; | |
right: 10%; | |
position: fixed; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=position($position, $args) | |
@each $o in top right bottom left | |
$i: index($args, $o) | |
@if $i and $i + 1 <= length($args) and type-of(nth($args, $i + 1)) == number | |
#{$o}: nth($args, $i + 1) | |
position: $position | |
=absolute($args) | |
+position(absolute, $args) | |
=fixed($args) | |
+position(fixed, $args) | |
=relative($args) | |
+position(relative, $args) | |
// Usage | |
.element | |
+relative(top 0 left 1em) | |
.other-element | |
+fixed(top 1em left "WAT? A STRING?!" right 10% bottom) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* SCSS code */ | |
@mixin position($position, $args) { | |
@each $o in top right bottom left { | |
$i: index($args, $o); | |
@if $i and $i + 1<= length($args) and type-of(nth($args, $i + 1)) == number { | |
#{$o}: nth($args, $i + 1); | |
} | |
} | |
position: $position; | |
} | |
@mixin absolute($args) { | |
@include position(absolute, $args); | |
} | |
@mixin fixed($args) { | |
@include position(fixed, $args); | |
} | |
@mixin relative($args) { | |
@include position(relative, $args); | |
} | |
/* Example Usage */ | |
.element { | |
@include relative(top 0 left 1em); | |
} | |
.other-element { | |
@include fixed(top 1em left "WAT? A STRING?!" right 10% bottom) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit: http://www.sitepoint.com/sass-mixins-kickstart-project/