Skip to content

Instantly share code, notes, and snippets.

@brian-mcallister-lab49
Created October 30, 2020 20:51
Show Gist options
  • Save brian-mcallister-lab49/f8de1041bbd8c960c6d5b0b70c14580f to your computer and use it in GitHub Desktop.
Save brian-mcallister-lab49/f8de1041bbd8c960c6d5b0b70c14580f to your computer and use it in GitHub Desktop.
TWL-Lab49/Controlling scope in nested Sass selectors.

There's a few tricks that you can use to control scope when nesting Sass selectors. The best use case I've been able to find is when you're writing selectors with BEM.

.block {
  $this: &; // <-- Here's the trick.
  
  &--modified {
    #{$this}__nested-block {
      some: style;
    }
  }
}

...which will compile to:

.block--modified .block__nested-block { some: style; }

If you had instead written this as:

.block {  
  &--modified {
    &__nested-block {
      some: style;
    }
  }
}

...you would end up with:

.block--modified__nested-block { some: style; }

which is probably not what you intended.

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