Sass @extend & Placeholders
@extend tells Sass: "this selector should get all the styles of that other selector". Instead of copying declarations like a mixin, Sass adds your selector to the other rule's selector list. The result is less repeated CSS.
Basic @extend
.message {
border: 1px solid #ccc;
padding: 10px 14px;
border-radius: 6px;
}
.message-success {
@extend .message;
border-color: #2e7d32;
color: #2e7d32;
}
.message-error {
@extend .message;
border-color: #c62828;
color: #c62828;
}.message, .message-error, .message-success {
border: 1px solid #ccc;
padding: 10px 14px;
border-radius: 6px;
}
.message-success {
border-color: #2e7d32;
color: #2e7d32;
}
.message-error {
border-color: #c62828;
color: #c62828;
}
Look at the first rule: .message, .message-error and .message-success share one block. In HTML you only need class="message-error" — not class="message message-error".
Placeholder selectors %
Often the base style should never be used on its own. A placeholder selector starts with %. It isn't output by itself — it only appears when something extends it:
%card-base {
background: white;
border-radius: 10px;
box-shadow: 0 2px 8px rgb(0 0 0 / 0.12);
padding: 16px;
}
.profile-card {
@extend %card-base;
text-align: center;
}
.product-card {
@extend %card-base;
display: grid;
gap: 8px;
}
%unused {
color: red; // never extended, so never output
}.product-card, .profile-card {
background: white;
border-radius: 10px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
padding: 16px;
}
.profile-card {
text-align: center;
}
.product-card {
display: grid;
gap: 8px;
}
Extend also rewrites related selectors
@extend works on every rule where the extended selector appears, including pseudo-classes and descendant selectors:
.link {
color: #cc6699;
}
.link:hover {
text-decoration: underline;
}
.nav-item {
@extend .link;
font-weight: 600;
}.link, .nav-item {
color: #cc6699;
}
.link:hover, .nav-item:hover {
text-decoration: underline;
}
.nav-item {
font-weight: 600;
}
Limitations and pitfalls
| Pitfall | Why |
|---|---|
Can't extend across @media boundaries |
Extending a selector defined outside the media query from inside one is an error |
| Selector explosions | Extending selectors that appear in many complex rules can produce huge selector lists |
| Order surprises | The shared styles are placed where the extended rule is, not where @extend is written |
| Only simple selectors | You can extend .a or %a, but not .a .b |
Note: Under the module system,
@extendonly affects rules in modules that your module loads (upstream), not unrelated files — which keeps it predictable.
Mixin or @extend?
- Use
@extendwith a placeholder when several classes are truly "a kind of" the same thing and share identical styles. - Use a mixin when you need arguments, or inside media queries.
- When in doubt, prefer mixins. They are more predictable, and many style guides recommend avoiding
@extendin large codebases.
Test yourself
What happens to a %placeholder rule that nothing extends?
- It is output as a class
- It is omitted from the CSS
- Compilation fails
- It becomes a comment
How does @extend differ from @include?
- @extend copies declarations; @include groups selectors
- @extend adds the selector to the existing rule; @include copies the declarations
- They are identical
- @extend only works with functions
Which of these can you NOT do with @extend?
- Extend a class
- Extend a placeholder
- Extend a selector defined outside the
@mediablock you're in - Extend from inside a nested rule