Sass, SCSS, and Less
PhpStorm integrates with compilers that translate Sass, Less, and SCSS code into CSS. To use a compiler in PhpStorm, you need to configure it as a File Watcher based on the relevant predefined template.
Before you start
Make sure you have Node.js on your computer.
Make sure the File Watchers and Less or Sass required plugins are enabled on the Settings/Preferences | Plugins page, tab Installed, see Managing plugins for details.
Installing Sass/SCSS
In the embedded Terminal (Alt+F12) , type:
npm install -g sass
Learn more from the Sass official website.
Installing Less
In the embedded Terminal (Alt+F12) , type:
npm install --global less
Learn more from the Less official website.
Compiling your code into CSS
To compile your code automatically, you need to configure a Sass, Less, or SCSS File Watcher which will track changes to your files and run the compiler.
When you open a file, PhpStorm checks whether an applicable File Watcher is available in the current project. If such File Watcher is configured but disabled, PhpStorm displays a popup that informs you about the configured File Watcher and suggests to enable it.
If an applicable File Watcher is configured and enabled in the current project, PhpStorm starts the compiler automatically upon the event specified in the New Watcher dialog.
If the Auto-save edited files to trigger the watcher checkbox is selected, the File Watcher is invoked as soon as any changes are made to the source code.
If the Auto-save edited files to trigger the watcher checkbox is cleared, the File Watcher is started upon save ( , Ctrl+S) or when you move focus from PhpStorm (upon frame deactivation).
Learn more from File Watchers.
PhpStorm creates a separate file with the generated output. The file has the name of the source Sass, Less, or SCSS file and the extension .css. The location of the generated files is defined in the Output paths to refresh field of the New Watcher dialog. However, in the Project Tree, they are shown under the source file which is now displayed as a node.
Create a File Watcher
In the Settings/Preferences dialog (Ctrl+Alt+S), click File Watchers under Tools. The File Watchers page that opens shows the list of already configured File Watchers.
Click
or press Alt+Insert. Depending on the tool you are going to use, choose the Less, Sass, or SCSS predefined template from the list.
In the Program field, specify the path to the compiler archive depending on the chosen predefined template.
lessc for Less.
sass for Sass/SCSS.
If you followed the standard installation procedure with npm, PhpStorm locates the required files itself and fills in the field automatically. Otherwise, type the path manually or click
and choose the file location in the dialog that opens.
Proceed as described in File Watchers.
Example: compiling SCSS into CSS
Suppose your project is structured as follows:
![Compiling SCSS into CSS: project structure Compiling SCSS into CSS: project structure](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_project_structure.png)
As you can see, _grid.scss is imported in Page.scss. The example below shows how Page.scss is compiled into CSS when you save your project manually or automatically and how the changes to _grid.scss are reflected in the generated CSS file.
Create a File Watcher of the type SCSS: open the Settings/Preferences dialog (Ctrl+Alt+S), go to , click
on the toolbar, and select SCSS from the list.
In the New File Watcher dialog, that opens, all the mandatory fields are already filled in.
Actually, these settings are sufficient to run the compiler successfully.
Let's change grid.scss, for example, replace
margin-left: 0;
at line 31 withmargin-left: 12px;
. This triggers our File Watcher and the compiler processes Page.scss. As a result, two files are generated and shown nested under Page.scss:Page.css with compiled CSS code
Page.css.map with source maps that let you step through your app during a debugging session.
Although the default settings are sufficient to run the compiler successfully, let's still take a closer look at them to see how the File Watcher's behavior can be customized.
Change the action that triggers the File Watcher
The File Watcher wakes up and launches the transpiler as soon as your project is saved manually (
or Ctrl+S) or automatically.In general, your code is saved automatically when you move the focus from PhpStorm (on frame deactivation). With File Watchers, auto-save is also performed when you edit a file from the File Watcher's scope. As a result, the transpiler may be running all the time you type, which may cause performance issues. To solve the problem, suppress saving edited files automatically.
Press Ctrl+Alt+S to open the IDE settings and select SCSS in our example) and click on the toolbar. In the Edit File Watcher dialog, expand the Advanced Options area and clear the Auto-save edited files to trigger the watcher checkbox.
![Auto-save edited files off Auto-save edited files off](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_file_watcher_autosave_on_edit_off.png)
By default, the File Watcher wakes up even when a file from its scope is edited from outside PhpStorm. To override this behavior and transpile files only on editing internally, clear the Trigger the watcher on external changes checkbox.
![Trigger the File Watcher on external changes is off Trigger the File Watcher on external changes is off](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_file_watcher_on_external_changes_off.png)
Change the scope
By default, PhpStorm monitors changes in all files with the .scss extension in the entire project. This works for our example. However, you can change the scope to process, for example, only uncommitted changes. In a large project, this will save time.
Press Ctrl+Alt+S to open the IDE settings and select Advanced Options area, and select the applicable scope from the list. Learn more from Scopes and file colors.
. , expand the![SCSS File Watcher: change default scope SCSS File Watcher: change default scope](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_file_watcher_settings_change_scope.png)
Custom output location
By default, the generated .css and .css.map files are stored in the folder where the original file is and are shown as its children in the Project tool window. You can change this default behavior to store all generated .css and .css .map files in a separate folder.
Let's start with a simple case. Suppose you have a custom_output.scss file in the project root.
![Project structure Project structure](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_custom_output_project_structure_plain.png)
Let's edit custom_output.scss, for example, replace fill-opacity: abs(50);
at line 6 with fill-opacity: abs(60);
. With the default File Watcher configuration, the generated files custom_output.css and custom_output.css.map will be stored in the project root and shown as children of custom_output.scss.
![Default settings: the output is stored in the project root Default settings: the output is stored in the project root](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_custom_output_result_with_default_settings.png)
It would be convenient to store all the output in a separate folder, for example, css. Let's create a custom SCSS_custom_output File Watcher with the css folder as the output location.
Press Ctrl+Alt+S to open the IDE settings and select as described above.
. , then create a SCSS File WatcherUpdate the default settings as follows:
In the Arguments field, type:
$FileName$:$ProjectFileDir$/css/$FileNameWithoutExtension$.cssIn the Output paths to refresh field, type:
$ProjectFileDir$/css/$FileNameWithoutExtension$.css:$ProjectFileDir$/css/$FileNameWithoutExtension$.css.map
![New File Watcher with custom output path New File Watcher with custom output path](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_custom_output_default_fw_updated.png)
Save the new File Watcher and make sure it is enabled.
![New File Watcher with custom output path saved and enabled New File Watcher with custom output path saved and enabled](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_custom_output_fw_saved.png)
Now, if you edit custom_output.scss, for example, replace fill-opacity: abs(50);
at line 6 with fill-opacity: abs(60);
, PhpStorm creates a css folder and stores the generated custom_output.css and custom_output.css.map files in it.
![Custom settings: the output is stored in a separate folder Custom settings: the output is stored in a separate folder](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_custom_output_result_with_custom_settings.png)
Custom output location: preserve the original folder structure
Let's now consider an example where .scss files are stored in a folder structure, for example:
![Project structure Project structure](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_custom_output_project_structure_tree.png)
With the default File Watcher, the generated files will be stored next to the original .scss files. If we use the custom File Watcher as described above, all the generated files will be stored in one same css folder:
![Custom output: the generated files are stored in a separate folder in a plain structure Custom output: the generated files are stored in a separate folder in a plain structure](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_custom_output_result_with_custom_fw_plain.png)
To have PhpStorm preserve the folder structure, let's create another custom File Watcher.
Press Ctrl+Alt+S to open the IDE settings and select as described above.
. , then create a SCSS File WatcherUpdate the default settings as follows:
In the Arguments field, type:
$FileName$:$ProjectFileDir$/css/$FileDirRelativeToProjectRoot$/$FileNameWithoutExtension$.cssIn the Output paths to refresh field, type:
$ProjectFileDir$/css/$FileDirRelativeToProjectRoot$/$FileNameWithoutExtension$.css:$ProjectFileDir$/css/$FileDirRelativeToProjectRoot$/$FileNameWithoutExtension$.css.map
![Custom output with folder structure: File Watcher updated Custom output with folder structure: File Watcher updated](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_custom_output_structured_fw_updated.png)
Save the new File Watcher and make sure it is enabled.
![Custom output with folder structure: File Watcher saved and enabled Custom output with folder structure: File Watcher saved and enabled](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_custom_output_structured_fw_saved.png)
Now, if you edit custom_output_body.scss, custom_output_header.scss, or custom_output_footer.scss, PhpStorm creates a css folder with the subfolders' structure that preserves the structure of styles_structured.
![Custom settings: the output is stored in a separate folder, the structure preserved Custom settings: the output is stored in a separate folder, the structure preserved](https://resources.jetbrains.com/help/img/idea/2022.1/ws_scss_example_custom_output_result_structured.png)
Configuring syntax highlighting
You can configure Less/Sass/SCSS-aware syntax highlighting according to your preferences and habits.
In the Settings/Preferences dialog (Ctrl+Alt+S), go to .
Select the color scheme, accept the highlighting settings inherited from the defaults or customize them as described in Colors and fonts.