Skip to content

Instantly share code, notes, and snippets.

@lunelson
Created May 6, 2013 09:43
Show Gist options
  • Save lunelson/5524240 to your computer and use it in GitHub Desktop.
Save lunelson/5524240 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com, the Sass playground.
// automatic adjacent selector generation
$heading-blocks: h1 h2 h3 h4 h5 h6;
$other-blocks: p, ul, ol;
@mixin adjacent($list:()) {
@if $list == () { // self-adjacent, if no list provided
& + & { @content; }
} @else { // takes space or comma separated list of single elements
$arr: unquote("#{nth($list, 1)} + &");
@for $n from 2 through length($list) {
$arr: append($arr, unquote(", #{nth($list,$n)} + &"));
}
#{$arr} { @content; }
}
}
.test {
@include adjacent {
color: blue;
}
@include adjacent($heading-blocks) {
color: green;
}
@include adjacent($other-blocks) {
color: red;
}
}
.test + .test {
color: blue;
}
h1 + .test, h2 + .test, h3 + .test, h4 + .test, h5 + .test, h6 + .test {
color: green;
}
p + .test, ul + .test, ol + .test {
color: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment