Extension and Operators in SASS

Extension and Operators in SASS. Learn and implement Extension and operators in SASS.

The extension is a feature in SASS / SCSS through which we can apply styles of one class to another just by calling the @extend in the subsequent class. Here’s the following example in which we have explained the classic use case of extending 

Create a class with the name paragraph1

.paragraph1{
    color: red;
    background: white; 
    &:hover{
        color: pink;
    }
}

Create another class with the name paragraph2 and inside it call the paragraph1 style with @extend function.

.paragraph2{
    @extend .paragraph1;
    &:hover{
        color: yellow;
    }
}

Operators in SASS / SCSS is similar to CSS operators. There are operator functions such as addition, subtraction, multiplication and division. 

How operators are used in CSS:

.paragraph1{
    width:  calc(80% - 40%);
}

How operators are used in SASS / SCSS: 

.paragraph1 {
    width: 80% - 40%;
}

There is no need to use the calc function. 

The basics of SASS / SCSS is over with the extend and operators. You can explore more with the learning in your projects.

Light and Dark Theme using Mixin

Light and Dark Theme using Mixin and implement in your web projects.

It is possible to switch between a dark and light theme using simple SCSS  / SASS without many code lines. We can leverage Mixin to achieve the change in light and dark themes. Following is the code for creating Mixin which can easily switch between light and dark themes. 

We will leverage two functions lighten and darken inside Mixin two achieve the functionality. Lighten function will help lighten the applied colour, and darken function will help darken the used colour. 

$primary-color: #000000;
$text-color: #3af80a;
$secondary-color: #432396;

@mixin theme($light-theme: true) {
    @if $light-theme{
        background: lighten($primary-color, 100%);
        color: darken ($text-color, 100%);
    }
}

Now create a class called light and apply it to the body tag. Inside the light class now include the theme mixin to enable the theme switch property.

.light{
    @include theme ($light-theme: true);
}

If the $light-theme is true, the HTML page will apply a light theme, the background colour will be light, and the text colour will be dark. If the $light-theme is false, the HTML page will apply a dark theme. 

.light{
    @include theme ($light-theme: false);
}

The above code will create a dark background and light text colour. In the next tutorial, we will learn Extension and Operators in SASS / SCSS.

Mixins in SASS

Learn Mixins in SASS and reutilize style

Mixins permit you to characterize styles that can be re-utilized all through your stylesheet. They make it simple to try not to utilize non-semantic classes like .float-left and to disperse assortments of libraries’ styles. 

Mixins are characterized utilizing the @mixin at-rule, which is composed @mixin <name> { … } or @mixin name(<arguments…>) { … }

Following is a simple example of using mixing to define content to centre.

@mixin flexCenter{

display: flex;

justify-content: center; 

align-items: center; 

}

Now just using @include to call the mixin will copy the entire style. The beauty is that it can be called n number of times inside the SCSS file.

.main {

@include flexCenter;

}

The SCSS compiles to the following CSS

.main {

display: flex;

justify-content: center; 

align-items: center; 

}

Here’s another classic example using arguments inside mixins to define the flex-direction. 

@mixin flexCenter($direction) {

flex-direction: $direction

}

To call the mixin with the argument, follow the example.

.main{

@include flexCenter(column);

}

The column parameter defined inside flexCenter mixin will specify the flex-direction to the column, and the following is equivalent CSS

.main {

display: flex;

justify-content: center; 

align-items: center; 

flex-direction: column

}

Learn to implement

Functions in SASS

Learn functions in sass. Create functions, call them and level up your SASS Skills.

Functions permit you to characterize complex operations on SassScript values that you can reuse all through your stylesheet. They make it simple to extract out standard formulae and practices in a decipherable manner. The function helps in computing calculations and return values in SASS / SCSS. 

Functions are characterized utilizing the @function at-rule, which is composed @function <name>(<arguments…>) { … }

Following is a simple demonstration of how we can implement a function in SASS / SCSS. Let’s define the variable font-weights to allocate a name to respective font-weight such as light, regular and bold.

$font-weights:(
    "light": 300,
    "regular": 400,
    "bold": 700
);

Next, we can define the function weight to integrate the font-weights class.

@function weight($weight-name){
    @return map-get($font-weights , $weight-name)
}

Now, we can directly leverage function by calling the function weight to define font-weight in CSS

.main{
     font-weight: weight(light);
}

The respective output CSS will be as follows:

.main{
    font-weight: 300;
}

In the next tutorial, we will be learning Mixins in SASS

partials in sass

Easily implement Partials in SASS in 2021

Learn Partials in SASS quickly in 5 mins and start implementing in your projects

