TExcelTAD
Home › Sass › Sass @extend & Placeholders

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

scss · try it yourself
.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:

scss · try it yourself
%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:

scss · try it yourself
.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, @extend only affects rules in modules that your module loads (upstream), not unrelated files — which keeps it predictable.

Mixin or @extend?

  • Use @extend with 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 @extend in large codebases.

Test yourself

What happens to a %placeholder rule that nothing extends?

  1. It is output as a class
  2. It is omitted from the CSS
  3. Compilation fails
  4. It becomes a comment

How does @extend differ from @include?

  1. @extend copies declarations; @include groups selectors
  2. @extend adds the selector to the existing rule; @include copies the declarations
  3. They are identical
  4. @extend only works with functions

Which of these can you NOT do with @extend?

  1. Extend a class
  2. Extend a placeholder
  3. Extend a selector defined outside the @media block you're in
  4. Extend from inside a nested rule