Skip to content

Less - Import Directives

The @import directive in Less is fundamental for organizing your stylesheets. It allows you to split your Less code into multiple, smaller, and more manageable files. These files can then be imported and combined into a single CSS output file during compilation. This practice promotes modularity, reusability, and maintainability in large projects. You can place @import statements almost anywhere in your Less code, though they are commonly placed at the top of a file.

For example, to import a file named _variables.less, you would use: @import "_variables"; or @import "_variables.less";.

Less handles @import statements differently based on the file extension (or lack thereof):

  • .less Extension: If the filename explicitly ends with .less (e.g., @import "theme.less";), Less treats it as a Less file, parses it, and includes its content.
  • No Extension: If the filename has no extension (e.g., @import "buttons";), Less assumes it’s a .less file and tries to import buttons.less.
  • .css Extension: If the filename ends with .css (e.g., @import "normalize.css";), Less treats it as a standard CSS file. By default, it will output a literal CSS @import "normalize.css"; statement in your compiled CSS. (Note: Using import options like (inline) can change this behavior to embed the CSS content directly).
  • Other Extensions: If the file has any other extension (e.g., @import "config.txt";), Less will still attempt to treat it as a Less file and import its content. This is less common and usually implies the content is Less-compatible.
  • Remote Paths: While Less can import files from URLs (e.g., @import "http://example.com/styles.less";), this is generally discouraged for local development due to dependencies on network availability and potential security concerns. Local imports are preferred for project assets.

Example Syntax:

@import "variables"; // Imports 'variables.less'
@import "mixins.less"; // Imports 'mixins.less'
@import "theme.css"; // Outputs '@import "theme.css";' in the CSS file
@import (less) "generic.file"; // Forces 'generic.file' to be treated as Less

Let’s create a small project structure to demonstrate imports.

Assume you have the following HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<title>LESS Import Directives</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<h2 class="main-heading">Example of Import Directives</h2>
<p class="highlighted-text">Welcome to a modular stylesheet example!</p>
</body>
</html>

Create a file named _variables.less (the underscore is a common convention for partial files not meant to be compiled directly):

@primary-color: #007bff;
@base-font-size: 16px;
@text-color: #333;

Create another file named _typography.less:

.main-heading {
color: @primary-color;
font-size: @base-font-size * 1.5;
}
.highlighted-text {
font-size: @base-font-size;
font-style: italic;
}

Now, create your main style.less file, which imports the others:

@import "_variables"; // Imports variables
@import "_typography"; // Imports typography rules
body {
font-family: Arial, sans-serif;
color: @text-color;
margin: 20px;
}
// Additional style in the main file
.highlighted-text {
background-color: #f0f0f0; // This adds to the styles from _typography.less
padding: 10px;
}

Compile style.less to style.css:

lessc style.less style.css

The generated style.css will contain:

/* Content from _variables.less is used but not directly output if only variables */
/* Content from _typography.less */
.main-heading {
color: #007bff;
font-size: 24px;
}
.highlighted-text {
font-size: 16px;
font-style: italic;
}
/* Content from style.less */
body {
font-family: Arial, sans-serif;
color: #333;
margin: 20px;
}
.highlighted-text {
background-color: #f0f0f0;
padding: 10px;
}

When index.html is viewed in a browser:

  • The h2 element with class .main-heading will be blue (@primary-color) and have a font size of 24px, styled by rules from _typography.less using variables from _variables.less.
  • The p element with class .highlighted-text will have styles combined from both _typography.less (font-size, italic) and style.less (background-color, padding). Less merges properties for the same selector from different imported files and the main file.
  • This demonstrates how @import helps in structuring your code logically, making it easier to manage and update.

It’s a best practice to organize Less projects with a main file that imports various partials (often prefixed with an underscore, like _forms.less, _buttons.less) that handle specific aspects or components of your site’s styling.