Partial in SASS / SCSS is useful when the project files are enormous. Reusing or maintaining such huge SCSS files is cumbersome. Partial SCSS allows developers to split the variable, resets or page-wise SCSS separately and quickly embed them in the main SCSS. The main advantage of SASS is that partial SCSS files will not compile into new CSS files and therefore it becomes easier to manage. 

partials in sass
Partials in SASS

To make a particular SCSS a partial file, we can add an underscore before the file name and follow by .scss extension. For example, if we are to take variables of the main scss to a partial file, we can cut the variables and paste in a new scss file with the name _variables.scss and we can embed it using the import function in the main scss. 

SCSS file before the split of partial SCSS files: 

File name: main.scss

$primary-color: #d2d2d2;

$secondary-color: #322332;

$accent-color: #435434;

$text-color: #0000;

p{

color:$primary-color;

background-color: $secondary-color;

}

CSS Output

p{

color: #d2d2d2;

background-color: #322332;

}

Split variables to new SCSS files

File name: _variables.scss

$primary-color: #d2d2d2;

$secondary-color: #322332;

$accent-color: #435434;

$text-color: #0000;

File name: main.scss

@import './variables'; 

p{

color:$primary-color;

background-color: $secondary-color;

}

Please note: No need to call the filename with an extension scss, calling just the name of the file is sufficient

CSS Output

p{

color: #d2d2d2;

background-color: #322332;

}

In the next tutorials, we will learn how to create functions and call functions in SASS.

For more details about various courses, check out Ampersand Academy.

Nesting in SASS

Learn Nesting in SASS with easy guide and working examples

Nesting in SASS / SCSS is easy with the help of “&” to achieve the nesting. In SASS “&” behaves uniquely and it denotes the current selector.  The selector changes as we nest down.  If we want to define a division with a width of 80% and aligning to a centre with margin style, we use the following code.

.main{
    width:80%;
    margin: 0 auto;
}

Now if we want to apply the class inside main and with the name of main_new. The class main_new defines the style inside the main class. We can nest it with the following code.

.main{
    width:80%;
    margin:0 auto;
    &_new{
        font-size: 2em;
    }
}

The above SCSS will compile into the following CSS code. 

.main {
    width:80%;
    margin:0 auto;
}
.main_new{
    font-size: 2em;
}

To copy the SCSS expression string to the subclass, we can use “#{&}” to copy the entire main class expression to the following subclass. If we want to achieve the CSS as mentioned below, we can use the nesting with interpolation.

Desired CSS

.main{
    width:80%;
    margin:0 auto;
}

.main .main_new{
    font-size:2em;
}

Actual SCSS Code

.main{
    width:80%;
    margin:0 auto;
    #{&}_new{
        font-size: 2em;
    }
}

Successfully, you have learned nesting in SASS. In the next tutorial, we will be learning Partials in SASS, how to implement partials in your website project

Variables in SASS

What are variables in SASS and Learn Variables in SASS

Sass variables are basic: you relegate a value to a name that starts with $, and afterwards, you can allude to that name rather than the actual worth. In any case, notwithstanding their straightforwardness, they’re perhaps the most valuable tool Sass brings to the table. Variables make it conceivable to lessen redundancy, do complex math, arrange libraries, and considerably more.

A variable affirmation looks a great deal like a property assertion: it’s composed <variable>: <expression>. In contrast to a property, which must be pronounced in a style rule or at-rule, variables can be proclaimed anyplace you need. To utilize a variable, simply remember it for worth.

CSS also has its variables, and CSS variables are compatible with almost 90% of the available browsers. Following is CSS variable syntax.

:root{
	--blue:#1e90ff;
}

To call the variable syntax in CSS is as follows.

p{
	background-color: var(--blue);
}

SASS variables are easy to implement. The SASS variable starts with $ and its easy to call the variable by prefixing the $ just before the variable name. Both SASS and SCSS variables begin with $. By implementing the following code, you’ll learn variables in SASS.

SASS Variable Syntax

$primary-color: #d2d2d2
p
	color: $primary-color

SCSS Variable Syntax

$primary-color: #d2d2d2;
p{
	color:$primary-color;
}

CSS Output

p{
	color: #d2d2d2
}

CSS Variables vs. SASS Variables

CSS has variables of its own, which are entirely different from Sass variables. Know the distinctions! Sass variables are completely gathered away by Sass. CSS variables are remembered for the CSS yield. CSS variables can have various qualities for various components, however, Sass variables just have each worth in turn. Sass variables are basic, which implies assuming you utilize a variable and, change its worth, the previous use will remain something very similar. CSS variables are explanatory, which implies in the event that you change the worth, it’ll influence both prior utilizes and later employments.

In the next tutorial, we will be learning Nesting and Interpolation in SASS.

Maps in SASS

What is Maps in SASS and learn how to use in SASS

