less语法(一) --- Less语言特性

一、less语言特性

1.1   概览

  作为 CSS 的一种扩展,LESS CSS 不仅向下兼容 CSS 的语法,而且连新增的特性也是使用 CSS 的语法。这样的设计使得学习 LESS 很轻松,而且你可以在任何时候回退到 CSS。

1.2 变量

1.2.1 变量的定义和使用

  定义变量:less定义变量使用 “@变量名: 变量值” 的方式定义;

  使用变量:在需要使用定义好的变量的css属性后面直接使用 “@变量名” 即可。

例如:在less文件中代码如下

@width : 100px;  //定义变量
.w {
     @width;   //使用变量
}

1.2.2 变量的作用域

  如果对同一个变量定义两次的话,在当前作用域中最后一次定义的将被使用。这不 CSS 的机制类似,最后一次定义的值会成为这个属性的值。若定义在嵌套乊中,那么这个变量就叧能服务亍这个嵌套乊内的属性了。变量也是没有顺序可言的,只要页面里有(即可以在变量定义之前使用),都会按顺序覆盖,按顺序加载。例如

@width : 1px;
.meng {
    @width : 2px;
    .long {
        @width : 9000px;
        @width;
        @width : 22px;
    }
     @width;
}

  编译后为:

.meng {
     2px;
}
.meng .long {
     22px; //注意此处是22px,而不是9000px
}

  

例2:在变量定义之前使用:

.header {
     @width;   //在变量定义前使用变量是完全可以的,这和其他语言有区别
}
@ 50px;

  

1.2.3 字符串插值

  Less可以通过@{变量名} 的方式进行字符串插值(字符串的拼接)。例如

@logoUrl = "http://www.XXX.com";
image {
    background-image: url("@{logoUrl}/image/logo.peg")
}

  编译后的CSS为:

image {
    background-image: url("http://www.XXX.com/image/logo.png")
}

  

1.2.4 选择器插值

  Less可以通过 @{变量名} 的方式对CSS的选择器进行插值。例如:

@mySelected: my-class;  //注意此处不能使用双引号或单引号,选择器插值只是单纯的将变量的全部去替换

.@{mySelected}: {   //选择器前面的点 .  表示是类选择器,如果是id选择器,使用#
     100px;
}

  编译后的CSS

.my-class {
    100px;
}

  

1.2.5 media query  作为变量

  如果你希望在 media query 中使用 LESS 变量,你可以直接使用普通的变量方式。因为“~”后面的值是不被编译的,所以可以用作 media query 的参数。小例子如下

:LESS 代码

@singleQuery: ~"(max- 768px)";
div{
    background-color: red;
}
@media screen and @singleQuery {
    div {
        background-color: blue;
    }
}    

  编译后的CSS

div{
    background-color: red;
}
@media screen and (max- 768px) {
    div {
        background-color: blue;
    }
}    

  

1.2.6 用一个变量值的变量

  在定义变量值时使用其它的变量

Less代码:

@meng : 5201314px;    
@loveDay : meng;
div {
    @@loveDay;  //需要使用两个@,相当于@loveDay = meng, 第一个@和@loveDay的结合为@meng 即 5201314px
}

编译后的CSS

div {
   5201314px;  
}

1.3  混合

  在 Less 中,混合可以将一个定义好的 class A (一组已经定义好的样式)轻松的引入到另一个 class B 中,从而简单实现 class B 继承 class A 中的所有属性。我们还可以带参数地调用,就像使用凼数一样

1.3.1 继承类名

  在 LESS 中,可以定义一些通用的属性集为一个 class,然后在另一个 class中去调用这些属性。如果我们现在需要在其他 class 中引入那些通用的属性集,那么我们叧需要在任何 class 中调用就可以了。任何 CSS class, id 属性集都可以以同样的方式引入。例如:

.common-box {   //前面的点 . 表示CSS的选择器,也可以使用 # 等选择器
     100%;  
    height: 50%;   
}

.span {
    display: block;  
}

body {
    background-color: #EEEEE;
    .common-box;
    span{
          .span;
           100px;
          height: 100px;
    }
}

  编译后的CSS 

.common-box {
     100%;  
    height: 50%;   
}

.span {
    display: block;  
}

body {
    background-color: #EEEEEE;
     100%;
    height: 50%;
}

body span {
    display: block;
     100px;
    height: 100px;
}

  

1.3.2 带参数混合和默认值混合

  在 LESS 中,还可以像函数一样定义一个带参数的属性集合,参数可以定义默认值,当不传参数时,使用默认值。然后在其他选择器中像函数一样调用它。示例如下:

.width(@ 10px) {  //此处可以写成@width, 也可以写成@ 10px(表示默认值是10px) ,当调用该属性集合时,不传参数则使用默认值
 	 @width;
	background-color: #000000;
	color: #FFFFFf;
}

