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 ofExample 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
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
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
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!

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
