Inherit a class to another file in Sass
Last Updated :
07 Aug, 2023
Sass or syntactically awesome style sheets is a CSS preprocessor that gives CSS such powers that are not available in plain CSS. It gives the power of using expressions, variables, nesting, mixins(Sass form of functions), inheritance, and more. Other well-known CSS preprocessor examples include Less and Stylus but Sass is more popular.
Sass includes two syntaxes:
SCSS or Sassy CSS
SCSS uses the .scss syntax and it is very similar to regular CSS. SCSS is fully compliant with CSS. SCSS can be assumed as a superset of CSS. Any valid CSS style is a valid SCSS. Because of the similarity with CSS, it takes less time to get started with it.
Example:
css
$heading-color: #e94e1b; //Using Sass Variables
h4 {
color: $heading-color;
}
The indented syntax or Sass
This is the original syntax of Sass. It uses .sass file extension. It uses all the features of Sass but instead of using the curly-braces, it uses indentation to describe the format of the document. It is not fully compliant with CSS.
Example
css
$heading-color: #e94e1b;
h4
color: $heading-color
@import and @use:
A single CSS file can eventually grow larger and it'll be a tough job to maintain large stylesheets. So, it would be easier if there's a way to separate classes into various files. So one can import only the necessary files. In this way the stylesheet will be smaller and also maintenance will be easier. The stylesheet will also maintain the DRY(Don't Repeat Yourself) rule.
Approach 1: To import another stylesheet into a stylesheet, the @import keyword is used. The CSS or Scss files to be imported can reside in the same folder or somewhere else on the internet.
Syntax:
@import 'file-name';
@import url('url of the stylesheet')
Example
css
@import './buttons.scss';
@import url('https://example.com/css/breadcrumbs.scss');
Compiled CSS
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css');
.btn-large {
border-radius: 3rem;
border: 4px solid black;
background: black;
color: white;
width: 20rem;
height: 10rem;
display: flex;
align-items: center;
justify-content: center;
}
This is a feasible solution to import a CSS file into another file. But Sass has deprecated @import and will eventually remove it. Instead of using @import, Sass now supports @use because CSS also has a @import feature and there are some other major drawbacks of using @import which is a topic for another article.
Approach 2: The @use has to be used with a namespace. Suppose a file buttons residing on the same directory has a color variable called primary-color. To use the variable in other files, the below syntax will be followed.
Syntax:
@use 'file-name';
Example:
css
@use 'buttons';
.card {
color: buttons.$primary-color;
}
Compiled CSS:
.btn-large {
border-radius: 3rem;
border: 4px solid black;
background: black;
color: white;
width: 20rem;
height: 10rem;
display: flex;
align-items: center;
justify-content: center;
}
The namespace is used because of the naming conflicts that exist in @import. If two imported files contain two different variables of the same name, @import will force to use the variable value defined in the last imported file.
The @use also gives the ability to use a user-defined namespace.
Example:
html
@use 'buttons' as btn;
.card {
color: btn.$primary-color;
}
The as keyword allows defining custom namespaces.
An important thing to note here is that the Live Sass Compiler on VS Code is based on LibSass and LibSass currently doesn't support the @use function. So it is better to use the Dart Sass which generally is the first to implement new features. Installing Dart Sass is easy. This post here describes the steps.
Inheriting Styles from other classes
The previous section briefly described how to import and use styles stored in another file using the @import and the @use operator. To inherit a style from another class or id, the @extend keyword is used. Let's see an example, suppose the buttons class has a color: green; and opacity: .5; property, now to inherit these styles into another class, the @extend keyword will be used.
Example:
buttons.scss file
css
/* buttons.scss file */
.btn {
color: red;
opacity: 0.5;
}
style.scss file
css
/*style.scss file
where the style is to be inherited */
@use 'buttons';
.new-btn {
@extend .btn;
}
So, the above code will inherit the properties from .btn class to the .new-btn class.
Now, let's take a look at the compiled CSS file.
Compiled CSS
/* style.css */
.btn, .new-btn {
color: red;
opacity: 0.5;
}
One thing to note here is that the styles from the .btn class are not getting copied to the new-btn class, rather it comma separates the original selector which reduces duplicate code.
Conclusion
So, in this article, the @include, @use and the @extend keyword used in Sass are described briefly. Inheriting can also be achieved using mixins. To read about the mixin, you can check this article. Though if the style doesn't take any parameters, it is better to use the @extend method.
Similar Reads
How to Include One CSS File in Another?
Here are the methods to include one CSS file in another:Method 1. Using @import RuleThe @import rule allows you to import one CSS file into another, enabling modularity and reusability of styles.html<html> <head> <link rel="stylesheet" href="styles.css"> </head> <body>
3 min read
How To Apply Concept of Inheritance in CSS?
Inheritance, a fundamental concept in object-oriented programming (OOP), mirrors real-life inheritance where children inherit traits from parents. CSS Inheritance: In CSS inheritance, the child element will naturally inherit properties from its parent element.Syntax:<style> #parentclass { colo
2 min read
How to center a div on the edge of another div in SASS ?
In this article, we will see how to centre a div on the edge of another div using SASS. SASS is an extension of CSS that stands for Syntactically Awesome Style Sheets. It helps us to create things like variables, nested rules, etc. So that we can reuse the code. Centering a div on the edge of anoth
4 min read
How to create a LESS file and how to compile it ?
LESS (which stands for Leaner Style Sheets) is a backward-compatible language extension for CSS. CSS itself is great for defining styles and applying them to various HTML elements but it has a few limitations. Limitations of CSS: Writing CSS code becomes exhausting, especially in big projects.Mainta
2 min read
How to use a sass variable in nth child class ?
SASS is one of the most popular CSS extension languages which provides superpower to the normal CSS. The nth-child() class is a pseudo-class, when some HTML element has more than two elements of the same kind then we can use the nth-child class pseudo-class to provide styling to those elements witho
2 min read
How to create a mixin for placeholder in SASS ?
Mixin is one of the best features of SASS, which works like a function. You can create mixins for repeated CSS properties and then include those mixins using @include in your CSS class. Approach: We create a mixin, we are using @content which let us inject the whole CSS code block inside the mixin,
2 min read
Can a CSS class inherit one or more other classes ?
In this article, we will see how a class can inherit properties of other classes using CSS, along with understanding their basic implementations through the illustration. A descendent class can inherit any CSS property from the parent class by using Inheritance. Inheritance is the process by which g
4 min read
How to create optional arguments for a SASS mixin?
To create optional arguments for a SASS @mixin, the mixin declared must be passed and it should be included. The following approach will explain clearly. Syntax: @mixin function-name($var1, $x:val){ /* stylesheet properties */ } @include function-name(value, 0); Approach: Assigning Null in @mixin in
2 min read
How to calculate percent minus px in SASS ?
Calculating the difference between percent and px is not as simple as 50% - 30px. Obviously you'll be getting incompatible unit error. This is because SASS cannot perform arithmetic operations on values that cannot be converted from one unit to another. SASS does not know exactly how wide a "percent
2 min read
How to import regular CSS file in SCSS file ?
Importing a regular CSS file into a SCSS file allows you to use standard CSS styles within your SCSS file. This is done by using the @import directive, enabling the CSS file to be included and compiled along with SCSS styles during preprocessing.@import keyword: The @import keyword in SCSS allows yo
3 min read