Skip to content

Instantly share code, notes, and snippets.

@brookjordan
Created December 5, 2017 09:27
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 brookjordan/1d4ea60f37231c093fa5e09919d34cc9 to your computer and use it in GitHub Desktop.
Save brookjordan/1d4ea60f37231c093fa5e09919d34cc9 to your computer and use it in GitHub Desktop.
In order to just add an element to a selector in SCSS is infuriatingly difficult. This attempts to ease the pain.
//
// Usage:
//
// .my-class {
// // other styles…
//
// @include add-element(a) {
// color: blue;
// }
//
// // other styles…
// }
//
// As these either don't work, or don't have the desired effect:
//
// .my-class {
// // other styles…
//
// a& { // "&" may only be used at the beginning of a compound selector.
// color: blue;
// }
//
// a#{&} { // outputs: ".my-class a.my-class"
// color: blue;
// }
//
// // other styles…
// }
//
@mixin add-element($element) {
@at-root #{$element}#{&} {
@content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment