Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sass 101 - A newb's guide: Selector inheritance w/silent @extend

Sometimes you’ll write styles for a class that you only ever want to @extend, and never want to use directly in your HTML. This is especially true when writing a Sass library, where you may provide styles for users to @extend if they need and ignore if they don’t.

If you use normal classes for this, you end up creating a lot of extra CSS when the stylesheets are generated, and run the risk of colliding with other classes that are being used in the HTML. That’s why Sass supports “placeholder selectors” (for example, %foo).

Placeholder selectors look like class and id selectors, except the # or . is replaced by %. They can be used anywhere a class or id could, and on their own they prevent rulesets from being rendered to CSS.

selector_inheritance_w_silent_extend.scss

For more information and examples as to how the @extend directive works, be sure to read Sass Extends: be aware of the loop.

SassMeister Gist

%large-type {
font-size: 3.83333em;
line-height: 1.17391em;
margin-bottom: 0.3913em;
color: #333333;
}
h1 {
@extend %large-type;
}
h2 {
@extend %large-type;
font-size: 2.66667em;
}
.big-letters {
@extend %large-type;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment