First things first – I use SCSS Syntax. Also, you need a compiler – I use NetBeans (Koala is also good). But these are for Windows users just like myself – I have no doubts that for Linux users, there are much more powerful solutions :-).
Directory structure
I’d say this is up to:
- personal preference
- project requirements
- … and few other things.
You can go with something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
style.scss partials/_base.scss partials/_grids.scss partials/_typography.scss ... vendor/_jquery.scss ... etc. |
Partials
As you can see in the above structure, there are partials – this is the word you use for Sass files. It is a nice way to structure CSS, based on the layout, project type, etc.
Import
Use the @import clause for importing partials into the main (compiled) file. Normally you would put import clauses into the main style.scss file and the syntax would be something like the following:
1 2 3 4 5 |
// Modules and Variables @import "partials/base"; @import "partials/buttons"; @import "partials/grids"; @import "partials/typography"; |
Notice that you don’t need _ or .scss suffix.
Variables
Yes, these are the variables you see in other programming languages. In _base.scss partial, you can put something like:
1 |
$primary-color: #333; |
And you would call it like:
1 2 3 |
body { color: $primary-color; } |
But in _buttons.scss partial, you could also use the following, and without the need of redeclaring this variable:
1 2 3 |
button { background: $primaryColor; } |
But needless to say – if you want to reuse this variable across other partials, make sure you define it in _base.scss file (or whichever partial will be imported first), possibly on the top, before this variable has been used. As you know, in CSS, whatever comes last takes the precedence. And there is a quite big chance that your CSS file will not be compiled at all, as the compiler would return an error ( something like variable not declared or so).
Mixins and @include
Now Mixins is an excellent topic. Let the show you few mixins and notice that they use @include clause:
1 2 3 4 5 6 |
@mixin border-radius($radius) { -webkit-border-radius: $radius; -moz-border-radius: $radius; -ms-border-radius: $radius; border-radius: $radius; } |
And this is how you would call it in our example:
1 |
.box { @include border-radius(10px); } |
But there is absolutely no reason why wouldn’t you use it again:
1 2 |
.box-wider { @include border-radius(20x); } |
And you would get the following result:
1 2 3 4 5 6 7 8 9 10 11 |
.box { -webkit-border-radius: 10px; -moz-border-radius: 10px; -ms-border-radius: 10px; border-radius: 10px; } .box-wider { -webkit-border-radius: 20x; -moz-border-radius: 20x; -ms-border-radius: 20x; border-radius: 20x; } |
I guess you are already asking – do I have to write all these mixins, or has someone already done this before? Sure they did it – and a lot of people did it! You can find an extensive list on this blog post, While you can find also some of them on my resources page.
Inheritance
Why bother with redeclaring the styles, when you can use an @extend clause. It would go like:
1 2 3 4 5 6 7 8 9 10 |
.message { border: 1px solid #ccc; padding: 10px; color: #333; } .success { @extend .message; border-color: green; } |
And the CSS result will be:
1 2 3 4 5 6 7 |
.message, .success { border: 1px solid #ccc; padding: 10px; color: #333; } .success { border-color: green; } |
Nesting
If you use the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
nav { ul { margin: 0; padding: 0; list-style: none; } li { display: inline-block; } a { display: block; padding: 6px 12px; text-decoration: none; } } |
You would get:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
nav ul { margin: 0; padding: 0; list-style: none; } nav li { display: inline-block; } nav a { display: block; padding: 6px 12px; text-decoration: none; } |