Skip to content

Less - Import Options

The @import at-rule in LESS is fundamental for organizing your stylesheets into multiple, manageable files. It allows you to include the content of other .less or .css files into your current file. LESS extends the standard CSS @import with several powerful options that control how files are processed and included. These options provide fine-grained control over the compilation process.

By default, if an imported file has a .less extension, it’s processed as a LESS file. If it has a .css extension, it’s treated as plain CSS and output as a standard CSS @import statement (unless it’s a local file that LESS can parse). Other extensions are typically treated as LESS files.

Import options are specified in parentheses after the @import keyword and before the file path:

`@import (keyword) "filename";`
Sr.No.Import Option & SyntaxDescription & Use Case
1reference
@import (reference) "utilities.less";
Use this to import variables, mixins, and selectors from utilities.less for use in the current file, without outputting any of the CSS rules from utilities.less itself. This is ideal for creating libraries of mixins or variable definitions that don’t produce output on their own.
If utilities.less contains only mixins and variables, using (reference) ensures it doesn’t add any CSS to the output unless its mixins/variables are actually used.
2inline
@import (inline) "third-party.css";
Includes the content of the specified file directly into your output CSS without any LESS processing. This is useful for incorporating third-party CSS libraries or any plain CSS that you don’t want LESS to parse or modify.
The content of third-party.css will be copied verbatim into the compiled CSS.
3less
@import (less) "styles.txt";
Forces LESS to treat the imported file as a LESS file, regardless of its actual extension (e.g., .txt, .css). LESS will parse and process its content accordingly.
If styles.txt contains valid LESS syntax, it will be compiled as part of your stylesheet.
4css
@import (css) "theme.less";
Forces LESS to treat the imported file as a standard CSS file. If it’s a local file, its content might be inlined. If it’s a remote URL, it will typically result in a standard CSS @import url(...) statement in the output. LESS will not process its content as LESS syntax.
If theme.less is imported with (css), its LESS-specific features like variables or mixins will not be processed by the importing file context.
5once
@import (once) "_variables.less";
This is the default behavior for LESS imports. It ensures that a file is imported and processed only one time, even if multiple @import statements reference it within the project. This prevents duplicate CSS output and redundant processing.
If _variables.less is imported multiple times, (once) (or no option) ensures its definitions are included only once.
6multiple
@import (multiple) "mixins-with-params.less";
Allows the same file to be imported and processed multiple times. Each @import (multiple) statement will include the file’s content again. This can be useful in specific scenarios, such as applying mixins defined in a file with different parameters at different points, or when a file generates CSS that needs to be repeated.
Use with caution, as it can lead to bloated CSS if not managed carefully. It’s often used when a file generates output based on variables that change between imports.
7optional
@import (optional) "theme-customizations.less";
Prevents compilation errors if the specified file to import cannot be found. If the file exists, it’s imported as usual; otherwise, the @import statement is silently ignored, and compilation continues.
This is useful for optional theme files, user-specific overrides, or feature-specific styles that may or may not be present in a project.

You can combine multiple import options by separating them with commas within the parentheses. The order usually doesn’t matter.

Example:
@import (less, optional) "maybe-a-less-file.config";
// This will try to import 'maybe-a-less-file.config', treat it as a LESS file,
// and will not error if the file is not found.
@import (reference, optional) "_local-settings.less";
// This will import '_local-settings.less' for its variables/mixins only,
// and will not error if the file doesn't exist.

Understanding and utilizing these import options effectively is key to building modular, maintainable, and scalable CSS architectures with LESS.