Sub topic 12
@extend and placeholder selectors
This chapter assumes you can write nested rules, variables and mixins in SCSS, and that you can compile a file with the Sass command line. It covers @extend, which lets one selector reuse another selector's styles. It also covers placeholder selectors and the problems that come with both.
What @extend does
A mixin copies declarations into every rule that includes it. @extend works differently. It finds the rule you extend and adds the extending selector to that rule's selector list. The declarations are written once, and the output stays small.
.message {
padding: 1rem;
border: 1px solid #999999;
&:hover {
opacity: 0.8;
}
}
.message--error {
@extend .message;
color: #cc3333;
}Compiled CSS:
.message, .message--error {
padding: 1rem;
border: 1px solid #999999;
}
.message:hover, .message--error:hover {
opacity: 0.8;
}
.message--error {
color: #cc3333;
}Sass rewrote every rule that mentions .message, including .message:hover, so .message--error gets the hover style as well. This is the main advantage of @extend over a mixin. It understands selectors, so the extending selector follows the extended one everywhere it appears.
In your markup, .message--error alone is now enough. You no longer need class="message message--error".
Placeholder selectors
Extending a real class has a side effect. The base class, such as .message, is also emitted as a normal rule, even when you only wanted it as a source of shared styles. A placeholder selector avoids this. It starts with % and is never written to the CSS unless something extends it.
%btn-base {
padding: 0.5rem 1rem;
border-radius: 4px;
}
.btn-primary {
@extend %btn-base;
background: #3366cc;
}
.btn-danger {
@extend %btn-base;
background: #cc3333;
}Compiled CSS:
.btn-primary, .btn-danger {
padding: 0.5rem 1rem;
border-radius: 4px;
}
.btn-primary {
background: #3366cc;
}
.btn-danger {
background: #cc3333;
}There is no %btn-base in the output. If you delete both @extend lines, the shared rule disappears from the CSS too. This makes placeholders a good fit for libraries, where you may define dozens of building blocks and ship only the ones that are used.
Where @extend goes wrong
@extend is simple on small examples. The problems appear when the stylesheet grows.
Source order changes the cascade
Sass writes the shared rule at the position of the extended rule, not at the position of the @extend. If the placeholder comes after the rules that extend it, the shared declarations arrive later and win ties.
.alert {
@extend %box;
padding: 2rem;
}
%box {
padding: 1rem;
}Compiled CSS:
.alert {
padding: 2rem;
}
.alert {
padding: 1rem;
}The browser applies padding: 1rem, even though you wrote 2rem in .alert. Define placeholders before the rules that extend them. This is one reason to keep placeholders in their own file that loads first.
Extending across @media fails
An @extend inside a @media block can only extend selectors declared inside that same block. Extending a placeholder from outside it is a compile error.
%card {
padding: 1rem;
}
@media (min-width: 48rem) {
.wide {
// Error: You may not @extend an outer selector from within @media.
@extend %card;
}
}Sass cannot make the outer rule apply only inside the media query. A mixin has no such limit, because it copies declarations to the spot where it is included.
Selector lists can multiply
When the extended selector appears inside complex selectors, Sass generates combinations. Suppose .a .b and .c .b both exist and you extend .b from several places. The output holds a selector for every combination that could match. A few extends are harmless. Heavily nested code with many extends can produce very long selector lists and files larger than the mixin version would have been.
Extend does not take arguments
A placeholder is a fixed block of CSS. You cannot pass it a color or a size. If two buttons need different padding, you need a mixin, or a placeholder for each variant.
Missing targets are errors
If the selector you extend does not exist, compilation fails. This catches typos. When a target is deliberately optional, add the !optional flag:
.badge {
@extend .maybe-defined !optional;
font-size: 0.75rem;
}Without a matching rule for .maybe-defined, this compiles with no error and nothing is extended.
Extend and the module system
When you split code with @use, an @extend only affects the stylesheet it is written in and the stylesheets that file loads through @use or @forward. It does not reach files that load it. This keeps extends predictable across modules. A typical layout puts the placeholders in a partial and loads it where it is needed:
// _placeholders.scss
%btn-base {
padding: 0.5rem 1rem;
border-radius: 4px;
}
// main.scss
@use 'placeholders';
.btn-primary {
@extend %btn-base;
background: #3366cc;
}Placeholders are not namespaced the way variables are, so you write %btn-base, not placeholders.%btn-base. The @use line only makes the placeholder visible to main.scss.
Choosing between @extend and a mixin
| Question | @extend with placeholder | Mixin |
|---|---|---|
| Output size | Declarations written once, selectors grouped | Declarations repeated in each rule |
| Arguments | None | Yes, with defaults |
| Works inside `@media` | Only for selectors in the same block | Yes |
| Cascade position | Moves to the extended rule's position | Stays where it is included |
| Risk | Order surprises, long selector lists | Larger CSS |
A workable default is to use a mixin unless you have a reason not to. Mixins behave predictably because they copy exactly what you wrote, exactly where you included it. Reach for @extend when the styles are static and identical across many selectors, and when you want the extension to follow the base rule's states, such as :hover and :focus. Gzip compression handles repeated declarations well, so the size saving from @extend is often smaller than it looks in the raw CSS.
Avoid extending a class that is also styled in other contexts, such as .message in a deeply nested selector. Extend placeholders instead, since they have exactly one meaning.
Common errors
- Extending from inside
@mediaa selector that was declared outside it. - Defining the placeholder after its users and then wondering why overrides lose.
- Extending a real class and getting an unwanted base rule in the output.
- Expecting
@extendto accept arguments or to copy declarations at the point of use. - Extending a selector that does not exist, which fails unless you add
!optional.
Exercise
A stylesheet defines .panel { margin: 1rem; } and later .panel-wide { margin: 2rem; @extend .panel; }. Which margin does .panel-wide get, and why? How would you fix it?
Show answer
It gets 1rem. The grouped rule .panel, .panel-wide { margin: 1rem; } is written at the position of .panel, which is before .panel-wide { margin: 2rem; } only if .panel was defined first. Here .panel comes first, so the grouped rule appears earlier and .panel-wide's own 2rem rule comes later and wins. The 1rem result occurs if the order is reversed, with the base defined after. Check the compiled output to see which rule is last. To make the result independent of order, define the base rule or placeholder before every rule that extends it, and keep overrides in the extending rule.
Exercise
You need a .card style whose padding differs by theme (1rem for compact, 2rem for roomy), and one variant must apply only at widths of 48rem and up. Should you use @extend or a mixin?
Show answer
Use a mixin. The padding varies, so you need an argument, which placeholders do not accept. The variant must also live inside @media, and @extend cannot reach a selector declared outside that block.