Sub topic 12
@extend and placeholder selectors
This chapter assumes you can write SCSS rules, use nesting, and write mixins with @include. It covers @extend, placeholder selectors, and how to choose between @extend and a mixin.
What @extend does
@extend tells Sass that one selector should share the styles of another. Sass does not copy declarations into the new rule. Instead, it adds the extending selector to the selector list of the original rule.
.message {
padding: 1rem;
border: 1px solid #999;
}
.message-error {
@extend .message;
border-color: #cc3333;
}This compiles to:
.message,
.message-error {
padding: 1rem;
border: 1px solid #999;
}
.message-error {
border-color: #cc3333;
}In your HTML you only need class="message-error". The shared declarations are written once in the output.
Placeholder selectors
A placeholder selector starts with %. It exists only to be extended. Sass does not emit a rule for a placeholder unless something extends it. This avoids shipping a base class like .message that you never use on its own.
%btn-base {
padding: 0.5rem 1rem;
border-radius: 4px;
}
.btn-primary {
@extend %btn-base;
background: #3366cc;
}
.btn-danger {
@extend %btn-base;
background: #cc3333;
}The compiled CSS contains .btn-primary, .btn-danger with the shared padding and radius. The text %btn-base never appears in the output.
Gotchas
@extend rewrites selectors across the whole compiled stylesheet. This leads to a few problems:
- Output order. The shared rule appears where the placeholder is defined, not where the extend is written. If a later rule has the same specificity, it can override the shared styles in surprising ways.
- Selector growth. Extending a selector that is nested or used in many places can multiply selectors in the output.
- Media queries. You cannot extend a selector defined outside a
@mediablock from inside that block. Sass reports an error. - Scope. With the module system,
@extendonly affects code that your stylesheet loads with@useor@forward. Extending a placeholder you did not load has no effect on it.
@extend or mixin?
Both reduce repetition. They produce different output.
| Need | Prefer | Reason |
|---|---|---|
| Share a fixed set of declarations between related classes | `@extend` with a placeholder | Declarations appear once in the CSS |
| Pass arguments, such as a color or size | Mixin | `@extend` cannot take parameters |
| Use inside `@media` | Mixin | `@extend` cannot cross media boundaries |
| Keep output predictable and rules in source order | Mixin | A mixin copies declarations where you include it |
A mixin duplicates declarations in every rule that includes it. That increases the file size before compression, but gzip handles repeated text well. Many teams default to mixins and use placeholders only for small, stable groups of declarations.
Exercise
A teammate writes .alert { @extend %box; color: red; } but %box is never defined in any file the stylesheet loads. What does Sass do, and what would you do if you needed the box styles to take a $color argument?
Show answer
Sass reports an error because the extended selector does not exist. Define %box (or load the partial that defines it) to fix that. For a $color argument, use a mixin such as @mixin box($color), because @extend cannot take arguments.
Exercise
Why does the compiled CSS contain no %btn-base rule when you define a placeholder that nothing extends?
Show answer
Sass emits a placeholder's rule only through the selectors that extend it. With no extenders, the rule has no selector to output, so it is left out.