SASS/SCSS / Partials and Imports

Importing Multiple Partials into Stylesheets

In this tutorial, you will learn how to import multiple partials into a main stylesheet. We'll cover the @import directive and how to use it to combine styles from different parti…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

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

Introduction

Welcome to this tutorial on importing multiple partials into stylesheets. The goal of this tutorial is to teach you how to use the @import directive to combine styles from different partials.

By the end of this tutorial, you will be able to:
- Understand what partials are and how they work
- Use the @import directive to import multiple partials into a main stylesheet

Prerequisites: Basic knowledge of CSS is required.

Step-by-Step Guide

Understanding Partials

Partials are smaller Sass files that you can import into other Sass files. This allows you to modularize your CSS and help keep things easier to maintain. A partial is simply a Sass file named with a leading underscore. For example, _buttons.scss.

Using @import

The @import directive takes the name of a Sass or CSS file to include the contents of that file right at the location of the @import. This is a powerful way to combine files and keep your stylesheets modular.

@import 'buttons';
@import 'colors';

In the example above, the CSS in _buttons.scss and _colors.scss will be imported.

Code Examples

Suppose we have two partials: _colors.scss and _buttons.scss.

_colors.scss:

/* Define some color variables */
$primary-color: #333;
$secondary-color: #CCC;

_buttons.scss:

/* Define some button styles */
.btn {
  background-color: $primary-color;
  color: $secondary-color;
}

In your main.scss file, import the two partials:

main.scss:

/* Import the partials */
@import 'colors';
@import 'buttons';

The output CSS will be:

.btn {
  background-color: #333;
  color: #CCC;
}

Summary

In this tutorial, you've learned how to import multiple partials into a main stylesheet using the @import directive. This helps you modularize your CSS and keep things easier to maintain.

Your next steps could be learning more about Sass features like mixins and functions. You might also want to explore other CSS preprocessors like Less.

Practice Exercises

  1. Create three partials: _colors.scss, _buttons.scss, and _typography.scss. Import them into a main stylesheet.

  2. Refactor the _buttons.scss partial from the code example to include a btn-large class that has a larger font-size and padding.

  3. Create a _variables.scss partial that defines some width and height variables. Use these variables in another partial.

Remember to import all your partials into a main stylesheet. Practice will help you get comfortable with the process and understand the concept better.

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

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

Watermark Generator

Add watermarks to images easily.

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

Random Name Generator

Generate realistic names with customizable options.

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