简介:
什么是css预处理器?
用某一种语言用来为css增加一些动态语言的特性(变量、函数 、继承等),具有使css更简洁、适应性更强,代码更直观等诸多好处
常见的css预处理器
less、scss、stylus
什么是less(leaner style sheets)?
一门css预处理语言,为css赋予了动态语言的特性
扩展了css语言,增加了变量、混合、嵌套、函数和运算等特性,使css更易于维护和扩展
less基本使用:
1.在浏览器中直接运行
编写less文件-->引入less文件-->引入less.js-->运行
注意:一定要先引入less.css,在引入less.js;如果less代码是写到单独的文件中,一定要在服务端环境下运行才生效
<link rel="stylesheet/less" type="text/css" href="./变量.less" />
<script src="../js/less.js"></script>
2.提前预编译
编写less文件-->利用工具转换成css文件-->引入css文件
考拉客户端:http://koala-app.com/index-zh.html
开源中国:https://tool.oschina.net/less
构建工具配置loader自动编译:webpack
这样就可以无需引入less.js,无需在服务端运行了
变量(Variables)
- less中定义变量的格式: @变量名:值;
- less中使用变量的格式:@变量名;
- 定义在{}外面的变量是全局变量,什么地方都可以用;定义在{}里面的变量是局部变量,只能在{}里使用
- less中的变量是延迟加载的,因此既可写到后面也可写到前面
- 和js一样不同作用域的变量不会相互影响,只有相同作用域的变量才会相互影响;和js一样在访问变量时会采用就近原则
基本使用方式:
@width: 100px;
@height: @width + 10px;
@bgColor:#f40;
@size:20px;
@font-size:"size";
@cl:pink ;
@color:"cl";
@f:font-size;
div{
width: @width;
height: @height;
background-color: @bgColor;
}
p {
font-size:@@font-size;
color: @@color;
}
编译为:
div {
width: 100px;
height: 110px;
background-color: #f40;
}
p {
font-size: 20px;
color: pink;
}
混合(Mixins)
混合(Mixin)是一种将一组属性从一个规则集包含(或混入)到另一个规则集的方法。
在预处理的时候less会自动将用到的封装好的类中的代码拷贝过来
假设我们定义了一个类(class)如下:
.bordered {
border-top: dotted 1px black;
border-bottom: solid 2px black;
}
如果我们希望在其它规则集中使用这些属性呢?没问题,我们只需像下面这样输入所需属性的类(class)名称即可,如下所示:
#menu a {
color: #111;
.bordered()/.bordered;
}
.post a {
color: red;
.bordered()/.bordered;
}
带参数的混合:
在此还可设置默认值,若未传值就使用默认值,反之使用所传入的值
还可给混合的指定形参传递数据
.whc(@w:50px,@h:50px,@c:pink){
width: @w;
height: @h;
color: @c;
}
.box{
.whc(100px,100px,#f40);
}
.box1{
.whc();
}
.box2{
.whc(@c:red);
}
less混合的可变参数:
.animate(@name,@time,...){
transition:@arguments;
}
div{
.animate(all,4s,linear,0s);
}
编译后:
div {
transition: all 4s linear 0s;
}
less中混合的匹配模式:
.triangle(Up,@width,@color){
width: 0;
height: 0;
border-width: @width;
border-style: solid solid solid solid;
border-color: @color transparent transparent transparent;
}
.triangle(Right,@width,@color){
width: 0;
height: 0;
border-width: @width;
border-style: solid solid solid solid;
border-color: transparent @color transparent transparent;
}
.triangle(Down,@width,@color){
width: 0;
height: 0;
border-width: @width;
border-style: solid solid solid solid;
border-color: transparent transparent @color transparent;
}
.triangle(Left,@width,@color){
width: 0;
height: 0;
border-width: @width;
border-style: solid solid solid solid;
border-color: transparent transparent transparent @color;
}
div{
.triangle(Down,40,green);
}
p{
.triangle(Left,40,red);
}
编译后:
div {
width: 0;
height: 0;
border-width: 40;
border-style: solid solid solid solid;
border-color: transparent transparent #008000 transparent;
}
p {
width: 0;
height: 0;
border-width: 40;
border-style: solid solid solid solid;
border-color: transparent transparent transparent #ff0000;
}
less中混合通用匹配模式:
@_:通用匹配模式,无论同名的哪一个混合被匹配了,都会先执行通用匹配模式中的代码
.triangle(@_,@width,@color){
width: 0;
height: 0;
border-style: solid solid solid solid;
}
.triangle(Up,@width,@color){
border-width: @width;
border-color: @color transparent transparent transparent;
}
.triangle(Right,@width,@color){
border-width: @width;
border-color: transparent @color transparent transparent;
}
.triangle(Down,@width,@color){
border-width: @width;
border-color: transparent transparent @color transparent;
}
.triangle(Left,@width,@color){
border-width: @width;
border-color: transparent transparent transparent @color;
}
div{
.triangle(Down,40,green);
}
p{
.triangle(Left,40,red);
}
编译后:
div {
width: 0;
height: 0;
border-width: 40;
border-style: solid solid solid solid;
border-color: transparent transparent #008000 transparent;
}
p {
width: 0;
height: 0;
border-width: 40;
border-style: solid solid solid solid;
border-color: transparent transparent transparent #ff0000;
}
嵌套(Nesting)
Less 提供了使用嵌套(nesting)代替层叠或与层叠结合使用的能力。假设我们有以下 CSS 代码:
#header {
color: black;
}
#header .navigation {
font-size: 12px;
}
#header .logo {
width: 300px;
}
用 Less 语言我们可以这样书写代码:
#header {
color: black;
.navigation {
font-size: 12px;
}
.logo {
width: 300px;
}
}
用 Less 书写的代码更加简洁,并且模仿了 HTML 的组织结构。
你还可以使用此方法将伪选择器(pseudo-selectors)与混合(mixins)一同使用。下面是一个经典的 clearfix 技巧,重写为一个混合(mixin) (& 表示当前选择器的父级):
.clearfix {
display: block;
zoom: 1;
&:after {
content: " ";
display: block;
font-size: 0;
height: 0;
clear: both;
visibility: hidden;
}
}
运算(Operations)
算术运算符 +、-、*、/ 可以对任何数字、颜色或变量进行运算
如果可能的话,算术运算符在加、减或比较之前会进行单位换算。
计算的结果以最左侧操作数的单位类型为准。
如果单位换算无效或失去意义,则忽略单位。
无效的单位换算例如:px 到 cm 或 rad 到 % 的转换。
在CSS3中新增了一个calc函数,可以实现简单的+-*/
// 所有操作数被转换成相同的单位
@conversion-1: 5cm + 10mm; // 结果是 6cm
@conversion-2: 2 - 3cm - 5mm; // 结果是 -1.5cm
// conversion is impossible
@incompatible-units: 2 + 5px - 3cm; // 结果是 4px
// example with variables
@base: 5%;
@filler: @base * 2; // 结果是 10%
@other: @base + @filler; // 结果是 15%
乘法和除法不作转换。因为这两种运算在大多数情况下都没有意义,一个长度乘以一个长度就得到一个区域,而 CSS 是不支持指定区域的。Less 将按数字的原样进行操作,并将为计算结果指定明确的单位类型。
@base: 2cm * 3mm; // 结果是 6cm
你还可以对颜色进行算术运算:
@color: #224488 / 2; //结果是 #112244
background-color: #112244 + #111; // 结果是 #223355
示例:
@the-border: 20px;
@base-color: #333;
@red: #e0171f;
.box1 {
color: @base-color * 3;
border: @the-border - 10 (@the-border - 2) / 2 @the-border + 2 @the-border * 2;
}
编译后:
.box1 {
color: #999999;
border: 10px 9px 22px 40px;
}
转义(Escaping)
转义(Escaping)允许你使用任意字符串作为属性或变量值。任何 ~"anything" 或 ~'anything' 形式的内容都将按原样输出,除非 interpolation。
@min768: ~"(min-width: 768px)";
.element {
@media @min768 {
font-size: 1.2rem;
}
}
编译为:
@media (min-width: 768px) {
.element {
font-size: 1.2rem;
}
}
函数(Functions)
Less 内置了多种函数用于转换颜色、处理字符串、算术运算等。函数手册(opens new window)
函数的用法非常简单。
下面这个例子将介绍如何利用 percentage 函数将 0.5 转换为 50%,将颜色饱和度增加 5%,以及颜色亮度降低 25% 并且色相值增加 8 等用法:
@width: 0.5;
.class {
width: percentage(@width); // returns `50%`
}
映射(Maps)
从 Less 3.5 版本开始,你还可以将混合(mixins)和规则集(rulesets)作为一组值的映射(map)使用。
#colors() {
primary: blue;
secondary: green;
}
.button {
color: #colors[primary];
border: 1px solid #colors[secondary];
}
输出符合预期:
.button {
color: blue;
border: 1px solid green;
}
作用域(Scope)
Less 中的作用域与 CSS 中的作用域非常类似。首先在本地查找变量和混合(mixins),如果找不到,则从“父”级作用域继承。
@var: red;
#page {
@var: white;
#header {
color: @var; // white
}
}
与 CSS 自定义属性一样,混合(mixin)和变量的定义不必在引用之前事先定义。
因此,下面的 Less 代码示例和上面的代码示例是相同的:
@var: red;
#page {
#header {
color: @var; // white
}
@var: white;
}
注释(Comments)
块注释和行注释都可以使用:
/* 一个块注释
* style comment! */
@var: red;
// 这一行被注释掉了!
@var: white;
注意:less中的单行注释不会参与预处理,也就是单行注释不会出现在编译好的css文件中
导入(Importing)
“导入”的工作方式和你预期的一样。你可以导入一个 .less 文件,此文件中的所有变量就可以全部使用了。如果导入的文件是 .less 扩展名,则可以将扩展名省略掉:
@import "library"; // library.less
@import "typo.css";
继承(extend)
混合是直接拷贝过来而继承是通过并集选择器的方式
.center{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
.father:extend(.center){
background: red;
.son:extend(.center){
background: darkorange;
}
}
编译后:
.center
.father,
.father .son {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
.father{
background: red;
}
.son{
background: darkorange;
}
条件判断
less中可以通过when给混合添加执行限定条件,只有满足才会执行混合中的代码
when表达式中可以使用比较运算符、逻辑运算符、检查函数来进行条件判断
(),()相等于js中的逻辑||:
()and()相等于js中的逻辑&&:
.size(@width,@height) when (@width=100px),(@height=100px) {
width: @width;
height: @height;
}
div{
.size(100px,80px);
}
.box{
.size(90px,100px);
}
编译后:
div {
width: 100px;
height: 80px;
}
.box {
width: 90px;
height: 100px;
}
使用判断类型的内置函数:
.size(@width,@height) when (ispixel(@width)) {
width: @width;
height: @height;
}
div{
.size(100px,50px);
//.size(100rem,50px);不会执行
}