Skip to content

Less - Importing

The @import at-rule in LESS allows you to include the content of one LESS or CSS file into another. This is crucial for organizing your stylesheets into manageable modules, promoting reusability and maintainability. When you import a .less file, its variables, mixins, and rules become available in the importing file. Importing plain .css files will include them as standard CSS imports.

LESS processes @import statements by default, merging the imported code directly. File extensions (.less) are optional for LESS files.

Let’s consider a common project structure where styles are modularized:

my-project/
├── index.html
├── css/ (compiled CSS will go here)
└── less/
├── style.less (main file)
├── _variables.less (variables, prefixed with _ as a convention for partials)
└── _base.less (base styles, partial)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LESS Importing Example</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header>
<h1>Understanding LESS Imports</h1>
</header>
<main>
<p class="main-text">This paragraph uses styles imported from a base file.</p>
<p class="highlight-text">This paragraph has a highlight color defined by an imported variable.</p>
</main>
</body>
</html>
// Defines reusable variables
@primary-color: #3498db;
@secondary-color: #e74c3c;
@text-font-stack: Helvetica, Arial, sans-serif;
@base-font-size: 16px;
// Basic styling for HTML elements
body {
font-family: @text-font-stack;
font-size: @base-font-size;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
color: #333;
}
.main-text {
line-height: 1.6;
}
// Import variables first, so they are available for other imports and rules
@import "_variables"; // .less extension is optional
// Import base styles
@import "_base.less"; // .less extension can be explicit
// Styles specific to style.less
header {
background-color: @primary-color;
color: white;
padding: 1em 0;
text-align: center;
margin-bottom: 20px;
}
.highlight-text {
color: @secondary-color;
font-weight: bold;
border-left: 3px solid @secondary-color;
padding-left: 10px;
}

To compile style.less into css/style.css, you would use the LESS compiler (Node.js environment assumed):

npm install -g less
lessc less/style.less css/style.css

This command compiles less/style.less (including its imports) into css/style.css.

/* Compiled from less/_variables.less and less/_base.less and less/style.less */
body {
font-family: Helvetica, Arial, sans-serif;
font-size: 16px;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
color: #333;
}
.main-text {
line-height: 1.6;
}
header {
background-color: #3498db;
color: white;
padding: 1em 0;
text-align: center;
margin-bottom: 20px;
}
.highlight-text {
color: #e74c3c;
font-weight: bold;
border-left: 3px solid #e74c3c;
padding-left: 10px;
}

LESS provides several options to control how files are imported:

  • @import (reference) "file.less";: Imports the LESS file, but its selectors are not output unless they are extended or used by mixins. Variables and mixins from the referenced file are available.
  • @import (inline) "file.css";: Includes the source file in the output as is, without processing. Useful for CSS files that shouldn’t be parsed by LESS.
  • @import (less) "file.txt";: Treats the imported file as a LESS file, regardless of its extension.
  • @import (css) "file.less";: Treats the imported file as a CSS file, outputting it as a standard CSS @import rule.
  • @import (once) "file.less"; (Default): Ensures the file is imported only once. Subsequent imports of the same file are ignored.
  • @import (multiple) "file.less";: Allows the same file to be imported multiple times, duplicating its content in the output.
  • @import (optional) "non-existent-file.less";: Prevents compilation errors if an optional imported file is not found.

You can also import remote LESS files or standard CSS files:

// Importing a remote LESS file (less common for local development)
// @import (less) "http://example.com/styles/theme.less";
// Importing a standard CSS file (will output as CSS @import)
@import (css) "normalize.css";
// Importing a CSS file to be processed by LESS (if it contains LESS-compatible syntax or you want to use its contents in LESS operations)
// @import (less) "bootstrap-grid.css";
  • Structure your LESS files into logical modules (e.g., variables, base styles, components, layouts).
  • Use a main LESS file (e.g., style.less) to import all other partials.
  • Prefix filenames of partials (files not meant to be compiled directly) with an underscore (e.g., _variables.less, _forms.less). This is a common convention, though not enforced by LESS.
  • Import variable and mixin definitions before files that use them.

Understanding how @import works is fundamental to building scalable and maintainable stylesheets with LESS. For more detailed information, refer to the official LESS documentation on imports.