目录

Overview

描述 (Description)

在样式表中通常可以多次重复使用相同的值。 可以使用variables ,而不是多次使用相同的值。 它使代码维护更容易,并且可以从单个位置控制这些值。

例子 (Example)

以下示例演示了LESS文件中variables的使用 -

<html>
   <head>
     <link rel = "stylesheet" href = "style.css" type = "text/css" />
     <title>LESS variables overview</title>
   </head>
   <body>
      <h1>Welcome to IoWiki</h1>
      <div class = "div1">
         <p>LESS is a CSS pre-processor that enables customizable, 
            manageable and reusable style sheet for web site.</p>
      </div>
      <div class = "div2">
         <p>LESS is a dynamic style sheet language that extends the capability of CSS. 
            LESS is also cross browser friendly.</p>
      </div>
   </body>
</html>

现在创建style.less文件。

style.less

@color1: #ca428b;
.div1 {
   background-color : @color1;
}
.div2 {
   background-color : @color1;
}

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

lessc style.less style.css

执行上面的命令; 它将使用以下代码自动创建style.css文件 -

style.css

h1 {
   color: #D0DC11;
}
.div1 {
   background-color: #ca428b;
   color: #D0DC11;
}
.div2 {
   background-color: #ca428b;
   color: #D0DC11;
}

输出 (Output)

请按照以下步骤查看上述代码的工作原理 -

  • 将上述html代码保存在less_variables_overview.html文件中。

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

较少概述
↑回到顶部↑
WIKI教程 @2018