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!

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let's look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

In this article we will be diving into the world of scrollbars. I know, it doesn’t sound too glamorous, but trust me, a well-designed page goes hand-in-hand

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),