less学习笔记

  1. 两种注释方法
  (1)  /**/   该方法在less转化为css时会显示
  (2)  // 该方法不会在css中显示
  1. 变量:使用 @ 定义
 @color : red;   /* 1.定义普通变量 */
 @m : margin;   /*  2.将属性定义为变量,调用时用{}包裹,不常用 */
 @s : 'http://127.0.0.1';    /*  3.作为url前缀 */
 #main {   /*  4.变量会延迟加载 */
    @var : 0 ;
    background-color : @color;
    @{m} : 0;
    background-img : url('@{base}/img/2.jpg');
    .inner {
        @var : 2;
        three: @var;  /*   3   */
        @var : 3;
    }
 }
  1. 嵌套规则:父子层关系,使用 &
#body {
  width: 300px;
  height: 300px;
  background-color: @color;
  display: flex;
  justify-content: center;
  align-items: center;
  .inner {
    width: 100px;
    height: 100px;
    /* 使用&时:#body .inner:hover */
    &:hover {
      // &代表#body .inner
      background-color: red;
    }
    /* 不使用&:#body .inner :hover */
    :hover {
      background-color: red;
    }
  }
}
  1. 混合:代码重用
  • 普通混合:会被编译为css
.center {
  position: absolute; 
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: red;
}
  • 不带输出的混合:使用(),此时不会被转化为css
.center () {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: red;
}

/* 调用 */

#top {
  .top1 {
    .center  // 直接调用即可;
  }
  .top12 {
    .center;
  }
}
  • 带参数的混合
.left (@t,@l,@b,@r) {
  position: relative;
  top: @t;
  left: @l;
  bottom: @b;
  right: @r;
}
/* 调用 */
#wrap {
  .left(100px, 100px,100px,100px);
}
  • 带参数且有默认值的混合
.content (@l,@r,@t:10px,@b:10px) {
 /* 带默认值必须放置于后面 */
  position: relative;
  top: @t;
  left: @l;
  bottom: @b;
  right: @r;
}
#inner {
  .content(50px, 50px);
}
  • 命名参数
#outter {
  /* 命名参数,可以无视放置位置 */
  .content(@r:20px,@l:60px,@t:10px);
}
  • 匹配模式
.main (@_) {
  /* 存放公共属性 */
  width: 0;
  height: 0;
}
/* 四个方向 */
.main (L,@w,@c) {
  border-width: @w;
  border-style: solid;
  border-color: transparent transparent transparent @c;
}
.main (R,@w,@c) {
  width: 0;
  height: 0;
  border-width: @w;
  border-style: solid;
  border-color: transparent @c transparent transparent;
}
.main (T,@w,@c) {
  width: 0;
  height: 0;
  border-width: @w;
  border-style: solid;
  border-color: @c transparent transparent transparent;
}
.main (B,@w,@c) {
  width: 0;
  height: 0;
  border-width: @w;
  border-style: solid;
  border-color: transparent transparent @c transparent;
}

#inner {
  .main(B,@w:40px, @c: red); /*  匹配模式,使用@_存放公共方法 */
}
  • arguments变量:替代书写变量
.border-radius (@x:0,@y:0,@blur:1px,@color:red) {
  box-shadow: @arguments;/* 省去了写参数的步骤,直接使用arguments作为替代 */
  -webkit-box-shadow: @arguments; /* 兼容写法 */
  -moz-box-shadow: @arguments;
}
/* 调用 */
#bor {
  .border-radius(2px,5px,15px,green);
}
  1. 运算:仅需一方带单位即可
#container {
  width: 100px;
  height: 100px;
  .border-radius(2+15px, 5px, 15px, red); // box-shadow: 17px 5px 15px red;
}
  1. 引入文件:@import ‘路径’
    @import ‘…/1.less’

  2. less继承 :extend 继承方法不支持带参数 所有的继承都必须在选择器的末尾。

.toper {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: red;
}

.border:extend(.toper) {   /* border自身的属性  */
  width: 100px;
  height: 100px;
}
  1. 原样输出:~
#con{
  height: 100px;
  height: 100px;
  margin: 10px;
  border-width: ~"50+10px";  /* 进行原样输出 */
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值