Skip to content

Less - 安装

本章将指导你安装 Less,这是一个 CSS 预处理器(CSS preprocessor),它通过变量、混入(mixins)、运算(operations)和函数(functions)等动态行为扩展了 CSS。使用 Less 最常见的方式是使用 Node.js 将其编译为标准 CSS。

  • Node.js 和 npm: 通过命令行或构建工具编译 Less 需要 Node.js。npm (Node Package Manager) 包含在 Node.js 中,用于安装 Less 本身。
  • 文本编辑器(Text Editor): 用于编写代码的任何文本编辑器(例如,VS Code, Sublime Text, Atom)。

如果你尚未安装 Node.js,请访问 https://nodejs.org/ 并下载适用于你操作系统的安装程序(Windows、macOS 或 Linux)。通常建议使用 LTS(长期支持)版本以获得稳定性。按照安装程序提供的安装说明进行操作。

安装 Node.js 后,打开终端(Windows 上是命令提示符 Command Prompt,macOS/Linux 上是 Terminal),然后输入以下命令来验证安装:

node -v
npm -v

这些命令应该分别显示已安装的 Node.js 和 npm 版本。

你可以将 Less 全局安装在系统上,或在项目内本地安装。对于初学者和一般的命令行使用,全局安装很常见。

全局安装:

npm install -g less

-g 标志将 Less 全局安装,使 lessc (Less 编译器) 命令在你的终端的任何目录中都可用。

本地(项目)安装(推荐用于项目):

# Navigate to your project directory first
# cd /path/to/your/project
npm install less --save-dev

这将 Less 安装为当前项目的开发依赖项,并将其添加到你的 package.json 文件中。这是项目的首选方法,因为它确保项目中的所有开发者使用相同版本的 Less。通常,你会通过 npm 脚本来运行本地安装的 Less。

安装(全局或本地)后,你可以通过检查其版本来验证 Less 是否正确安装:

lessc -v

这应该输出已安装的 Less 版本,例如:lessc 4.1.3 (Less Compiler) [JavaScript]。具体的版本号可能不同。

我们来创建一个基本的 Less 文件并将其编译为 CSS。

1. 创建一个 HTML 文件 (index.html):

Section titled “1. 创建一个 HTML 文件 (index.html):”
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Less Example</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<header>
<h1>Welcome to Modern Less</h1>
</header>
<main>
<p>Dynamic styling made easy!</p>
<button class="btn-primary">Click Me</button>
</main>
</body>
</html>

2. 在同一目录中创建一个 Less 文件 (style.less):

Section titled “2. 在同一目录中创建一个 Less 文件 (style.less):”
// Define some variables
@primary-color: #3498db; // A nice blue
@secondary-color: #2ecc71; // A calm green
@text-color: #333;
@base-font-size: 16px;
// Global styles
body {
font-family: Arial, sans-serif;
color: @text-color;
line-height: 1.6;
margin: 20px;
}
header h1 {
color: @primary-color;
text-align: center;
}
main p {
font-size: @base-font-size * 1.1;
}
// A reusable mixin for button styles
.button-base() {
display: inline-block;
padding: 10px 15px;
border: none;
border-radius: 4px;
color: white;
cursor: pointer;
text-decoration: none;
font-size: @base-font-size;
}
.btn-primary {
.button-base(); // Use the mixin
background-color: @primary-color;
&:hover {
background-color: darken(@primary-color, 10%);
}
}

打开终端,导航到你保存 style.less 和 index.html 的目录,然后运行:

lessc style.less style.css

这个命令将处理 style.less 并在同一目录中生成一个 style.css 文件。如果 style.css 已经存在,它将被覆盖。

body {
font-family: Arial, sans-serif;
color: #333333;
line-height: 1.6;
margin: 20px;
}
header h1 {
color: #3498db;
text-align: center;
}
main p {
font-size: 17.6px;
}
.btn-primary {
display: inline-block;
padding: 10px 15px;
border: none;
border-radius: 4px;
color: white;
cursor: pointer;
text-decoration: none;
font-size: 16px;
background-color: #3498db;
}
.btn-primary:hover {
background-color: #217dbb;
}

在 Web 浏览器中打开 index.html。你应该看到样式已应用:标题为蓝色,以及一个有样式的蓝色按钮。

每次更改后手动重新运行 lessc 效率低下。在现代开发中,你通常会:

  • 使用文件监听器(file watcher)在保存时自动重新编译 Less 文件。可以配置 nodemon、chokidar-cli 或 less-watch-compiler 等工具来实现此目的。
  • 使用 Webpack (使用 less-loader)、Parcel、Gulp (使用 gulp-less) 或 npm 脚本等工具将 Less 编译集成到构建过程中。
  • lessc: command not found: 如果你全局安装了 Less 并遇到此错误,可能是你系统的 PATH 环境变量未包含 npm 全局二进制文件目录。在线搜索“npm global path not found [你的操作系统]”以查找解决方案。或者,尝试使用 npx lessc ... 运行,它会使用本地或全局安装的包。
  • 项目依赖项(Project Dependencies): 对于团队项目或确保一致的环境,始终本地安装 Less(npm install less --save-dev)。然后,你可以使用 package.json 中的 npm 脚本来运行 lessc,例如:"scripts": { "compile:less": "lessc src/style.less dist/style.css" },并使用 npm run compile:less 执行。
  • 官方文档(Official Documentation): 有关最新信息、高级用法和语言功能,请始终参考 Less.js 官方网站:http://lesscss.org/