Maps in SASS / SCSS hold key-value pairs, and it enables users to lookup value with the help of key. Defining it is similar to defining a variable; it starts with $ following which we type the Map name. Within braces, we add key: value pairs. For example:

$font-weights: (

"regular": 400,

"light": 300,

"bold": 700

); 

This is a typical example that depicts Maps. In order to change the font weight of an element, we have defined font weights and within it, we have multiple key-value pairs which can be called in order to apply respective font weights to the text elements of the HTML. Kindly observe the syntax on declaring the variable and various key-value pairs which can be extended to any use cases for your project.

Try experimenting with font-family, define variables and add several key-value pairs for each text element such as paragraphs, heading 1, heading 2 and so on. Now that we have defined the maps, the next step is to call maps so that the respective CSS property applies to the text elements.

Calling Maps in your files

It’s easy to leverage SASS Maps by calling the map-get(variable, value) function. In place of the variable, we can add here font-weight and the corresponding value will automatically suggest based on the key-value name we define in the variable. 

body{

font-weight: map-get($font-weights, regular);

}

Equivalent CSS Code

body{

font-weight: 400;

}

In the next tutorial, we will be learning variables in SASS. You’ll learn how to create a variable in SASS/SCSS, how to use it for referencing, and the best practices while using variables in SASS.

Learn SASS in 9 easy tutorials! 

Getting Started with SASS. Learn SASS from this series of tutorials

What is SASS?

SASS is one of the most powerful, stable and professional CSS supersets in the World. 

What does SASS means?

  • Sass fullform is Syntactically Awesome Stylesheet
  • Sass is an extension to Cascading Style Sheets (CSS)
  • Sass is a powerful CSS pre-processor
  • Sass is fully adaptable with all versions of CSS
  • Sass diminshes css repetition and therefore increases productivity
  • Sass was developed by Hampton Catlin and developed by Natalie Weizenbaum in 2006
  • Sass is opensource.

Why Use Sass?

CSS Stylesheets are getting bigger, more complex, and cumbersome to manage. This is why a CSS pre-processor can help in making the CSS stylesheet smaller, less complex and easier to manage, thereby making the life of the designer easy and empowers them to leverage advanced styling concepts in their website and web development projects. Sass is enriched with features that are not presently available in the normal CSS stylesheets. Features like variables, nested rules, mixins, imports, inheritance, built-in functions, and other vital features make SASS one of the most powerful CSS preprocessors in the world.

What you should know before learning SASS?

Before getting started with an SASS preprocessor one should have at least intermediary expertise or exposure in HTML and CSS. Without the knowledge of HTML or CSS, it is not possible to learn SASS and implement it in your projects. So, if you want to learn GTML and CSS you can check out tutorials from W3Schools and learn the basics and intermediate level. Once you have intermediary knowledge in HTML and CSS, you can easily learn SASS from these easy tutorials

Learn the benefits of SASS: 

CSS Compatible: 

SASS is compatible with all versions of CSS. Compatibility makes programmers integrate with the project seamlessly.

Feature Rich:

SASS is full of unique and problem-solving features which makes it a one-of-a-kind CSS preprocessor in the World. 

Most Approved:

SASS is approved globally by several companies and programmers. Many developers use SASS as the standard preprocessor of CSS. 

Community:

SASS has enormous community support which includes thousands of developers and thousands of web companies. 

Extensions:

SASS has two extension formats

  1. .sass – Older SASS extension and uses indented syntax. After processing, it changes to standard CSS.
  2. .scss – Sassy CSS and is a relatively new extension. All proper CSS stylesheet is also a valid SCSS stylesheet.

How to process SASS

It’s not possible to integrate SASS or SCSS stylesheets directly into the website or web application projects. SASS or SCSS is processed to become a valid CSS file and then integrated into the project. There are several ways to go about processing SASS or SCSS to CSS. We are going to discuss only the free tools/plugins to process SCSS

Desktop Application: 

There are several paid desktop applications that help in processing SASS or SCSS to CSS; however, there is one free application that does the job well and is available in all operating systems, i.e., Scout-App. You can install the Scout-App from the following URL.

https://scout-app.io/

VS Code Extension: 

The easiest way to process SASS or SCSS is to use open source Text Editor – Microsoft Visual Studio Code and use the available Live SASS Compiler and Live Server extensions in the Extension Store. After typing the valid SCSS or SASS styles, you can click the Watch SCSS button in the bottom part of VS Code. Every time, we add a new line of SCSS, the extension processes it and creates equivalent CSS in the .css file that the extension self creates. We can directly add the generated .css file to our web project.

Get SASS VS Code Extension

You’ll learn SASS features such as Maps, Variables, mixins, nesting, partials, functions, theming, extensions and operators in the next tutorial