Importing CSS Files into LESS: Syntax Options and Compilation Behavior Analysis

Dec 11, 2025 · Programming · 9 views · 7.8

Keywords: LESS preprocessor | CSS import | @import directive

Abstract: This article provides an in-depth exploration of importing CSS files into the LESS preprocessor, focusing on how different @import directive options affect compilation behavior. By comparing three import methods—(css), (less), and (inline)—it details the syntax format, compilation output, and applicable scenarios for each option. With practical code examples, the article explains how to correctly reference style classes from external CSS files and resolve common 'undefined' errors, offering valuable guidance for front-end developers on LESS compilation configuration.

Overview of CSS File Import Mechanisms in LESS Preprocessor

As a CSS preprocessor, LESS's @import directive supports not only importing .less files but also CSS files. However, different import methods lead to distinct compilation behaviors, and understanding these differences is crucial for proper stylesheet construction.

Basic Syntax and Options of the @import Directive

The @import directive in LESS supports controlling how imported files are processed through option parameters. The basic syntax format is:

@import (option) "file-path";

where the option parameter determines how the LESS compiler handles the imported file content.

(css) Option: Preserving CSS As-Is Output

When using the (css) option, the imported file is not parsed by the LESS compiler but is instead output directly as a CSS @import statement in the final stylesheet. For example:

@import (css) "lib";

After compilation, this generates:

@import "lib";

This approach maintains the independence of CSS files, with the browser loading them at runtime. However, it prevents direct referencing of style classes or variables defined in the imported CSS file within the LESS file.

(less) Option: Parsing CSS as LESS

To reference styles defined in external CSS files, the (less) option must be used. This instructs the LESS compiler to parse the .css file as if it were a .less file, making its style rules available for reference. For example:

@import (less) "lib.css";

Assuming lib.css contains:

.type {
    font-family: Arial, sans-serif;
    color: #333;
}

The .type class can then be directly referenced in the LESS file:

.small {
    font-size: 60%;
    .type;
}

After compilation, the .small selector inherits all style properties from .type, outputting:

.small {
    font-size: 60%;
    font-family: Arial, sans-serif;
    color: #333;
}

Note that if the file extension is omitted and no option is specified, LESS defaults to searching for a .less file. Explicitly using the (less) option ensures proper parsing of .css files.

(inline) Option: Inlining CSS Content

In addition to the two main options, LESS provides the (inline) option. This copies the CSS file's content directly into the output file without LESS parsing. For example:

@import (inline) "../timepicker/jquery.ui.timepicker.css";

This is suitable for scenarios where third-party CSS libraries need to be embedded directly into the output file, but again, these styles cannot be referenced in LESS code.

Common Error Analysis and Solutions

A common error developers encounter is attempting to reference CSS style classes that were not properly imported. The NameError: .type is undefined error in the original question occurred precisely because the default import method (or incorrect option) was used, preventing the LESS compiler from recognizing the .type class defined in the .css file.

The solution is to explicitly specify the import option:

// Incorrect approach - cannot reference CSS style classes
@import "../style.css";

// Correct approach - can reference CSS style classes
@import (less) "../style.css";

This way, the LESS compiler fully parses the style.css file, making all its style rules available in the current LESS context.

Practical Application Scenarios and Best Practices

In actual development, appropriate import strategies should be chosen based on specific needs:

  1. Reusing styles from CSS libraries: Use the (less) option to parse CSS as LESS, facilitating inheritance and extension.
  2. Maintaining CSS file independence: Use the (css) option to let the browser load external CSS files at runtime.
  3. Inlining third-party styles: Use the (inline) option to embed CSS content directly into the output file.

It is recommended to unify import strategies during project builds to avoid compilation inconsistencies from mixing different options. For large projects, consider establishing dedicated import configuration files to clarify the processing method for each external resource.

Compilation Configuration and Performance Considerations

Different import methods impact compilation output and performance differently: the (less) option increases compilation time due to parsing additional CSS files; the (css) option delays loading to browser runtime, potentially affecting page rendering performance; the (inline) option increases output file size but reduces HTTP requests.

Developers should weigh these factors based on project specifics, balancing development efficiency, maintenance costs, and runtime performance.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.