Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 39 ยท Variables, Nesting and Partials

Sass @extend and Placeholder Selectors

Learn how @extend shares rules between selectors, how %placeholder selectors avoid unused CSS, how @extend differs from mixins, and its limits and pitfalls.

Many components share a base look. Error and success messages are both boxes with padding and a border; primary and secondary buttons share spacing and typography. In plain CSS you either repeat the declarations or add several classes to the HTML. Sass offers a third way: @extend tells one selector to inherit the styles of another, and the compiler rewrites the CSS so the shared rule applies to both.

Your first @extend

SCSS
.message {
  padding: 12px 16px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
}

.message-error {
  @extend .message;
  border-color: #dc2626;
  color: #991b1b;
}

This compiles to:

CSS
.message, .message-error {
  padding: 12px 16px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
}
.message-error {
  border-color: #dc2626;
  color: #991b1b;
}

Sass did not copy the declarations. It added .message-error to the selector list of .message. The result is smaller CSS than a mixin would give, and the HTML needs only the class message-error.

Placeholder selectors

Sometimes the base rule is not a real class at all. You only wrote it to be extended, and you do not want it in the output on its own. A placeholder selector starts with % instead of a dot. It is invisible in the compiled CSS unless something extends it:

SCSS
%panel {
  padding: 16px;
  border-radius: 8px;
  background: white;
  box-shadow: 0 1px 3px rgb(0 0 0 / .15);
}

.card { @extend %panel; }
.modal { @extend %panel; padding: 32px; }

The output contains one rule for .card, .modal and no %panel at all. If nothing extends %panel, nothing is emitted, which keeps libraries lean.

Extend with a placeholder

Notice how the output has .msg { ... } as the only home of the shared declarations, and the three modifier classes only add what differs.

Extending is smarter than copying

@extend understands selectors, not just class names. If the extended selector appears inside other selectors, Sass updates those too:

SCSS
.error { color: red; }
.error a { text-decoration: underline; }

.fatal { @extend .error; }

Compiled, both rules get .fatal added: .error, .fatal { ... } and .error a, .fatal a { ... }. The subclass inherits the whole family of rules that mention the base. That is powerful, but it is also the source of surprises.

@extend vs @mixin

Both reuse code, but they work differently:

@extend @mixin + @include
Output Groups selectors, no duplicate declarations Copies declarations into every use
Arguments None Yes
Works with Selectors Declarations and rules
Cross media queries Limited Works anywhere
Compiled size Usually smaller Can grow quickly (but gzip helps a lot)
Predictability Selector order changes Output order is where you wrote it

Guidance that works well in practice:

  • Use a mixin when you need parameters, when the block lives inside a media query, or when you want predictable output.
  • Use @extend with a placeholder for a fixed set of shared base styles in the same context, such as the example above.
  • Do not extend regular component classes from unrelated parts of the codebase. It couples files together in ways that are hard to see.

Many style guides recommend mixins by default, because a repeated declaration block compresses extremely well under gzip, and @extend can change selector ordering and thus specificity or cascade results.

Limits and gotchas

  1. You cannot extend across @media. A rule inside @media (min-width: 600px) can extend only selectors that are also inside that same media query. Extending a top-level selector from within a media block is an error, since Sass cannot rewrite the outer rule.
  2. Order matters in the output. The extended rule stays where it was written. If .message-error overrides something and its base rule appears later in the stylesheet through another extension, the cascade can change.
  3. Selector explosion. Extending a selector that appears in complex combinations (.a .b .c, .x:not(.y)) can generate many combined selectors. Sass warns about nothing, but you will notice bloated output.
  4. Nested or compound selectors. @extend .a.b; is an error: extend one simple selector at a time, with multiple @extend lines if needed.

@extend !optional

Normally, extending something that does not exist is an error, which catches typos. Add !optional to silence it when the base may be absent (for example, in a library that works with or without a plugin):

SCSS
.notice { @extend .maybe-defined !optional; }

Extend chains

Extends can chain: if .b extends .a and .c extends .b, then .c gets everything from both. Sass resolves the chain for you, but long chains are hard to reason about. Keep them to two levels.

Extend vs mixin output

The first output lists both tags in one selector list over one set of declarations (Sass may reorder the list). The second repeats the three declarations twice. For a handful of uses the difference is trivial; across hundreds of uses @extend is shorter, and the next lesson on mixins shows when the flexibility of a mixin wins.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a placeholder selector like %panel do when nothing extends it?

    Placeholders exist only to be extended. Without an @extend they produce no output.
  2. 2. How does @extend change the compiled CSS?

    Sass rewrites selector lists (for example .a, .b) rather than copying declarations.
  3. 3. Which is a good reason to choose a mixin over @extend?

    @extend takes no arguments and cannot cross media queries. Mixins are parameterized and work anywhere.
  4. 4. What does the !optional flag on @extend do?

    Without it, extending a missing selector is an error. !optional makes that case silent.

Summary

  • @extend shares a rule by adding the extending selector to the base selector's list; no declarations are copied.
  • %placeholder selectors are never output by themselves, so they are ideal base styles.
  • Use @extend for simple, same-context sharing and mixins when you need arguments, media queries or predictable order.
  • You cannot extend across @media boundaries; use !optional for bases that may not exist.
  • Avoid long extend chains and extending unrelated components.

Next: Interpolation