Is it possible in SASS to inherit from a class in another file?

CssInheritanceStylesheetSass

Css Problem Overview


The question pretty much says it all.

For instance, if I were using, say, Twitter Bootstrap, could I define classes in my own SASS stylesheet that inherit from Bootstrap's CSS classes? Or does inheritance in SASS only work within the scope of a single file?

Css Solutions


Solution 1 - Css

YES! its possible.

If you want all <button> elements to inherit the .btn class from Twitter Bootstrap's Default buttons

In your styles.scss file you would have to first import _bootstrap.scss:

@import "_bootstrap.scss";

Then below the import:

button { @extend .btn; }

Solution 2 - Css

**I might be mistaken, but if I get what you're trying to do, can't you just use the @extend .classname; command inside the element that you'd want to extend? Naturally, you should only modify your own code to preserve updatability.

Solution 3 - Css

To my knowledge, you have to use @import of the file containing the classes you want to use into your SASS file in order to utilize them in that file. However, I am not a SASS/SCSS expert, so someone may know of another way to remotely use them that I am not aware of.

Solution 4 - Css

Just as the accepted answer has shown this is possible with @import, however @import has been deprecated by sass

> The Sass team discourages the continued use of the @import rule. Sass will gradually phase it out over the next few years, and eventually remove it from the language entirely. Prefer the @use rule instead.

The @use rule is better suited for use now, since it does not pollute the scope of the importing (user) module. unfortunately at the time of writing the use rule is only implemented in Dart sass.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDan TaoView Question on Stackoverflow
Solution 1 - CssagustibrView Answer on Stackoverflow
Solution 2 - CssMark ErnstView Answer on Stackoverflow
Solution 3 - CssScottSView Answer on Stackoverflow
Solution 4 - CssehabView Answer on Stackoverflow