开发学院

您的位置:首页>教程>正文

教程正文

LESS 变量概述

LESS 变量概述

  如果相同的属性或值重复多次出现在样式表中,那么你可以使用变量。它使代码维护更容易,这些值可以从单个位置控制。

例子

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

   <body>
      <h1>Welcome to Tutorialspoint</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;
}

  编译less文件

lessc style.less style.css

  得到css文件

/*style.css*/
h1 {
   color: #D0DC11;
}

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

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

输出

 执行以下步骤,看看上面的代码工作:

  •   保存上面的html代码到 less_variables_overview.html 文件。

  •   在浏览器中打开该HTML文件,得到如下输出显示。

less_variables_overview.jpg