Home  >  Article  >  Web Front-end  >  What is the use of the SASS @import function?

What is the use of the SASS @import function?

PHPz
PHPzforward
2023-09-14 19:01:051075browse

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'

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', ...

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;
}

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;

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;
}

File name - Colors.scss

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

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;

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;
}

File name - _Color.scss

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

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:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete