Skip to content
tezvyn:
Menu

Sass @extend: Inherit Styles, Not HTML Classes

Source: sass-lang.comMediumHow cards are made

@extend lets one selector inherit styles from another, grouping them in the final CSS without adding classes to your HTML. Use it for modifier classes to keep markup clean. The footgun: overusing it can generate long, complex selectors, bloating your CSS.

Why it exists

Often, one component should be a variation of another, sharing most of its styles. The naive approach is to add multiple classes to the HTML, like class="alert alert-warning". This clutters markup, is error-prone, and mixes styling concerns into the HTML structure. @extend was created to solve this by establishing these relationships directly within the CSS.

The mental model

Think of @extend as telling Sass, "Wherever you see selector A, pretend you also see selector B." It doesn't copy style properties like a mixin does. Instead, it finds every style rule that targets selector A and adds selector B to its list, effectively grouping them in the final compiled CSS.

How it works

When you write .error--serious { @extend .error; }, Sass doesn't just copy the styles from .error. Instead, it finds every instance of .error in your stylesheet (including .error:hover or div > .error) and adds the corresponding version of .error--serious to the selector list. The input .error { color: red; } and .error--serious { @extend .error; } becomes the output .error, .error--serious { color: red; }. This process, called "intelligent unification," happens after most other Sass compilation.

When to use it

Use @extend when there is a clear "is-a" relationship between two classes. It's perfect for component modifiers in methodologies like BEM, where .button--danger is a specific type of .button. This keeps your HTML clean, requiring only class="button--danger".

When not to use it

Avoid extending generic, widely-used selectors (like .clearfix or .hidden). This can cause @extend to generate massive, bloated selector lists that are hard to debug and hurt performance. If you only want to reuse a block of properties without creating a semantic link between selectors, use a @mixin instead. A mixin copies the properties, which is more predictable and avoids selector bloat.

One canonical example

You want a serious error message that inherits from a basic error style but has a thicker border.

Scss input

.error { border: 1px solid red; background-color: #fdd; }

.error--serious { @extend .error; border-width: 3px; }

Compiled css output

.error, .error--serious { border: 1px solid red; background-color: #fdd; }

.error--serious { border-width: 3px; }

Interview question

When is it generally advisable to avoid using Sass @extend?

  • a.When the base selector is a component modifier, like .button--danger.
  • b.When the target HTML element already has multiple classes assigned to it.
  • c.When the styles being inherited are very minimal, consisting of only one or two declarations.
  • d.When you only want to reuse a block of properties without establishing a semantic "is-a" relationship.Correct
Why?

The card states that if you only want to reuse properties without creating a semantic link, a @mixin is preferable. @extend creates a semantic "is-a" relationship by grouping selectors, which is not always the desired outcome.

Just read this? Test yourself on what you have been reading.

Read the original → sass-lang.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles