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 "http://www.tutorialspoint.com/less/lib.less"; // first declaration of @color
@color: green; // this will override @color defined previously
p {
   color : @color;
}

以下代码将从 https://www.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.html