Table of Contents
grammar
Example 3
Output
Advantages of using the @import directive
Disadvantages of using the @import directive
Using partial files in SASS
Home Web Front-end CSS Tutorial What is the use of the SASS @import function?

What is the use of the SASS @import function?

Sep 14, 2023 pm 07:01 PM

SASS is a CSS preprocessor that keeps CSS code dry by not allowing duplication in the code. There are various directives available in SASS, one of them is the @import directive.

The '@import' directive is used to import the code of one '.scss' or '.sass' file into another file and execute it during compilation. We can use the "@import" directive to import variables, functions, mixins, etc. from one file to another.

grammar

Users can use the '@import' directive in SASS to import files according to the following syntax.

@import 'test'
Copy after login

We imported the 'test.scss' or 'test.sass' file in the above syntax. Here, we don’t need to specify the file extension when importing the code, the compiler will automatically detect it.

If the user wants to import multiple CSS files in a single file, the following syntax should be used.

@import 'file1', 'file2', 'file3', 'file4', ...
Copy after login

Now, let us understand how to import a file using @import directive with an example.

The Chinese translation of

Example 1

is:

Example 1

In the example below, we have added some variables in the "font.scss" file. After that, we use the "@import" directive to import the contents of the "fonts.scss" file into the "styles.scss" file.

In the "style.scss" file, we use the variables of the "font.scss" file. After that, we compiled the code of the "styles.scss" file and the user can observe the updated code of the "style.css" file in the output image.

File name - Style.scss

@import "fonts";
$height: 5rem;
$border: 2px, solid, blue;
div {
   height: $height;
   border: $border;
   border-radius: 1rem;
}
h1 {
   font-size: $heading-font-size;
   font-weight: $heading-font-weight;
   color: $heading-font-color;
   font-family: $heading-font-family;
}
p {
   font-size: $paragraph-font-size;
   font-weight: 200;
}
Copy after login

File name - Fonts.scss

$normal-font-size: 1rem;
$paragraph-font-size: 1.2rem;
$heading-font-size: 1.5rem;
$heading-font-weight: 700;
$heading-font-color: #000;
$heading-font-family: "Roboto", sans-serif;
Copy after login

Output

SASS @import 函数有什么用?

The Chinese translation of

Example 2

is:

Example 2

In the following example, we add color-related variables to the "color.scss" file and font-related variables to the "fonts.scss" file. In the "style.scss" file, we use the "@import" directive to import the "fonts.scss" and "colors.scss" files together.

In the "style.scss" file, we use color and font variables. In the output, the user can observe the values ​​of specific CSS properties where we use variables.

File name - Style.scss

@import "fonts", "colors";
div {
   color: $text-color;
   background-color: $background-color;
}
ul {
   li {
      color: $text-color;
      background-color: $background-color;
      font-size: $normal-font-size;
   }
}
h1 {
   color: $primary-color;
   font-size: $heading-font-size;
   font-weight: $heading-font-weight;
   font-family: $heading-font-family;
}
Copy after login

File name - Colors.scss

$text-color: #000;
$background-color: #fff;
$primary-color: #000;
$secondary-color: #fff;
$tertiary-color: #000;
Copy after login
Copy after login

File name - Fonts.scss

$normal-font-size: 1rem;
$paragraph-font-size: 1.2rem;
$heading-font-size: 1.5rem;
$heading-font-weight: 700;
$heading-font-family: "Roboto", sans-serif;
Copy after login

Output

SASS @import 函数有什么用?

Advantages of using the @import directive

There are some benefits to using the "@import" directive, which we explain below.

  • We can import the CSS code of one file into another file.

  • We can create a separate CSS file for each component of the code and import it when needed.

Disadvantages of using the @import directive

There are some disadvantages to using the '@import' directive, which we explain below.

  • It makes all content in the CSS file, such as variables, functions, mixins, etc., globally accessible. Therefore, it is difficult for developers to determine where a specific variable is defined.

  • Since everything in each imported file becomes global, each file should have different variable names to avoid conflicts.

  • The SASS compiler compiles every scss file, whether imported or not, which increases compilation time and reduces code efficiency.

Using partial files in SASS

In order to solve the above shortcomings, we can use partials in SASS. We can create a partial scss file by prefixing the file name with an underscore. For example, '_test.scss', '_colors.scss', etc.

Whenever we use partials, the SASS translator will not compile the code of the partial file, which can improve the efficiency of the code. However, we can import the contents of the partial scss file into the main scss file.

This is an example using partial scss files.

Example 3

In the example below, we create the '_colors.scss' partial file and import it in the 'style.scss' file. In this example, the code compiles more efficiently because we use partial files.

However, the code output remains the same whether we use part of the code or not.

File name - Style.scss

@import "colors";
img {
   width: 100%;
   height: 100%;
   background-color: $background-color;
}
p {
   color: $text-color;
}
Copy after login

File name - _Color.scss

$text-color: #000;
$background-color: #fff;
$primary-color: #000;
$secondary-color: #fff;
$tertiary-color: #000;
Copy after login
Copy after login

Output

SASS @import 函数有什么用?

Users learned to use the "@import" directive to import the code of one file into another file. It helps us break SCSS code into smaller chunks and avoid duplication. However, there are some disadvantages of using @import directive, but we can use partials to solve this problem.

The above is the detailed content of What is the use of the SASS @import function?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

PHP is A-OK for Templating PHP is A-OK for Templating Apr 11, 2025 am 11:04 AM

PHP templating often gets a bad rap for facilitating subpar code — but that doesn't have to be the case. Let’s look at how PHP projects can enforce a basic

Programming Sass to Create Accessible Color Combinations Programming Sass to Create Accessible Color Combinations Apr 09, 2025 am 11:30 AM

We are always looking to make the web more accessible. Color contrast is just math, so Sass can help cover edge cases that designers might have missed.

See all articles