Partials in SASS help us to break up our files into smaller files without affecting performance.
The use of partials allows us to modularize our CSS, to help keep things maintainable.
We divide up our Sass into separate files representing different components. A partialsâ name will always start with an underscore _
. We then import the partial using an @import
directive.
Using Partials
Letâs say our Sass file is getting rather large. We might choose to make a partial file that contains just the code thatâs relevant to the header section; weâd call it _header.scss
and move the appropriate code into that new file.
We would then import it back into main.css
, like so:
// In main.scss
@import 'header';
Note: When you import a file, thereâs no need to include the _
or .scss
file extension.
@import in CSS vs SASS
The @import
directive is of course, also available in CSS. However, there is an important difference. Whenever an import statement is used in CSS, an HTTP request is made to the server. This increases the amount of resource requests and negatively affects the performance of a web page. SASS does not do that. SASS takes the file that you want to import from and combines it with the file youâre importing. So you can serve a single CSS file to the web browser, which does not affect the performance.
In the next post in this series, weâll be looking at how to implement inheritance with extends
.
Related Posts:
A little about me..
Hey, Iâm Tim! đ
Iâm a freelance business owner, web developer & author. I teach both new and experienced freelancers how to build a sustainable and successful freelancing business. Check out my Complete Guide to Freelancing if you'd like to find out more.
While you're here, you can browse through my blogs where I post freelancing tips, code tutorials, design inspiration, useful tools & resources, and much more! You can also join the newsletter, or find me on X.
Thanks for reading! đ