If you're a lover of SCSS, you'll definitely want to make sure to use it in your Angular applications. Luckily the Angular CLI does all the setup for you!
Let's first walk through the file changes that the Angular CLI handles for us and how you can modify existing projects to switch over to SCSS for styling. I'll then go over how I like to set up my SCSS files and folder structure when working on Angular projects.
Let's get into it!
New Project Setup Using Angular CLI
When creating a new Angular app using the ng new app-name
command, the CLI will ask you "Which stylesheet format would you like to use?" - select SCSS.
When choosing this option when creating a new project, the main things the CLI does is the following:
- Adds the
src/styles.scss
reference to theangular.json
build/options/styles
section - Creates a
styles.scss
file and adds it to yoursrc
folder
The CLI will configure the testing files and references as well, but we won't go into those here.
ππΌ Tip: Run
ng --version
in your terminal to check what @angular/cli version you are running, and update to the latest usingnpm i -g @angular/cli@latest
.
Switching Existing Angular Project to SCSS
If you ever need to switch to the SCSS stylesheet format in an existing project, having the two items above should be all you need to get going!
Updating CLI Config
The other thing you need to do is update your CLI configuration to create SCSS files instead of CSS files when you create new components. There are two ways to do this - using the CLI command or adding the reference manually. To use the command type the following into your terminal:
ng config schematics.@schematics/angular:component.styleext scss
ππΌ Tip: Sometimes this command modifies the
angular.json
in the wrong place. Make sure it is modified in the top section of your file under your project (usually line 8 or so if you don't have many configuration changes).
To make the change manually add the following schematics reference into your angular.json
file:
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
Using the SCSS-Scaffold NPM Package
A while back ago I created an Angular Schematic (you can read more about it here) that adds of all of the SCSS folder structure for you with the easy command npm add scss-scaffold, so you donβt have to π.
You can check it out the post I wrote about it here.
SCSS Structure
Global Styling Philosophy
You certainly can keep things simply by having one global styles.scss
SCSS file, and you can also use styling on a component-by-component basis.
I like to keep my styles as global as possible avoiding component styling as I find that things can get very messy very quickly, especially when working on teams with multiple developers.
Having global styles keeps everyone using the same structure and system, and encourages discussions about patterns and use cases.
SCSS Folder Structure
Once you have your Angular project ready and set up to use SCSS, it's time to move some things around.
To get things started:
- Create a folder in your
src
project folder calledstyles
. - Move your
styles.scss
file into the newly createdstyles
folder. - Update your
angular.json
file to point to the new location of yourstyles.scss
file.
Now, it's time to build out our folder scaffold. I like to follow something similar to the 7-1 Pattern but modify some of the names.
Below is an example of how I lay out the style folders:
Base Folder
My base folder contains global styles which has some general layout items, a few general default overrides (think normalize.css-type styles), and my base font size so I can use rems consistently.
The other thing my base folder contains is a typography stylesheet for all things fonts. This not only includes headings and general fonts, but also links and other custom font classes as needed.
Pages
I try to use pages sparingly but this is where I put non-standard styles that apply only to a certain page. I often look for patterns and systems to apply globally to re-usable elements, but sometimes when there are some custom considerations, this is where I'll place them.
Modules (or Components)
The Modules (or commonly known as Components), keeps all of my reusable component styles. This includes things like the header and footer, buttons, labels, navigation, cards, or any other elements that you are using throughout your project in several places.
Helpers
Helpers is where some of the most important things are. The first thing you'll need is your constants. This stylesheet contains all of the variables for your colors, font-families, z-indexes, and some default sizing. For example, I like to add $border-radius-large
and $border-radius-small
variables to help keep that aspect consistent throughout the app.
You'll also want to add a mixin stylesheet for some custom-made reusable elements. Mixins allow you to take in arguments - I like to use them for shapes and also for font groups (such as defining a base "title font" style that I can then build upon in my other stylesheets.
Vendors (Optional)
Lastly, you might want to include a vendor folder if you're incorporating a third party library like Bootstrap or Normalize.css. Sometimes it helps to keep any vendor-specific configuration or override styles in this vendor folder. You can also import only those specific stylesheets (such as Bootstrap base tools) and keep that here.
ππΌ Tip: When installing a vendor library, say using NPM, make sure you update your
angular.json
file to reference it's source location such as:
"styles": [
"../node_modules/bootstrap/dist/scss/bootstrap.scss",
"styles/styles.scss"
Print Stylesheet (Optional)
The other file I sometimes like to include is the print.scss
stylesheet that covers any custom print styling of your site. This is especially helpful on very animated or colorful websites that contain technical documentation - you might want to include styles that simplify things, and possibly omit elements for printing for easier content digesting. You could easily put this in your base folder, but I like keeping it separate as it often modifies content significantly different than intended for the overall application when used in its natural format (digital device).
SCSS Directory Files
SCSS structure includes having a directory file (i.e. _base-dir.scss
) in each folder that includes all of the individual stylesheets within that folder. Those directory files, then roll up into the 'main' styles.scss
folder which helps keep things clean and organized, and easy to read. I like to set up my directory files to look something like this:
The header helps me quickly know what I'm looking at - especially when I'm editing a lot of files. Below are the important pieces where you import the individual stylesheets within that folder, in this case, the base folder.
ππΌ Tip: Make sure that you add your import to your directory file every time you create a new stylesheet inside of a folder.
SCSS Main File Roll-Up
The last thing that you need is to roll-up all of those directory files into your main styles.scss
file that is actually being read by your app (as we told it to in the angular.json
file).
This is what my styles.scss
folder might look like:
ππΌ Tip: Remember that the order of imports matters! If you're using constants / variables that your other stylesheets reference, you'll need to make sure that directory import is at the top being imported before the files that are using those variables.
Using Component Styling
If you do want to use component styling but still want to use some of the SCSS structure, you can simply import the specific stylesheet into your your-component.component.scss
file using this format:
@import 'styles/variables';
Note that you'll have to update this path if you ever move this file or change its name.
Summary
That's it! Once you have SCSS up and running and a nicely organized folder structure, it should help you be much more efficient and organized when it comes to maintaining your stylesheet / CSS code.
Happy coding!
After-Thoughts π
Although the CLI has lots of configuration options β I find myself wishing there was a βno stylesβ flag (similar to the --skipTests=false
flag that currently exists) when creating components. Since I structure all of my stylesheets independently in my styles folder, I donβt ever have the need for component based stylesheets.
Luckily, if itβs something youβre looking for there are flags for inline styling, external styling and changing your stylesheet preference configurations (as we saw above).
Resources π§°
Here are some resources if you want to learn more about SCSS and the using SCSS with Angular and the CLI:
- SASS Guidelines
- Official SASS Lang Layout
- Scotch.io Tutorial (outdated but helpful background info)
- Angular CLI Commands
Top comments (3)
Thank for the contribution! Currently I am building a new Angular app and I am starting with getting a good folder structure.
Wow, really nice article. From the beautiful cover image to describing things to giving afterthoughts and resources! Great work.
And welcome to the DEV Community :)
Thanks for your support Vaibhav!