Why to put “_” in front of filename in SCSS ?

When we add “_” in front of the SCSS file, it means that it is partial for SCSS. When the compiler gets any SCSS files starting with “_”, it simply ignores the file. If you want this partial for SCSS to be included for styles, you must use @import. The advantage of using partials is that you can use style files to organize your code and all the files would be compiled on a single file. The purpose of partial SCSS is to keep your styles separated into logical sections.
In the end, we want one SCSS file to have complied whereas we can have many logical partial SCSS files.

Syntax:

@import filename;

Note: While importing we don’t put “_”, just the file name.



Examples 1: Let’s create a colors file _colors.scss

filter_none

edit
close

play_arrow

link
brightness_4
code

$first-color: blue;
$second-color: yellow;
  
// let's include it in main
// SCSS file, style.scss
  
@import "colors"
body {
  color: $first-color;
  background: $second-color;
}

chevron_right


Output:

body {
  color: blue;
  background: yellow; 
}

Examples 2: Let’s create a colors file _font.scss

filter_none

edit
close

play_arrow

link
brightness_4
code

$font: arial;
$color: red;
  
// Let's include it in main
// SCSS file, style.scss
  
@import "font";
body {
  font: 100% $font;
  color: $color;
}

chevron_right


Output:

body {
  font: 100% arial;
  color: red;
}


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.