HomeSoftware DevelopmentHow do you create software to make use of SCSS ?

How do you create software to make use of SCSS ?


Angular is a well-liked JavaScript framework for constructing net purposes. It offers a set of instruments and libraries for constructing client-side purposes utilizing fashionable net applied sciences.

Sass (Syntactically Superior Model Sheets) is a CSS preprocessor that provides highly effective options to CSS, equivalent to variables, mixins, and capabilities. Sass types are written in a particular syntax referred to as SCSS (Sass CSS), which is a superset of CSS. SCSS recordsdata have the .scss extension and are compiled into common CSS recordsdata that can be utilized in net purposes.

On this article, we’ll undergo a step-by-step course of explaining the right way to create an Angular software and use Sass for styling. We are going to begin by creating a brand new Angular venture and putting in Sass. Then, we’ll configure Angular to make use of Sass and create a Sass file. Lastly, we’ll import the Sass file into an Angular part and run the applying to check the Sass types. Earlier than getting began, be sure you have the next stipulations:

  • Node.js and npm are put in in your native machine. You possibly can obtain the newest model of Node.js from the official web site.
  • Angular CLI (Command Line Interface) is put in globally in your native machine. You possibly can set up Angular CLI by operating the next command in your terminal:
npm set up -g @angular/cli

We are going to create an software to make use of scss by implementing them with 2 totally different strategies. 

Technique 1: Set up and use SCSS, after initializing the Angular venture.

Step 1: Create a brand new Angular venture

To create a brand new Angular venture, open your terminal and navigate to the listing the place you wish to create your venture. Then, run the next Angular CLI command:

ng new my-project

Change my-project with the identify of your venture. This command will create a brand new listing with the identify of your venture and generate all the mandatory recordsdata and folders for an Angular venture inside it.

Step 2: Set up Sass

To make use of Sass in your Angular venture, you should set up the node-sass package deal. Navigate to your venture listing and run the next command:

npm set up sass

This can set up the node-sass package deal as a improvement dependency in your venture.

Step 3: Configure Angular to make use of Sass

To configure Angular to make use of Sass, you should modify the angular.json file within the root of your venture. Open the file and discover the tasks > [project-name] > architect > construct > choices part. Then, add the next property:

"stylePreprocessorOptions": {
  "includePaths": [
    "src/styles"
  ]
}

This can inform Angular to search for Sass recordsdata within the src/types listing.

Step 4: Create a Sass file

Now, that you’ve got configured Angular to make use of Sass, you’ll be able to create a Sass file in your venture. Create a brand new file with the .scss extension within the src/types listing (or every other listing that you just specified within the stylePreprocessorOptions property within the angular.json file). For instance, you’ll be able to create a file referred to as types.scss.

Step 5: Import the Sass file into your Angular part

To make use of the Sass file in your Angular part, you should import it within the part’s styleUrls property. For instance, when you’ve got a part referred to as MyComponent, you’ll be able to import the Sass file like this:

import { Element } from '@angular/core';
@Element({
   selector: 'app-my-component',
   templateUrl: './my-component.part.html',
   styleUrls: ['./styles.scss']
})
export class MyComponent {
   // part code goes right here
}

Step 6: Run the Angular software

To run the Angular software, navigate to the venture listing in your terminal and run the next command:

ng serve --open

This can begin the event server and serve your Angular software. Now you can view the applying in your browser.

Instance: This instance describes the creation of an Angular software to implement the scss.

HTML

<div class="middle">

    <h1 type="shade: inexperienced;">

        GeeksforGeeks

    </h1>

    <h3>

        How do you create an angular software to make use of scss?

    </h3>

    <h4>

        Technique 1: Set up and use SCSS, after initializing 

        the Angular venture.

    </h4>

</div>

  

<button (click on)="toggleList()">

    Present Steps

</button>

<div *ngIf="showList">

    <ul>

        <li>

            Create a brand new Angular venture

        </li>

        <li>

            Set up Sass

        </li>

        <li>

            Configure Angular to make use of Sass

        </li>

        <li>

            Import the Sass file in your Angular part

        </li>

        <li>

            Run the Angular software

        </li>

    </ul>

</div>

CSS

$main-background-color: inexperienced;

$main-text-shade: white;

$main-font-size: 16px;

$main-border-radius: 4px;

  

@mixin button-style {

  padding: 8px 16px;

  background-color: $main-background-color;

  shade: $main-text-color;

  border: none;

  border-radius: $main-border-radius;

  font-size: $main-font-size;

  cursor: pointer;

}

  

physique {

  margin: 0;

  padding: 0;

}

  

button {

  @embody button-style;

}

  

ul {

  list-style: inside;

  padding: 5;

  margin: 10;

}

  

li {

  padding: 5px;

  font-size: $main-font-size;

  shade: grey;

}

Javascript

import { Element } from '@angular/core';

  

@Element({

    selector: 'app-root',

    templateUrl: './app.part.html',

    styleUrls: ['./app.component.scss']

})

export class AppComponent {

    title = 'scss-app';

    showList: boolean = false;

    toggleList() {

        this.showList = !this.showList;

    }

}

Output:

 

Technique 2: Use or configure SCSS in your Angular venture on the time of initialization with the type flag set to make use of SCSS.

Step 1: Make an Angular Undertaking with the CLI with the type flag.

The beneath command will create a brand new venture with the identify “project-name” and allow SCSS because the default styling language.

ng new project-name --style=scss

Now, you’ll be able to create a brand new part and it’ll have a .scss file with the identical identify because the part. For instance, we’ve made the beneath venture with a mode flag set to scss.

 

Undertaking Construction: The beneath venture will seem after profitable set up:

 

Instance: That is one other instance that implements the SCSS with an Angular software.

HTML

<div class="middle">

    <h1 type="shade: inexperienced;">GeeksforGeeks</h1>

    <h3>

        How do you create an angular software to make use of scss?

    </h3>

    <h4>

        Technique 2: Use or configure SCSS in your 

        Angular venture on the time of initialization

        with the type flag set to make use of SCSS.

    </h4>

</div>

  

<button (click on)="openModal()">

     Present Steps

</button>

  

<div class="modal" 

     *ngIf="showModal">

    <div class="modal-content">

        <span (click on)="closeModal()">

            ×

        </span>

        <ul>

            <li>

                Create a brand new Angular venture

            </li>

            <li>

                Set up Sass

            </li>

            <li>

                Configure Angular to make use of Sass

            </li>

            <li>

                Import the Sass file in your Angular part

            </li>

            <li>

                Run the Angular software

            </li>

        </ul>

    </div>

</div>

CSS

$button-bg-shade: blue;

$button-text-shade: white;

$modal-bg-shade: rgba(0, 0, 0, 0.5);

  

@mixin button-style {

  background-color: $button-bg-color;

  shade: $button-text-color;

  border: none;

  border-radius: 5px;

  padding: 10px 20px;

  font-size: 16px;

  font-weight: daring;

  cursor: pointer;

  

  &:hover {

    background-color: lighten($button-bg-color, 10%);

  }

}

  

button {

  @embody button-style;

}

  

.modal {

  place: fastened;

  prime: 0;

  left: 0;

  width: 100%;

  top: 100%;

  background-color: $modal-bg-color;

  show: flex;

  justify-content material: middle;

  align-items: middle;

}

  

.modal-content {

  background-color: white;

  width: 50%;

  top: 50%;

  border-radius: 10px;

  box-shadow: 0px 0px 10px gray;

  padding: 20px;

}

  

li {

  padding: 10px;

  font-size: 16px;

  font-weight: daring;

  shade: gray;

}

Javascript

import { Element } from '@angular/core';

  

@Element({

    selector: 'app-root',

    templateUrl: './app.part.html',

    styleUrls: ['./app.component.scss']

})

export class AppComponent {

    title = 'scss-app';

    showModal: boolean = false;

    openModal() {

        this.showModal = true;

    }

  

    closeModal() {

        this.showModal = false;

    }

}

Output:

 

RELATED ARTICLES

Most Popular

Recent Comments