#app {
	 500px;
	background-color: #ffffff;
	border: 1px black solid;
	p {
		.width(50%);
		font-size: 25px;
	}
	span {
		.width(200px);
		font-size: 14px;
	}
}

  编译后的CSS

#app {
	 500px;
	background-color: #ffffff;
	border: 1px black solid;
}
#app p {
	 50%;
	background-color: #000000;
	color: #FFFFFf;
	font-size: 25px;
}
#app span {
	 200px;
	background-color: #000000;
	color: #FFFFFf;
	font-size: 14px;
}

  相当于将.width(@width)做为一个函数来调用,传入的参数就是当前选择器选择的元素的宽度。

1.3.3 隐藏属性继承

  如上1.3.1中 .common-box 和 .span编译后会存在于编译后的CSS中,而1.3.2中 .width(@width)编译后不会存在于编译后的CSS中,如果想要定义一些公用的CSS属性集合,又不希望编译后这些属性集合存在于编译后的CSS中,可以在定义是加上括号 () ,这样编译后的这些属性集合就不会存在于CSS中。例如:

.width() {
     100px;
}

div {
    background-color: red;
    .width();
}

  编译后的CSS

div {     //编译后 .width() 不会存在于CSS中
    background-color: red;
     100px;
}    

  

1.3.4 多参数混合

  多个参数可以使用分号戒者逗号分隔,这里推荐使用分号分隔,因为逗号有两重含义:它既可以表示混合的参数,也可以表示一个参数中一组值的分隔符。例如

