Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alexiscordova
Created July 6, 2016 17:43
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 alexiscordova/f111ff895b30dc85108c3ee5819ba891 to your computer and use it in GitHub Desktop.
Save alexiscordova/f111ff895b30dc85108c3ee5819ba891 to your computer and use it in GitHub Desktop.
Sass mixins to make CSS nth-child selectors easier to work with
// Select the first nth elements
@mixin first($number) {
@if $number == 1 {
&:first-child {
@content;
}
}
@else {
&:nth-child(-n + ${$number}) {
@content;
}
}
}
// Select the last nth elements
@mixin last($number) {
&:nth-child(-n + #{$number}) {
@content;
}
}
// Select all after nth element
@mixin after($number) {
&:nth-child(n + ${$number + 1}) {
@content;
}
}
// Select every nth element
@mixin every($number) {
&:nth-child(#{$number}n)) {
@content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment