I'd like to use Bootstrap 4 with SASS in an Angular(4+) project created with Angular CLI.
In particular I need to:
ng serve scriptIn your custom. scss , you'll import Bootstrap's source Sass files. You have two options: include all of Bootstrap, or pick the parts you need. We encourage the latter, though be aware there are some requirements and dependencies across our components.
Angular supports Sass, CSS, and Less to style global application styles as well as component styles. In addition, angular component styles have an effective CSS encapsulation mechanism that assures any component CSS is local to the component and does not globally alter any styles.
In order to setup Angular + Bootstrap 4 using SASS we just need to configure the Angular CLI and install the Bootstrap 4 npm package. There is no need to install any SASS compiler because it's already included.
EDIT: this answer has been updated to work with a newer version Angular CLI (tested with version 6.1.3). I left the instructions for the older Angular CLI in the bottom of this answer, however I strongly recommend you to update your Angular CLI version.
Edit your angular.json file and add the "styleext": "scss" key value to the projects.PROJECT_NAME.schematics.@schematics/angular:component object.
This is how it should look like:
{ // ... "projects": { "PROJECT_NAME": { // .... "schematics": { "@schematics/angular:component": { "styleext": "scss" } }, // ... } Simply use:
ng new my-project --style=scss
To accomplish this you just need to rename the style file from .css to .scss and change the @Component({ ... }) configuration accordingly:
styleUrls: ['./my-component.scss']
In this way the angular-cli will automatically watch and recompile these files whenever they change while you are executing commands like ng serve.
Install Bootstrap 4 via npm:
npm install bootstrap --save
Now add Bootstrap to the angular-cli.json config inside the styles array (before any other custom css/scss files in order to let them override bootstrap rules :
"styles": [ "node_modules/bootstrap/scss/bootstrap.scss", /* ... */ ], This way the Bootstrap 4 source code will stay clean and it will be very easy to upgrade it whenever a new version is released.
Any additional SASS styles which should globally affect the project (unlike the single Component styles) can be added under app/assets/scss and then referenced in the styles array of angular-cli.json.
My suggestion is to reference a single main.scss file which will include all your custom SASS styles: for example a _variables.scss for your custom variables, a _global.scss file for your global rules, etc..
So in your angular-cli.json you will reference just one custom main.scss file:
"styles": [ "node_modules/bootstrap/scss/bootstrap.scss", "src/assets/scss/main.scss" ], which internally includes all your custom global* SASS code:
// main.scss @import "variables"; @import "global"; // import more custom files... *Note that you MUST NOT include here the *.scss style files of the single Components.
There are some projects that allow you to use Bootstrap without jQuery.
Two examples:
ngx-bootstrap
ng-bootstrap
The difference between those two project is discussed here: What is the difference between "ng-bootstrap" and "ngx-bootstrap"?
WARNING: I will not maintain this part of the answer anymore, so instead to proceed reading it, I recommend to update your Angular CLI version and following the instructions above.
Run:
ng set defaults.styleExt scss
this will affect your .angular-cli.json config file ( example ).
Note: in case you're starting from scratch, you can create a new project using Angular CLI using: ng new my-project --style=scss which is the equivalent of creating a new project normally and then running the command mentioned above.
To accomplish this you just need to rename the style file from .css to .scss and change the @Component({ ... }) configuration accordingly:
styleUrls: ['./my-component.scss']
( example ).
In this way the angular-cli will automatically watch and recompile these files whenever they change while you are executing commands like ng serve.
Install Bootstrap 4 via npm:
npm install bootstrap --save
Now add Bootstrap to the .angular-cli.json config inside the styles array (before any other custom css/scss files in order to let them override bootstrap rules :
"styles": [ "../node_modules/bootstrap/scss/bootstrap.scss", /* ... */ ], ( example ).
This way the Bootstrap 4 source code will stay clean and it will be very easy to upgrade it whenever a new version is released.
Any additional SASS styles which should globally affect the project (unlike the single Component styles) can be added under app/assets/scss and then referenced in the styles array of .angular-cli.json.
My suggestion is to reference a single main.scss file which will include all your custom SASS styles: for example a _variables.scss for your custom variables, a _global.scss file for your global rules, etc.. ( example).
So in your .angular-cli.json you will reference just one custom main.scss file:
"styles": [ "../node_modules/bootstrap/scss/bootstrap.scss", "assets/scss/main.scss" ], which internally includes all your custom global* SASS code:
// main.scss @import "variables"; @import "global"; // import more custom files... *Note that you MUST NOT include here the *.scss style files of the single Components.
There are some projects that allow you to use Bootstrap without jQuery.
Two examples:
ngx-bootstrap
ng-bootstrap
The difference between those two project is discussed here: What is the difference between "ng-bootstrap" and "ngx-bootstrap"?
In addition to @ShinDarth's answer, you may want to go for a more minimal solution, importing only the Bootstrap modules you need instead of them all.
So you would replace:
"styles": [ "../node_modules/bootstrap/scss/bootstrap.scss", "assets/scss/main.scss" ], With:
"styles": [ "assets/scss/main.scss" ], Then your main.scss would look like:
// import only the Bootstrap modules you need, e.g. @import "~bootstrap/scss/functions"; @import "~bootstrap/scss/variables"; @import "~bootstrap/scss/mixins"; @import "~bootstrap/scss/grid"; @import "~bootstrap/scss/navbar"; @import "~bootstrap/scss/forms"; // import your own custom files, e.g. @import "variables"; @import "global";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With