Skip to content

Instantly share code, notes, and snippets.

@jasonkmccoy
Last active August 29, 2015 14:15
Show Gist options
  • Save jasonkmccoy/408b7c7ca356899f1045 to your computer and use it in GitHub Desktop.
Save jasonkmccoy/408b7c7ca356899f1045 to your computer and use it in GitHub Desktop.
Sass positioning mixin
.element {
top: 0;
left: 1em;
position: relative;
}
.other-element {
top: 1em;
right: 10%;
position: fixed;
}
=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)
/* 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)
}
@jasonkmccoy
Copy link
Author

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