HTML和CSS不属于编程语言而是属于标记语言,很难像JS同样定义变量、编写方法、实现模块化开发等。
LESS是一门CSS预处理语言,它扩展了CSS语言,增长了变量、Mixin、函数等特性,使CSS更易维护和扩展。
使用LESS基本上按照这样的步骤:编写LESS代码,使用NODE、JS或者是其余的工具把编写的LESS代码编译成咱们平时看到的CSS代码(由于浏览器是没法解析LESS的语法的,因此编写完成的LESS代码须要进行编译)。css
Less叫作预编译css,写好的less代码浏览器是不能直接渲染的,须要咱们把它编译成为能渲染的css才能够。node
开发环境中
在开发环境下,咱们通常都经过导入less插件来随时编译less代码。web
//注意rel="stylesheet/less"中必须加上less <link rel="stylesheet/less" href="./css/1.less"> <script src="./css/less-2.5.3.min.js"></script>
因为每一次加载页面都须要导入LESS插件,而且把LESS文件从新编译为CSS,很消耗性能,致使页面打开速度变慢。因此在生产环境中,咱们须要事前把LESS文件编译为正常的CSS后,在相应文件中引入,之后用户访问的都是编译好的CSS,为不是拿LESS现编译的。
生产环境下,咱们须要事先把LESS编译成CSS方法:
1)使用Node编译npm
这种方式是目前项目中最经常使用的方式,它是把咱们的LESS文件编译成CSS文件,咱们项目中直接的引入CSS文件便可. (1) 安装node (2) 把LESS模块安装到全局NODE环境中
npm install less -g
(3) 使用命令进行编译
//->把styles.less文件编译成styles.css文件(若是没有这个CSS文件本身会建立) lessc styles.less styles.css //->编译完成的CSS文件是通过压缩的 lessc styles.less styles.min.css -x或者--compress
若是你想要更牛X的压缩,还能够本身单独的设定,下面咱们使用–clean-css。这个须要提早的安装less-plugin-clean-css模块才能够。
//->安装less-plugin-clean-css npm install -g less-plugin-clean-css //->安装成功后就可使用它压缩了 lessc --clean-css styles.less styles.min.css
2)使用编译工具(less在线编译)编程
目前经常使用的编译工具备:Koala(听说目前最流行的)、在线编译(http://tool.oschina.net/less)、SimpLESS等。
less中的变量
和JS中的变量同样,只是LESS的变量定义不是使用VAR而是使用@。浏览器
//用变量存储公用的属性值 @shadowColor: #000; .inner { box-shadow: 0 0 10px 0 @shadowColor; } //用变量存储公用的URL、选择器 @selector: box; @bgImg: "../img"; @property: color; @name: "fung"; //->LESS代码 .@{selector} { width: 100px; height: 100px; @{property}: #000; background: url("@{bgImg}/test.png"); &:after { display: block; content: @@var; } }
混合(Mixins)
所谓的混合实际上是把某个选择器中的样式拷贝一份拿过来使用less
//->LESS代码 .public { width: 100px; height: 100px; } nav ul { .public; list-style: none; } //->编译为CSS的结果 .public { width: 100px; height: 100px; } nav ul { width: 100px; height: 100px; list-style: none; }
咱们发现其实nav ul是把public中设定的样式属性值copy了一份到本身的样式中。若是你想在编译完成的结果中不输出public这个样式的结果,只须要按照下述的代码编写便可:编程语言
//->LESS代码 .public() {//->在选择器后面加上()就能够不编译这个样式了 width: 100px; height: 100px; } nav ul { .public; list-style: none; } //->编译为CSS的结果 nav ul { width: 100px; height: 100px; list-style: none; }
3.extend模块化
虽然在上述的案例中,nav ul把public中的样式继承了过来,可是原理倒是把代码copy一份过来,这样编译后的CSS中依然会存留大量的冗余CSS代码,为了不这一点,咱们可使用extend伪类来实现样式的继承使用。 ``` //->LESS代码 .public { width: 100px; height: 100px; } nav ul { &:extend(.public); list-style: none; } //->编译为CSS的结果 .public, nav ul { width: 100px; height: 100px; } nav ul { list-style: none; } ``` 或者 ``` //->LESS代码 .public { width: 100px; height: 100px; } nav ul:extend(.public) { list-style: none; } //->编译为CSS的结果和第一种写法同样 ```
4.命名空间和做用域函数
在LESS中定义了命名空间就建立了一个私有的做用域,在这个私有做用域中使用的变量都是先看一下本身做用域中有没有,没有的话,在向上一级查找(相似于JS的做用域链) ``` //->LESS代码 @color: #ccc; .box { @color: #eee; .gray { color: @color; } } .box2 { .gray { color: @color; } } //->编译为CSS的结果 .box .gray { color: #eee; } .box2 .gray { color: #ccc; } ```
5.!important
在调用的混合集后面追加 !important 关键字,可使混合集里面的全部属性都继承 !important.
//->LESS代码 @color: #ccc; .box { @color: #eee; .gray { color: @color; } } nav ul { .box !important; } //->编译为CSS的结果 .box .gray { color: #eee; } nav ul .gray { color: #eee !important; }
6.函数
如同JS同样,LESS也能够向函数同样设定形参数,这个技巧在咱们的项目中会被常常的使用到,例如:处理CSS3的兼容问题
//->LESS代码 .transition(@property:all;@duration:1s;@function:linear;@delay:0s;) { -webkit-transition: @property @duration @function @delay; -moz-transition: @property @duration @function @delay; -ms-transition: @property @duration @function @delay; -o-transition: @property @duration @function @delay; transition: @property @duration @function @delay; } .box1 { .transition; } .box2 { .transition(@duration: 2s); } .box3 { .transition(@duration: 2s; @property: width;); } //->编译为CSS的结果 .box1 { -webkit-transition: all 1s linear 0s; -moz-transition: all 1s linear 0s; -ms-transition: all 1s linear 0s; -o-transition: all 1s linear 0s; transition: all 1s linear 0s; } .box2 { -webkit-transition: all 2s linear 0s; -moz-transition: all 2s linear 0s; -ms-transition: all 2s linear 0s; -o-transition: all 2s linear 0s; transition: all 2s linear 0s; } .box3 { -webkit-transition: width 2s linear 0s; -moz-transition: width 2s linear 0s; -ms-transition: width 2s linear 0s; -o-transition: width 2s linear 0s; transition: width 2s linear 0s; }
此外咱们须要值得注意的是,LESS中也有arguments。
//->LESS代码 .transition(@property:all;@duration:1s;@function:linear;@delay:0s;) { -webkit-transition: @arguments; transition: @arguments; } .box1 { .transition; } //->编译为CSS的结果 .box1 { -webkit-transition: all 1s linear 0s; transition: all 1s linear 0s; }