Less基本用法

变量

@nice-blue: #5b83ad;
@light-blue: @nice-blue + #111;

#header {
  background-color: @nice-blue;
  color: @light-blue;
}

结果为:

#header {
  background-color: #5b83ad;
  color: #6c94be;
}

Mixins

使用类选择器,ID选择器混入

.a, #b {
  color: red;
}
.mixin-class {
  .a();
}
.mixin-id {
  #b();
}

结果为:

.a,
#b {
  color: red;
}
.mixin-class {
  color: red;
}
.mixin-id {
  color: red;
}

不输出Mixins

如果要创建一个mixin,但又不想输出该mixin,则可以在其后加上括号。例如my-other-mixin

.my-mixin {
  color: black;
}
.my-other-mixin() { /** 带有括号,css中不会单独生成此代码**/
  background: white;
}
.class {
  .my-mixin;
  .my-other-mixin;
}

结果为

.my-mixin {
  color: black;
}
.class {
  color: black;
  background: white;
}

!important

.foo (@bg: #f5f5f5, @color: #900) {
  background: @bg;
  color: @color;
}
.unimportant {
  .foo();
}
.important {
  .foo() !important;
}

结果为:

.unimportant {
  background: #f5f5f5;
  color: #900;
}
.important {
  background: #f5f5f5 !important;
  color: #900 !important;
}

继承

.public {
  width: 100px;
  height: 100px;
}
nav ul {
  &:extend(.public);
  list-style: none;
}

结果为:

.public,
nav ul {
  width: 100px;
  height: 100px;
}
nav ul {
  list-style: none;
}

采用继承和Mixins带有括号的形式,都可以减少最终生成的css代码

循环

.generate-level(1);

.generate-level(@i) when(@i <= 5) {
  .level-@{i} {
    margin-left: (@i * 10+10px);
  }
  .generate-level(@i+1);
}
.level-1 {
  margin-left: 20px;
}
.level-2 {
  margin-left: 30px;
}
.level-3 {
  margin-left: 40px;
}
.level-4 {
  margin-left: 50px;
}
.level-5 {
  margin-left: 60px;
}

父选择器 &

.message {
  &-success {
    color: green;
  }
  &-failure {
    color: red;
  }
  &-notice {
    color: yellow;
  }
}

a {
  color: #ff6a00;
  &:hover {
    color: darken(#ff6a00, 10%);
  }
}

结果为:

.message-success {
  color: green;
}
.message-failure {
  color: red;
}
.message-notice {
  color: yellow;
}

a {
  color: #ff6a00;
}
a:hover {
  color: #cc5500;
}

合并

/** 合并 以逗号分隔 **/
.mixin() {
  box-shadow+: inset 0 0 10px #555;
}
.myclass {
  .mixin();
  box-shadow+: 0 0 20px black;
}
/** 合并 以空格分隔 **/
.mixin1() {
  transform+_: scale(2);
}
.myclass1 {
  .mixin1();
  transform+_: rotate(15deg);
}

/** 合并 以逗号分隔 **/
.myclass {
  box-shadow: inset 0 0 10px #555, 0 0 20px black;
}
/** 合并 以空格分隔 **/
.myclass1 {
  transform: scale(2) rotate(15deg);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青菜小王子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值