less实例

1.目录结构



2.index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>less</title>
    <link  href="styles.css" type="text/css" rel="stylesheet"/>
    <script src="https://cdn.bootcss.com/less.js/3.0.0-pre.4/less.js"></script>
</head>
<body>
    <div class="dialog"></div>
</body>
</html>

3.styles.less

@color: color;
@dialog: .dialog;
@suffix: fix;
// 空格将被忽略,若要保留空格则需要使用单引号或双引号
@hi: '徐 ';
@dear: 同保   ;

.dialog{
  // 用于 rule属性部件,必须使用"@{变量名}" 的形式
  background-@{color}: #888;
  // 用于 rule属性,必须使用"@{变量名}" 的形式
  @{color}: #000eff;
  border-color: @dialog-border-color;
  border-width: @dialog-border-width;
  border-style: solid;
}
// 用于 选择器,必须使用"@{变量名}" 的形式
@{dialog}{
    width: 200px;
}
@{dialog}::after{
  content: '@{hi}@{dear}';    // 用于 字符串拼接,必须使用"@{变量名}" 的形式
}
@h: 1000px;

// 用于 选择器部件,必须使用"@{变量名}" 的形式
.ie-@{suffix}{
  @h: 30px; // 存在作用域,局部作用域优先级高于全局作用域。
  height: @h; // 用于属性值,两种形式均可使用
  line-height: 30px;
}

// 1. 以@作为变量的起始标识,变量名由字母、数字、_和-组成
// 2. 没有先定义后使用的规定;
@dialog-border-color: #f70009;
@dialog-border-width: 10px;
@dialog-border-width: 1px; // 3. 以最后定义的值为最终值;

4.安装和命令行用法

$ npm install -g less
$ lessc styles.less styles.css

5.生成styles.css

.dialog {
  background-color: #888;
  color: #000eff;
  border-color: #f70009;
  border-width: 1px;
  border-style: solid;
}
.dialog {
  width: 200px;
}
.dialog::after {
  content: '徐 同保   ';
}
.ie-fix {
  height: 30px;
  line-height: 30px;
}

6.运行结果




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同保

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值