本文章主要简单了解一下less的用法,若想详细了解,请去官网:http://lesscss.cn/
一、语法
1.注释
2.变量
Less中的变量有以下规则:
1.以@作为变量的起始标识,变量名由字母、数字、_和-组成
2.没有先定义后使用的规定;
3.以最后定义的值为最终值;
4.可用于rule值、rule属性、rule属性部件、选择器、选择器部件、字符串拼接;
5.定义时 “@变量名: 变量值;” 的形式;引用时采用 “@变量名” 或 “@{变量名}” 的形式;
6.存在作用域,局部作用域优先级高于全局作用域。
less源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| @color: color; @dialog: .dialog; @suffix: fix;
@hi: 'hello '; @dear: there ; .dialog{ background-@{color}: #888; @{color}: blue; }
@{dialog}{ width: 200px; } @{dialog}::after{ content: ': @{hi}@{dear}!'; } @h: 1000px;
.ie-@{suffix}{ @h: 30px; height: @h; line-height: 30px; }
@dialog-border-color: #666; @dialog-border-width: 10px; @dialog-border-width: 1px;
|
最终输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| .dialog { background-color: #888; color: blue; } .dialog { width: 200px; } .dialog::after { content: ': hello there!'; } .ie-fix { height: 30px; line-height: 30px; }
|
二、gulp编译less
1.安装
全局安装:npm install -g less
项目内安装:npm install gulp-less –save-dev
2.使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| var gulp=require("gulp"); var less=require("gulp-less");
gulp.task("less",function(){ gulp.src('src/css/*.less') .pipe(less()) .pipe(gulp.dest("src/css")); });
gulp.task("watch",function(){ gulp.watch("src/css/*.less",['less']); });
|