Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Lucas Santos
Lucas Santos
19,315 Points

So in SASS does it mean that I need 2 of the same files in the same location.. Example test.css and test.scss ?

So lets say I have 3 css files in the same folder

  • test.css
  • style.css
  • color.css

does using SASS mean that I would need 6 files, meaning the same files but with .scss

so I would have:

  • test.css
  • test.scss
  • style.css
  • style.scss
  • color.css
  • color.scss

am I correct?

2 Answers

Erik McClintock
Erik McClintock
45,783 Points

Lucas,

Using SCSS, you would only need one CSS file, and that can be automatically compiled for you using either sass --watch at the command line, using software like Scout or Prepros, or even through packages for text editors like Sublime Text. You would have multiple .scss files as partials (i.e. files that would not get compiled), which would include your styles in the stylesheets that you have listed as "colors.css" and "test.css", etc. Then you would have a main SCSS stylesheet (let's call it style.scss, for example) which would include references to each of those "partials", as well as any other base styling (or you could have more partials for base styles, component styles, responsive styles, etc. - the list goes on!), which your Sass compiler would compile for you every time you make changes to and save style.scss, and would spit out style.css automatically. Style.css would then contain all the styles from all of the different partials and .scss files that you called into your one style.scss file.

So, you would end up having something more like this:

/*

SCSS files

the underscore at the front of these files tells the compilers that they are "partials", and thus to ignore them and not compile them on their own

_base.scss
_components.scss
_responsive.scss
_colors.scss
_test.scss

no underscore on the file below, and this is the file that would include all of those partials (making sure to include them in the proper order so as not to cause conflicts with the cascade, as in any stylesheet) and get compiled upon changes and saving it

style.scss


CSS file

the file below is what would get output, and pointed to in your link tag in the head of your HTML document

style.css

*/

Does this make sense? I'm not sure how far along you are yet in your Sass learnings, but this should all become clear before long!

Erik

Lucas Santos
Lucas Santos
19,315 Points

I am new to Terminal and SASS so I'm not fully understanding here so I'm going to follow up with some questions:

  1. when you use the underscore then name of file like _color.scss do you mean in the command line or naming the file with an underscore?

  2. So if you use the underscore method of naming files that file automatically becomes a partial of the main style sheet (the style sheet with no underscore) therefore if you compile that main style sheet for example style.scss it will make a style.css file with ALL of the partial file styles included in that one compiled main style sheet.. am I correct?

  3. To sum up what I took from your answer you would only need one main .scss file and the rest will be partials scss files that include the style when compiled into one .css file... correct?

Erik McClintock
Erik McClintock
45,783 Points

Questions answered below:

1) I mean in the filename itself; the underscore has nothing to do with the command line there. The only thing any of this would have to do with the command line is a) installing the SASS gem on your machine, and b) using the sass --watch command if you wanted to go that route (though there are alternatives if you want to steer clear of the command line, as mentioned in my initial answer)

2) No, those files do not automatically get pulled into your main .scss file, you need to put references to them in that file, as stated. Naming files with an underscore simply declares them as a partial, which tells your Sass compiler to ignore them. In the file that you DO want your Sass compiler to build (say, style.scss), you would reference the partial files via the import call, like this:

/* import your partials here */

@import '_color.scss';
@import '_components.scss';
@import '_responsive.scss';

Importing the partials in this manner will make their code pull into this style.scss file when the compiler runs it.

3) Yes, you only need one main .scss file for your compiler to build and turn into a .css file. In that .scss file you want to call the appropriate partials necessary to output all of your styles, and then you're good to go!

Erik

Lucas Santos
Lucas Santos
19,315 Points

ahh I see now ok makes perfect sense thanks a lot Erik!

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

In addition to everything Erik said above you don't even have to watch individual partial files.

With a simple watch command like below you can watch 2 folders so you can keep your files organised. So an SCSS file would output a CSS file in another folder.

sass --watch scss:css

Hope this helps. :)

Lucas Santos
Lucas Santos
19,315 Points

Why would you watch separate files or 2 folders when you can just import all the partial files into one main .scss file and watch that one file?

Erik McClintock
Erik McClintock
45,783 Points

Lucas,

He may have more to comment on, but initially, it seems Jonathan is reiterating that you do NOT watch the individual partial files.

Additionally, in the --watch command that he showed you, he's demonstrating that you can tell the compiler to watch the folder on the left of the colon, and compile TO the folder on the right side of the colon. So this tells Sass to watch your "scss" folder for any changes made to any full .scss file (i.e. not a partial), and when there are changes made/saved, to compile them into a .css file and deposit that .css file into your "css" folder.

You could also write the --watch command to watch a specific file and output to a specific file, as follows:

sass --watch input.scss:output.css

That would watch for any changes made/saved to your "input.scss" file and it would compile it and write all those changes to "output.css".

Erik

Lucas Santos
Lucas Santos
19,315 Points

so you mean instead of

sass --watch .

I would use

sass --watch scss:css

which instead of watching the main folder it watches for a folder within the main folder?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

You could name the folders anything you like. the scss:css in the watch command I recommended are examples of folder names you could use.

The first step is to use the terminal to move the directory you want which would be the root directory of your project.

Secondly you could type in a command

sass --watch sassfolder:cssfolder

Even if those folders didn't exist in your directory, Sass would create them. You'd need to then store an scss file or 2 with at lest one partial.

But let's also say you have a sass file in your project already. For the sake of this post lets say we used the above folder names. To out CSS in the CSS folder you'd write

sass --watch sassfolder/style.scss:cssfolder/style.css

This sends CSS output to a specific file in the cssfolder directory.

Hope that's clear. You use Sass commands to organise your project. :-)

Lucas Santos
Lucas Santos
19,315 Points

Ahh ok I see ok but just one part about your comment through me off. When you said:

You'd need to then store an scss file or 2 with at lest one partial.

so your telling me that there is a requirement of how many partials you need? or how many you need to create a folder?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi there,

There's no requirements for number of partials needed but you will of course need one scss file that isn't a partial (only one) so Sass knows which file to compile CSS output.

sass --watch scss:css

The command will only create a folder if it doesn't exist in that location. With only one compilable and many partials Sass will find the none partial file and use that file to output CSS.

But of course you can override this by explicitly specifying a filename to output CSS with by using...

sass --watch sassfolder/style.scss:cssfolder/style.css

Lucas Santos
Lucas Santos
19,315 Points

I see now thank's a ton Jonathan, really appreciate the help!