SASS/SCSS / Partials and Imports

Creating and Using Partials in SASS/SCSS

This tutorial covers the basics of creating and using partials in SASS/SCSS. You will learn how to break down styles into manageable chunks that can be imported into a main styles…

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Explains how to organize styles using partials and import them into main stylesheets.

Creating and Using Partials in SASS/SCSS

1. Introduction

In this tutorial, we will cover the basics of creating and using partials in SASS/SCSS. Partials are a great way to modularize your CSS and help keep things easier to maintain. You will learn how to break down styles into manageable chunks which can be imported into a main stylesheet.

By the end of this tutorial, you will be able to:

  • Understand what SASS/SCSS partials are.
  • Create SASS/SCSS partials.
  • Import partials into your main stylesheet.

Prerequisites: Familiarity with CSS and basic understanding of SASS/SCSS.

2. Step-by-Step Guide

Partials in SASS are used to split CSS into smaller, more manageable components. A partial is simply a SASS file. The file name begins with an underscore. The underscore lets SASS know that the file is only a partial file and that it should not be generated into a CSS file.

Creating a Partial

To create a partial, simply create a new .scss file in your directory and make sure it starts with an underscore. For example, we might have a partial for our variables called _variables.scss.

// _variables.scss
$font-stack:    Helvetica, sans-serif;
$primary-color: #333;

Importing a Partial

To use a partial, we use the @import directive. This will include the partial in the main .scss file.

// main.scss
@import 'variables';
body {
  font-family: $font-stack;
  color: $primary-color;
}

The above main.scss will compile into the following CSS:

body {
  font-family: Helvetica, sans-serif;
  color: #333;
}

3. Code Examples

Let's now look at a more complex example:

Example 1:

// _variables.scss
$font-stack:    Helvetica, sans-serif;
$primary-color: #333;

// _base.scss
body {
  font-family: $font-stack;
  color: $primary-color;
}

// main.scss
@import 'variables';
@import 'base';

Here, _variables.scss contains our variables, _base.scss contains some base styles, and main.scss imports both files. The compiled CSS will be:

body {
  font-family: Helvetica, sans-serif;
  color: #333;
}

Example 2:

// _reset.scss
* {
  margin: 0;
  padding: 0;
}

// main.scss
@import 'reset';
body {
  font-family: Arial, sans-serif;
}

In this example, _reset.scss contains a simple CSS reset. main.scss imports the reset then defines some styles. The compiled CSS will be:

* {
  margin: 0;
  padding: 0;
}
body {
  font-family: Arial, sans-serif;
}

4. Summary

In this tutorial, we covered how to create and use SASS/SCSS partials. We learned how to split CSS into smaller, more manageable components using partials. We did this by creating new .scss files beginning with an underscore and then imported them into our main .scss file using the @import directive.

Next, you might want to learn about SASS mixins, which allow you to make groups of CSS declarations that you want to reuse throughout your site.

You can practice your knowledge on SASS partials on platforms like CodePen or by building a small project and splitting your CSS into partials.

5. Practice Exercises

  1. Create a _variables.scss partial with 3 variables: $primary-color, $secondary-color, and $font-stack. Import it into a main.scss file and use the variables to style a body element.

  2. Create a _reset.scss partial with a simple CSS reset. Import it into a main.scss file and define some styles for a body and h1 element.

  3. Create a _base.scss partial with some base styles. Create a _layout.scss partial with some layout styles. Import both into a main.scss file.

Solutions:

// _variables.scss
$primary-color:   #333;
$secondary-color: #ccc;
$font-stack:      Arial, sans-serif;

// main.scss
@import 'variables';

body {
  font-family: $font-stack;
  color: $primary-color;
  background-color: $secondary-color;
}
// _reset.scss
* {
  margin: 0;
  padding: 0;
}

// main.scss
@import 'reset';

body {
  font-family: Arial, sans-serif;
}

h1 {
  color: blue;
}
// _base.scss
body {
  font-family: Arial, sans-serif;
}

// _layout.scss
.container {
  max-width: 960px;
  margin: 0 auto;
}

// main.scss
@import 'base';
@import 'layout';

Remember, it's always a good idea to practice and experiment with the code on your own. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Backlink Checker

Analyze and validate backlinks.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help