.mixin(@width) {
    width-1: @width;
}
.mixin(@width; @height:2em) {
    width-2: @width;
    height-2: @height;
}
.mixin(@width; @height; @margin: 2em) {
    width-3: @width;
    height-3: @height;
    margin: @margin @margin @margin @margin;
}
h1 {
    .mixin(red);
}
div {
    .mixin(#000,3px);
}
span {
    .mixin(#fff,3px,5px);
}

  编译后的CSS

h1 {    //从上到下匹配,只要匹配到,就和执行,这里第二个 .mixin虽然有两个参数,但是第二个参数有默认值,所以也能匹配到。这点和java等高级语言不同
    width-1: #ff0000;
    width-2: #ff0000;
    height-2: 2em;
}
div {  //同理
    width-2: #000000;
    height-2: 3px;
    width-3: #000000;
    height-3: 3px;
    margin: 2em 2em 2em 2em;
}
span {
    width-3: #ffffff;
    height-3: 3px;
    margin: 5px 5px 5px 5px;
}

  

1.3.5 @arguments 变量

  使用@arguments可以将所有的参数全部放到一个属性后面。例如:

.transition (@moveStyle :all;@delayTime : 4s;@moveType : ease-in; @moveTime :
2s){
    -webkit-transition: @arguments;
    -moz-transition: @arguments;
    -o-transition: @arguments;
    -ms-transition: @arguments;
    transition: @arguments;
}
div {
    .transition;
}
span {
    .transition (width);
}
h1 {
    .transition (height; 80s);
}
li {
    .transition (all; 80s; ease-out);
}
dd {
    .transition (color; 80s; ease-in-out; 30s);
}

  编译后的CSS

div {
    -webkit-transition: all 4s ease-in 2s;
    -moz-transition: all 4s ease-in 2s;
    -o-transition: all 4s ease-in 2s;
    -ms-transition: all 4s ease-in 2s;
    transition: all 4s ease-in 2s;
}
span {
    -webkit-transition: width 4s ease-in 2s;
    -moz-transition: width 4s ease-in 2s;
    -o-transition: width 4s ease-in 2s;
    -ms-transition: width 4s ease-in 2s;
    transition: width 4s ease-in 2s;
}
h1 {
    -webkit-transition: height 80s ease-in 2s;
    -moz-transition: height 80s ease-in 2s;
    -o-transition: height 80s ease-in 2s;
    -ms-transition: height 80s ease-in 2s;
    transition: height 80s ease-in 2s;
}
li {
    -webkit-transition: all 80s ease-out 2s;
    -moz-transition: all 80s ease-out 2s;
    -o-transition: all 80s ease-out 2s;
    -ms-transition: all 80s ease-out 2s;
    transition: all 80s ease-out 2s;
}
dd {
    -webkit-transition: color 80s ease-in-out 30s;
    -moz-transition: color 80s ease-in-out 30s;
    -o-transition: color 80s ease-in-out 30s;
    -ms-transition: color 80s ease-in-out 30s;
    transition: color 80s ease-in-out 30s;
}

  

1.3.6 !important 关键字

  调用定义好的属性集合时,在后面添加 !important ,则在生产的CSS代码中,该属性集合中的属性都会加上 !important。例如:

.my (@width : 20px; @height : 50px){
    @width;
    height : @height;
}
.meng2 {
    .my !important;
}
.long2 {
    .my(40px) !important;
}

  编译后的CSS

.meng2 {
     20px !important;
    height: 50px !important;
}
.long2 {
     40px !important;
    height: 50px !important;
}

  

1.3.7 高级参数用法

  在属性集合中可以使用 ... 表示多个参数。例如:

.mixin1 (...) { //接收0-N个参数
    padding:@arguments;
}
.mixin2 (@a: #FFFFFF; ...) { //接收0-N个参数
    padding:@arguments;
}
.mixin3 (@a; ...) { //接收1-N个参数
    padding:@arguments;
}

    

1.3.8 模式匹配与 Guard  表达式

  LESS 提供了通过参数值控制 mixin 行为的功能(相当于给定义的属性集合一个标识,当调用时标识一致,则调用)。相当于java中方法的重载,只不过这里需要定义一个标识来控制调用哪一个 .mixin 。示例如下:

.mixin (dark; @color) {   //调用.mixin 若第一个参数是dark,则调用这个属性集合
    color: darken(@color; 10%);
}
.mixin (light; @color) {  //调用.mixin 若第一个参数是light,则调用这个属性集合
    color: lighten(@color, 10%);
}
.mixin (@a; @color) {   //调用.mixin 只要是两个参数,都会调用这个属性集合
    display: block;
}

@switch: light;
.long {
    .mixin(@switch; #888);
}
@switch2 : dark;
.meng {
    .mixin(@switch2; #666);
}

  编译后的CSS代码

.long {
    color: #a2a2a2;
    display: block;    //因为调用是传了两个参数,所有一定会匹配到 .mixin(@a; @color)
}
.meng {
    color: #808080;
    display: block;
}

  

1.3.9 条件混合

  条件混合的语法: .属性集合名(参数集合) when (条件表达式) {属性集合}

1. 通过 LESS  自带的函数判断

.mixin (@a) when (lightness(@a) >= 50%) {  //lightness(@a >= 50%)是混合条件,即判断调用.mixin时传入的@a经过计算后是否大于等于50%,满足条件则混合该属性
    background-color: black; 
}
.mixin (@a) when (lightness(@a) < 50%) {
    background-color: white;2
}
.mixin (@a) {
    color: @a;
}
.meng {
    .mixin(#ddd);
}
.long {
    .mixin(#555);
}

  编译后的CSS

.meng {
    background-color: black;
    color: #dddddd;
}
.long {
    background-color: white;
    color: #555555;
}

  

2. 运算符判断

  Guards 支持的运算符包括:> >= = =<  < .说明一下,true 关键字是唯一被判断为真的值,也就是这两个混合是相等的

3. 多个条件

  语法: .属性集合名(参数集合) when (条件表达式),(条件表达式) {属性集合}

  多个 Guards 可以通过逗号表示分隔,如果其中任意一个结果为 true,则匹配成功。这个相当亍脚本中的或的意思

4. Guards 中的 and 和 not 

  and语法(相当于js中的与): .属性集合名(参数集合) when (条件表达式) and (条件表达式) {属性集合}  //表示两个条件均为true时才混合

  not语法(相当于js中的非): .属性集合名(参数集合) when not (条件表达式) {属性集合}  //当条件为false时混合

1.4 Less的嵌套语法

  Less的嵌套语法是指在一个选择器中可以嵌套其他选择器。例如:

div {
    color: red;
    .container {   //这里是嵌套的 class选择器
        background-color: #CCCCCC;
    }
    ul {
         200px;
        li {
            height: 100px;
        }
    }
}

  编译后的CSS

div {
  color: red;  
}
div .container{
    background-color: #CCCCCC;
}
div ul {
     200px;
}
div ul li {
    height: 100px;  
}

  

1.5  & 的用法

  嵌套语法编译后是父子关系,而 & 连字符编译后是伪类选择器.例如:

div {
    a {
        color: red;
        &:hover {
            color: blue;
        }
        &:focus {
            background-color: #DDDDDD;
        }
    }  
}

  编译后的CSS

div a {
    color: red;
}
div a:hover {
    color: blue;
}
div a:focus {
    background-color: #DDDDDD;
}

  

1.6 LESS  详解之命名空间

  LESS 中也有命名空间的概念。所谓的命名空间,是为了更好组织 CSS 戒者单纯是为了更好的封装,将一些变量戒者混合模块打包起来,一些属性集乊后可以重复使用。例如:

@height:100px;
.meng {
    .meng_button () {
        display: block;
        border: 1px solid black;
        background-color: grey;
        &:hover { background-color: white }
    }
    .meng_tab () {
        height:@height
        100px;
    }
    .meng_citation () {
        @height:200px;
        height:@height
    }
}
div {
    .meng > .meng_tab;
}
h1 {
    .meng > .meng_citation;
}

  编译后的CSS

div {
    height:100px;
     100px;
}
h1 {
    height: 200px;
}

  

原文地址:https://www.cnblogs.com/hebing0415/p/11784886.html