LESS - 默认变量



描述

默认变量只在变量未被设置时才设置变量。此功能并非必需,因为可以通过之后定义轻松覆盖变量。

示例

以下示例演示了 LESS 文件中默认变量 的用法:

<html>
   <head>
      <link rel = "stylesheet" href = "style.css" type = "text/css" />
      <title>LESS Default Variables</title>
   </head>

   <body>
      <h1>Welcome to Tutorialspoint</h1>
      <p>LESS is a CSS pre-processor that enables customizable, 
      manageable and reusable style sheet for web site.</p>
   </body>
</html>

现在创建文件style.less

style.less

@import "https://tutorialspoint.com/less/lib.less"; // first declaration of @color
@color: green; // this will override @color defined previously
p {
   color : @color;
}

以下代码将从https://tutorialspoint.com/less/lib.less路径导入lib.less文件到style.less中:

lib.less

@color: blue;

您可以使用以下命令将style.less编译为style.css

lessc style.less style.css

执行上述命令;它将自动创建包含以下代码的style.css文件:

style.css

p {
   color: green;
}

输出

按照以下步骤查看上述代码的工作方式:

  • 将上述 html 代码保存到less_default_variables.html文件中。

  • 在浏览器中打开此 HTML 文件,将显示以下输出。

LESS Default Variables
less_variables.htm
广告
© . All rights reserved.