Less 入门

但行好事,末问前程

CSS 预处理器

Less、 Sass 、 stylus

在这里插入图片描述
Less 官网

<link rel="stylesheet/less" type="text/css" href="styles.less" />


<script src="less.js" type="text/javascript"></script>

Less使用

嵌套应用

Less编译工具

koala官网:www.koala-app.com
推荐 VScode 编译插件 EASYLess

Less注释

以// 开头的注释,不会被编译到Css文件中
以/* */包裹的注释会被编译到Css文件中

Less 变量

注意变量为延迟加载

1、 普通属性值

@color: pink ;


.inner {
    background : @color  ; 
} 

2、自行的属性名或选择器名作为变量(不常用)

在这里插入图片描述
3、 作为URL
@{url}

Less 嵌套规则

1、 结构层次嵌套
2、 & 的使用(避免空格)

Less中的混合 (重要)

混合就是将一系列属性从一个规则集引入到另一个规则集的方式
1.普通混合
2.不带输出的混合
3.带参数的混合
4.带参数并且有默认值的混合
5.带多个参数的混合
6.命名参数
7.匹配模式
8. arguments变量

带参数并且有默认值的混合
在这里插入图片描述

*{
    margin: 0;
    padding: 0;
}
.juzhong(@w:100px,@h:100px,@c:pink){
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    margin: auto;
    width: @w;
    height: @h;
    background: @c;
}
#wrap{
    position: relative;
    margin: 0 auto;
    width: 400px;
    height: 600px;
    border: 1px solid;
}



#box1{
    .juzhong();
    z-index: 1;
}
#box2{
    .juzhong(@h:200px;@c:deeppink;);
}
 

arguments变量

鸡肋,实参类似于数组
在这里插入图片描述
简化下述参数

在这里插入图片描述

Less 继承(重要)

Less 代码
在这里插入图片描述

*{
    margin: 0;
    padding: 0;
}
.common:hover{
    background: pink;
}
.common{
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    margin: auto;
}
.juzhong(@w:100px,@h:100px,@c:pink){
    &:extend(.common all);
    width: @w;
    height: @h;
    background: @c;
}
#wrap{
    position: relative;
    margin: 0 auto;
    width: 400px;
    height: 600px;
    border: 1px solid;
}

#box1{
    .juzhong();
    z-index: 1;
}
#box2{
    .juzhong(@h:200px;@c:deeppink;);
}

编译后CSS 代码

* {
  margin: 0;
  padding: 0;
}
.common:hover,
#box1:hover,
#box2:hover {
  background: pink;
}
.common,
#box1,
#box2 {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto;
}
#wrap {
  position: relative;
  margin: 0 auto;
  width: 400px;
  height: 600px;
  border: 1px solid;
}
#box1 {
  width: 100px;
  height: 100px;
  background: #ffc0cb;
  z-index: 1;
}
#box2 {
  width: 100px;
  height: 200px;
  background: #ff1493;
}

Less 避免编译

less 语法
说明: 带上~“ ” 表示不编译内部代码
calc() 计算方法

*{
    margin: 0;
    padding: 0;
}
#wrap{
    width:~"calc(200px + 700px)";
}

编译结果

css

* {
  margin: 0;
  padding: 0;
}
#wrap {
  width: calc(200px + 700px);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值