【前端学习笔记】Less初认识

1 篇文章 0 订阅

定义

Less (Leaner Style Sheets 的缩写) 是一门向后兼容的 CSS 扩展语言。

用法

1,变量

@width: 10px;
@height: @width + 10px;

#header {
  width: @width;
  height: @height;
}

2,混合

.bordered {
  border-top: dotted 1px black;
  border-bottom: solid 2px black;
}
#menu a {
  color: #111;
  .bordered();
}

.post a {
  color: red;
  .bordered();
}

3,嵌套

#header {
  color: black;
  .navigation {
    font-size: 12px;
  }
  .logo {
    width: 300px;
  }
}
//&代表当前选择器Parent,比如此时为.clearfix:after
.clearfix {
  display: block;
  zoom: 1;

  &:after {
    content: " ";
    display: block;
    font-size: 0;
    height: 0;
    clear: both;
    visibility: hidden;
  }
}

Nested At-Rules and Bubbling
At-rules such as @media or @supports can be nested in the same way as selectors. The at-rule is placed on top and relative order against other elements inside the same ruleset remains unchanged. This is called bubbling.

.component {
  width: 300px;
  @media (min-width: 768px) {
    width: 600px;
    @media  (min-resolution: 192dpi) {
      background-image: url(/img/retina2x.png);
    }
  }
  @media (min-width: 1280px) {
    width: 800px;
  }
}

编译为:

.component {
  width: 300px;
}
@media (min-width: 768px) {
  .component {
    width: 600px;
  }
}
@media (min-width: 768px) and (min-resolution: 192dpi) {
  .component {
    background-image: url(/img/retina2x.png);
  }
}
@media (min-width: 1280px) {
  .component {
    width: 800px;
  }
}

4,运算

// numbers are converted into the same units
//数字会变为同一个单位
@conversion-1: 5cm + 10mm; // result is 6cm
@conversion-2: 2 - 3cm - 5mm; // result is -1.5cm

// conversion is impossible
//Example of impossible conversion: px to cm or rad to %.
//不同类型的单位符号会被转化为同一个从px-cm-rad-%
@incompatible-units: 2 + 5px - 3cm; // result is 4px

// example with variables
//对于变量会进行自我运算
@base: 5%;
@filler: @base * 2; // result is 10%
@other: @base + @filler; // result is 15%
@base: 2cm * 3mm; // result is 6cm

You can also do arithemtic on colors:

@color: #224488 / 2; //results in #112244
background-color: #112244 + #111; // result is #223355

However, you may find Less’s Color Functions more useful.

calc()特例:
为了与 CSS 保持兼容,calc() 并不对数学表达式进行计算,但是在嵌套函数中会计算变量和数学公式的值。

@var: 50vh/2;

width: calc(50% + (@var - 20px));  // result is calc(50% + (25vh - 20px))

4,Escaping
Anything inside ~“anything” or ~‘anything’ is used as is with no changes except interpolation.

@min768: ~"(min-width: 768px)";
.element {
  @media @min768 {
    font-size: 1.2rem;
  }
}

编译为:

@media (min-width: 768px) {
  .element {
    font-size: 1.2rem;
  }
}


@min768: (min-width: 768px);
.element {
  @media @min768 {
    font-size: 1.2rem;
  }
}

In 3.5+, many cases previously requiring “quote-escaping” are not needed.
5,函数

@base: #f04615;
@width: 0.5;

.class {
  width: percentage(@width); // returns `50%`
  color: saturate(@base, 5%);
  background-color: spin(lighten(@base, 25%), 8);
}

6,namespace和accessors

#bundle() {
  .button {
    display: block;
    border: 1px solid black;
    background-color: grey;
    &:hover {
      background-color: white;
    }
  }
  .tab { ... }
  .citation { ... }
}

可以如此使用:

#header a {
  color: orange;
  #bundle.button();  // can also be written as #bundle > .button
}

7,maps

#colors() {
  primary: blue;
  secondary: green;
}

.button {
  color: #colors[primary];
  border: 1px solid #colors[secondary];
}
This outputs, as expected:

.button {
  color: blue;
  border: 1px solid green;
}

8,作用域

@var: red;

#page {
  @var: white;
  #header {
    color: @var; // white
  }
}

9,注释
块注释和行注释

/* 一个块注释
 * style comment! */
@var: red;

// 这一行被注释掉了!
@var: white;

10,导入(Importing)
“导入”的工作方式和你预期的一样。你可以导入一个 .less 文件,此文件中的所有变量就可以全部使用了。如果导入的文件是 .less 扩展名,则可以将扩展名省略掉:

@import "library"; // library.less
@import "typo.css";

参考自:

https://less.bootcss.com/#

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值