This tutorial aims to guide you on how to organize your SASS/SCSS files effectively for large projects. We'll be focusing on structuring your stylesheets in a way that makes them easy to navigate, maintain, and scale.
By the end of this tutorial, you should be able to:
You should have a basic understanding of:
SASS (Syntactically Awesome Stylesheets) and its superset SCSS (Sassy CSS) are CSS pre-processors, which allow us to use variables, nested rules, mixins, functions, and more, all with a fully CSS-compatible syntax.
A good practice to organize SASS files is to split them into separate files based on functionality, and then import them into a single file. This approach is often referred to as "SMACSS" or "Modular Architecture".
Here's a typical directory structure:
stylesheets/
|
|-- base/
| |-- _reset.scss
| |-- _typography.scss
| ...
|
|-- components/
| |-- _buttons.scss
| |-- _carousel.scss
| ...
|
|-- helpers/
| |-- _variables.scss
| |-- _functions.scss
| ...
|
|-- layout/
| |-- _grid.scss
| |-- _header.scss
| ...
|
|-- pages/
| |-- _home.scss
| |-- _contact.scss
| ...
|
|-- vendors/
| |-- _bootstrap.scss
| ...
|
|-- main.scss
In this structure:
base/
holds the boilerplate content such as reset and typography rules.components/
is for discrete, reusable components.helpers/
contains global variables, mixins, functions.layout/
is for layout styles like header, footer, grid system.pages/
contains page-specific styles.vendors/
holds all the CSS files from external libraries and dependencies.In your main.scss
file, you'd import all other SCSS files. The underscores before each filename tell Sass that they are partial files, meaning they won't be compiled to CSS. Instead, they are imported and compiled into the main.scss
file.
// main.scss
// Helpers
@import 'helpers/variables';
@import 'helpers/functions';
// Base
@import 'base/reset';
@import 'base/typography';
// Layout
@import 'layout/grid';
@import 'layout/header';
// Components
@import 'components/buttons';
@import 'components/carousel';
// Pages
@import 'pages/home';
@import 'pages/contact';
// Vendors
@import 'vendors/bootstrap';
Let's consider a few code examples.
// helpers/_variables.scss
$primary-color: #336699;
$secondary-color: #FFCC00;
// helpers/_mixins.scss
@mixin transition($property: all, $duration: 0.3s, $timing: ease) {
transition: $property $duration $timing;
}
// components/_buttons.scss
.button {
background-color: $primary-color;
@include transition(background-color);
&:hover {
background-color: darken($primary-color, 10%);
}
}
In the above example, we used the $primary-color
variable and transition
mixin defined earlier. The resulting CSS will have a transition effect on the background-color
of .button
.
In this tutorial, we've learned about organizing SASS/SCSS files for large projects. We've covered how to structure your stylesheets effectively, making them easier to navigate and maintain. We've also looked at how to create a modular architecture for your stylesheets.
Create a SASS directory with an appropriate structure for a blog website.
Create variables for primary and secondary colors, and use them in a base/_typography.scss
file.
Create a mixin
for a responsive font-size
and use it in the components/_post.scss
file.
Tip: Keep practicing organizing your stylesheets as you work on different projects. The more you practice, the more comfortable you'll be with maintaining and scaling your stylesheets effectively.