1、less基础语法css
一、声明变量:@变量名:变量值;
使用变量:@变量名;web
二、混合(Mixins)
1)无参混合
声明: .class{}
调用:在选择器中,使用.class;直接调用less
2)有参无默认值混合:
声明:.class(@param){}
调用:.class(paramValue);
3)有参有默认值混合:
声明:.class(@param:10px){}
调用:.class(paramValue);post
>>>若是声明时,没有给参数赋默认值,则调用时,必须赋值,不然报错
>>>无参混合,实际上就是一个普通的class选择器,会被编译到CSS文件中;
而有参混合,在编译时,不会出如今CSS文件中。字体
@color:#ff0000;//声明变量
@length:100px;//声明变量
#div1{ width: 100px; height:@length;//使用变量
background-color: @color; } //无参混合
.borderRadius{ border-radius: 10px; -webkit-border-radius: 10px; -moz-border-radius: 10px; } //有参无默认值混合
.borderRadius1(@radius){ border-radius: @radius; -webkit-border-radius: @radius; -moz-border-radius: @radius; } //有参有默认值混合
.borderRadius2(@radius:10px){ border-radius: @radius; -webkit-border-radius: @radius; -moz-border-radius: @radius; } .class(){ width: 10px; height: 10px; .borderRadius2(); }
【变量使用原则】:
屡次频繁出现的值,后期须要统一修改的值,牵扯到数值运算的值,推荐使用变量;
【less中的变量类型】
在CSS中出现的属性值,在less中均可以用做变量名
1)数值类:不带单位的 123 带单位的 1px
2)字符串:带引号的 "hahhaha" 不带引号的 red #fffff
3)颜色类:red #ff0000 RGB(255,0,0)
4)值列表类型:多个值用逗号或空格分隔 10px solid redspa
三、less中的匹配模式
1)声明:
@pipei(@条件1,参数){}code
@pipei(@条件2,参数){}blog
@pipei(@_,参数){}
2)调用:
@pipei(条件的值,参数的值){}
3)匹配规则
根据调用时输入的条件值,去寻找与之匹配的混合执行。
@_表示无论匹配成功与否,都会执行的选项继承
/*匹配模式*/ .pipei(lefts,@width:10px){ margin-left: @width; } .pipei(rights,@width:10px){ margin-right: @width; } .pipei(tops,@width:10px){ margin-top: @width; } .pipei(bottoms,@width:10px){ margin-bottom: @width; } .pipei(@_,@width:10px){//@_表示无论匹配成功与否,都会执行的选项
padding: 10px; } @postion:lefts; .class1{ .pipei(@postion,20px); //调用:@pipei(条件的值,参数的值){}
}
四、@arguments 特殊变量:
在混合中,@arguments表示混合传入的全部参数。@arguments中的多个参数用空格分隔。
.border(@width,@style,@color){
border:@arguments;//-->border:@width @style @color;
}事件
.argu(@width,@style,@color){ // border:@width @style @color;
border: @arguments; } .class2{ .argu(10px,solid,red); //调用:分别传入三个参数的值
}
五、LESS中的加减乘除运算:
LESS中的全部变量和数值,能够进行+ - * /运算。
须要注意的是,当颜色值运算时,红绿蓝分三组运算。组内单独计算,组间互不干扰。
六、LESS中的嵌套
LESS中容许CSS选择器按照HTML代码的结构进行嵌套。
1)less中的嵌套默认是后代选择器,若是须要子代选择器,须要在前面加>
2)&符号,表示这个&所在的上一层选择器。好比上述&,表示 section ul:hover
/* * less中的嵌套 */ #section{ width: 800px; height: 200px; background-color: #ccc; >p{//>表示子代选择器
color: red; font-weight: bold;//加粗
} ul{ padding: 0; list-style: none; >li{ float: left; width: 100px; height: 50px; background-color: yellow; text-align: center; &:hover{//&表示上一层选择器(#section ul li:hover)
background-color: green; } } } }
2、sacc基础语法
一、scss中的变量
1)声明变量:$变量名:变量值;
SCSS中,容许将变量嵌套在字符串中,可是变量必须使用#{}包裹
eg:border-#{$left}:10px solid red;
$width:100px;//scss中的声明变量
$position:left; #div1{ width: $width; height: $width/10;
background-color: red; border-#{$position}:10px solid yellow; }
二、SCSS中的运算,会将单位进行运算。使用时需注意最终的单位是否正确。
eg:10px * 10px=100 px*px;
三、SCSS中的嵌套:选择器嵌套、属性嵌套、伪类嵌套
1)选择器嵌套ul{ li{} }
嵌套默认表示后代选择器,若是须要子代选择器,能够在选择器前面加>
能够在选择器的大括号中,使用&表示上一层的选择器。
2)伪类嵌套: li{ $:hover}
在选择器的{}中,使用&配合伪类事件,能够表示当前选择器的伪类
3)属性嵌套:font:{size:18px} -->font-size:18px;
对于属性名有-分割为多段的属性,可使用属性嵌套。属性名的前半部分必须紧跟:,才能
用{}包裹属性的后半部分。
section{ background-color: #CCCCCC; p{ color: red; } ul{ padding: 0; li{ list-style: none; //$=="section ul li"
font:{ size: 16px; weight:bold; family:"微软雅黑"; style:"italic"//字体为斜体
} } } }
四、混合宏、继承、占位符
1)混合宏:声明@mixin声明混合宏,在其余选择器中使用@include调用混合宏
@minxin hunhe{} .class{@include hunhe}
@minxin hunhe(@param){} .class{@include hunhe(value)}
@minxin hunhe(@param:value){} .class{@include hunhe()}
//继承
.class1{ color:red; } .class{ @extend .class1; background-color: yellow; } //混合宏
@mixin hunhe($color:red){ color: $color; } .class3{ @include hunhe(green); background-color: yellow; } .class4{ @include hunhe; background-color: blue; } //占位符
%class1{ color: red; } .class4{ @extend %class1; background-color: yellow; } .class5{ @extend %class1; background-color: green; }
1)混合宏:声明时,能够有参数,也能够没有参数。参数能够有默认值,也能够没有默认值。
可是调用时,必须符合声明时的要求。与LESS中的混合相同。
>>>优势:1)能够传参
2)不会产生同名class
>>>缺点:调用时,会把混合宏中的全部代码copy到选择器中,产生大量重复代码
2)继承:声明一个普通的class,在其余选择器中使用@extend 继承这个class
.class1{} .class1{ @extend .class1;}
>>>优势:将相同代码提取到并集选择其中,减小冗余代码;
>>>缺点:1)不能传参
2)会生出一个多于的class
3)占位符:使用%声明占位符,在其余选择器中使用@extend继承占位符;
%class1{} .class2{@extend %class1;}
>>>优势:1)将相同代码提取到并集选择其中,减小冗余代码;
2)不会生出一个多于的class。
>>>缺点:不能传参
五、if条件结构
条件结构须要写在选择器里面,条件结构的大括号直接包裹样式属性。
@if 条件{}
@else{}
//scss中的条件语句
.class6{ width: 100px; height: 100px; @if 1>2{ background-color: yellow;//设置背景色
}@else{ background-color: green; } }
六、for循坏:
@for $i from 1 to 10{} //不包含10
@for $i from 1 through 10{} //包含10
//for循坏 @for $i 1 through 10{ .border-#{$i}{ border: #{$i}px solid red;//设置外边框 } }
七、while循坏:
$i:@extend;
@while $i<10{
$i:$i+1;
}
//while循坏
$i:0; @while $i<10{ .while-#{$i}{ border: #{$i}px solid red; } $i:$i+1; }
八、each 循坏遍历
@each $item in a,b,c,d{
//$item表示a,b,c,d的每一项
}
//each 循坏遍历
$i:0; @each $item in c1,c2,c3,c4{ $i:$i+1; .#{$item}{ border: #{$i}px solid red; } }