Less 备忘清单

本备忘单旨在快速理解 Less 所涉及的主要概念,显示了它的常用方法使用清单。

入门

介绍

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

在 Node.js 环境中使用 Less

  1. $ npm install -g less
  2. $ lessc styles.less styles.css

在浏览器环境中使用 Less

  1. <link rel="stylesheet/less" type="text/css" href="styles.less" />
  2. <script src="https://cdn.jsdelivr.net/npm/less@4" ></script>

变量(Variables)

  1. @width: 10px;
  2. @height: @width + 10px;
  3. #header {
  4. width: @width;
  5. height: @height;
  6. }

编译 css 为:

  1. #header {
  2. width: 10px;
  3. height: 20px;
  4. }

另见: 变量的更多信息

混合(Mixins)

```less {1,8,13} .bordered { border-top: dotted 1px black; border-bottom: solid 2px black; }

menu a {

color: #111; .bordered(); }

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

  1. 另见: [混合(Mixin)的更多信息](https://lesscss.org/features/#mixins-feature)
  2. ### 嵌套(Nesting)
  3. ```css
  4. #header {
  5. color: black;
  6. }
  7. #header .navigation {
  8. font-size: 12px;
  9. }
  10. #header .logo {
  11. width: 300px;
  12. }

👇👇 更改为 less 的写法 ✅ 👇👇

  1. #header {
  2. color: black;
  3. .navigation {
  4. font-size: 12px;
  5. }
  6. .logo {
  7. width: 300px;
  8. }
  9. }

父选择器 &

  1. .button {
  2. color: blue;
  3. &-ok {
  4. background-image: url("ok.png");
  5. }
  6. &:hover {
  7. color: green;
  8. }
  9. }

编译 css 为:

  1. .button {
  2. color: blue;
  3. }
  4. .button-ok {
  5. background-image: url("ok.png");
  6. }
  7. .button:hover {
  8. color: green;
  9. }

@规则嵌套和冒泡

  1. .component {
  2. width: 300px;
  3. @media (min-width: 768px) {
  4. width: 600px;
  5. @media (min-resolution: 192dpi) {
  6. background-image: url(/img/icon2x.png);
  7. }
  8. }
  9. @media (min-width: 1280px) {
  10. width: 800px;
  11. }
  12. }

编译 css 为:

  1. .component {
  2. width: 300px;
  3. }
  4. @media (min-width: 768px) {
  5. .component {
  6. width: 600px;
  7. }
  8. }
  9. @media (min-width: 768px) and (min-resolution: 192dpi) {
  10. .component {
  11. background-image: url(/img/icon2x.png);
  12. }
  13. }
  14. @media (min-width: 1280px) {
  15. .component {
  16. width: 800px;
  17. }
  18. }

运算(Operations)

算术运算符 +-*/ 对任何数字、颜色或变量进行运算

  1. @conversion-1: 5cm + 10mm; // 结果是 6cm
  2. @conversion-2: 2 - 3cm - 5mm; // 结果 -1.5cm
  3. @incompatible-units: 2 + 5px - 3cm;
  4. // 结果是 4px
  5. @base: 5%;
  6. @filler: @base * 2; // 结果是 10%
  7. @other: @base + @filler; // 结果是 15%
  8. @base: 2cm * 3mm; // 结果是 6cm
  9. @color: (#224488 / 2); // 结果是 #112244
  10. background-color: #112244 + #111;
  11. // 结果是 #223355
  12. @color: #222 / 2;
  13. // 结果是 `#222 / 2`, not #111
  14. background-color: (#FFFFFF / 16);
  15. // 结果是 #101010

calc() 特例

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

  1. @var: 50vh/2;
  2. width: calc(50% + (@var - 20px));
  3. // 结果是 calc(50% + (25vh - 20px))

转义(Escaping)

  1. @min768: ~"(min-width: 768px)";
  2. .element {
  3. @media @min768 {
  4. font-size: 1.2rem;
  5. }
  6. }

编译 css 为:

  1. @media (min-width: 768px) {
  2. .element {
  3. font-size: 1.2rem;
  4. }
  5. }

从 Less 3.5 开始,可以简写为

  1. @min768: (min-width: 768px);
  2. .element {
  3. @media @min768 {
  4. font-size: 1.2rem;
  5. }
  6. }

在 Less 3.5+ 版本中,许多以前需要“引号转义”的情况就不再需要了

函数(Functions)

  1. @base: #f04615;
  2. @width: 0.5;
  3. .class {
  4. width: percentage(@width); // 返回 `50%`
  5. color: saturate(@base, 5%);
  6. background-color:
  7. spin(lighten(@base, 25%), 8);
  8. }

Less 内置了多种函数用于转换颜色、处理字符串、算术运算等

命名空间和访问符

  1. #bundle() {
  2. .button {
  3. display: block;
  4. border: 1px solid black;
  5. background-color: grey;
  6. &:hover {
  7. background-color: white;
  8. }
  9. }
  10. .tab { ... }
  11. .citation { ... }
  12. }

.button 类混合到 #header a 中,我们可以这样做

  1. #header a {
  2. color: orange;
  3. #bundle.button();
  4. // 还可以书写为 #bundle > .button 形式
  5. }

映射(Maps)

  1. #colors() {
  2. primary: blue;
  3. secondary: green;
  4. }
  5. .button {
  6. color: #colors[primary];
  7. border: 1px solid #colors[secondary];
  8. }

输出符合预期(css):

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

另见:映射(Maps)

作用域(Scope)

  1. @var: red;
  2. #page {
  3. @var: white;
  4. #header {
  5. color: @var; // white
  6. }
  7. }

和上面实例代码相同

  1. @var: red;
  2. #page {
  3. #header {
  4. color: @var; // white
  5. }
  6. @var: white;
  7. }

另见:懒加载

注释(Comments)

  1. /* 一个块注释
  2. * style comment! */
  3. @var: red;
  4. // 这一行被注释掉了!
  5. @var: white;

块注释和行注释都可以使用

导入(Importing)

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

另见:导入(Importing)的知识

Extend

  1. nav ul {
  2. &:extend(.inline);
  3. background: blue;
  4. }
  5. .inline {
  6. color: red;
  7. }

编译 css 为:

  1. nav ul {
  2. background: blue;
  3. }
  4. .inline,
  5. nav ul {
  6. color: red;
  7. }

函数

逻辑函数 if & boolean

  1. @bg: black;
  2. @bg-light: boolean(luma(@bg) > 50%);
  3. div {
  4. background: @bg;
  5. color: if(@bg-light, black, white);
  6. }

编译 css 为:

  1. div {
  2. background: black;
  3. color: white;
  4. }

字符串函数

:- :-
escape URL 编码应用于输入字符串中的特殊字符
e 字符串转义
% 第一个参数是带有占位符的字符串
  1. escape('a=1') // 输出 a%3D1
  2. @mscode: "ms:alwaysHasItsOwnSyntax.For.Stuff()"
  3. filter: e(@mscode);
  4. // 输出 filter: ms:alwaysHasItsOwnSyntax.For.Stuff();
  5. format-a-d: %("repetitions: %a file: %d", 1 + 2, "directory/file.less");
  6. // 输出 format-a-d: "repetitions: 3 file: "directory/file.less"";

替换字符串 replace

  1. replace("Hello, Mars?", "Mars\?", "Earth!");
  2. replace("One + one = 4", "one", "2", "gi");
  3. replace('This is a string.', "(string)\.$", "new $1.");
  4. replace(~"bar-1", '1', '2');

预期输出

  1. "Hello, Earth!";
  2. "2 + 2 = 4";
  3. 'This is a new string.';
  4. bar-2;

length

  1. @list: "banana", "tomato", "potato", "peach";
  2. n: length(@list);

预期输出

  1. n: 4;

返回值列表中的元素数

extract

  1. @list: apple, pear, coconut, orange;
  2. value: extract(@list, 3);

预期输出

  1. value: coconut;

返回列表中指定位置的值

range

  1. value: range(4);
  2. // 输出 value: 1 2 3 4;
  3. value: range(10px, 30px, 10);
  4. // 输出 value: 10px 20px 30px;

生成跨越一系列值的列表

each

  1. @selectors: blue, green, red;
  2. each(@selectors, {
  3. .sel-@{value} {
  4. a: b;
  5. }
  6. });

预期输出

  1. .sel-blue {
  2. a: b;
  3. }
  4. .sel-green {
  5. a: b;
  6. }
  7. .sel-red {
  8. a: b;
  9. }

每个列表成员的每个规则集都绑定到 @value@key@index 变量

  1. @set: {
  2. one: blue;
  3. two: green;
  4. three: red;
  5. }
  6. .set {
  7. each(@set, {
  8. @{key}-@{index}: @value;
  9. });
  10. }

预期输出

  1. .set {
  2. one-1: blue;
  3. two-2: green;
  4. three-3: red;
  5. }

将规则集的评估绑定到列表的每个成员

each()

  1. set-2() {
  2. one: blue;
  3. two: green;
  4. three: red;
  5. }
  6. .set-2 {
  7. // 调用 mixin 并迭代每个规则
  8. each(.set-2(), .(@v, @k, @i) {
  9. @{k}-@{i}: @v;
  10. });
  11. }

预期输出

  1. .set-2 {
  2. one-1: blue;
  3. two-2: green;
  4. three-3: red;
  5. }

使用 rangeeach 创建一个 for 循环

  1. each(range(4), {
  2. .col-@{value} {
  3. height: (@value * 50px);
  4. }
  5. });

预期输出

  1. .col-1 {
  2. height: 50px;
  3. }
  4. .col-2 {
  5. height: 100px;
  6. }
  7. .col-3 {
  8. height: 150px;
  9. }
  10. .col-4 {
  11. height: 200px;
  12. }

数学函数

:- :-
ceil(2.4) (输出 3) 向上舍入到下一个最大整数 #
floor(2.6) (输出 2) 向下舍入到下一个最小整数 #
percentage(0.5) (输出 50%) 将浮点数转换为百分比字符串 #
round(1.67) (输出 2) 应用舍入 #
sqrt(25cm) (输出 5cm) 计算数字的平方根。保持单位不变 #
abs(25cm) (输出 25cm) 计算数字的绝对值。 保持单位不变 #
sin(1deg) (输出 0.01745240643728351) 计算正弦函数 #
asin(-0.8414709848078965) (输出 -1rad) 计算反正弦(正弦的倒数)函数 #
cos(1deg) (输出 0.9998476951563913) 计算余弦函数 #
acos(0.5403023058681398) (输出 1rad) 计算反余弦(余弦的倒数)函数 #
tan(1deg) (输出 0.017455064928217585) 计算正切函数 #
atan(-1.5574077246549023) (输出 -1rad) 计算反正切(正切的倒数)函数 #
pi() (输出 3.141592653589793) π (pi) #
pow(0cm, 0px) (输出 1cm) 返回第一个参数的第二个参数次幂的值 #
mod(11cm, 6px) (输出 5cm) 返回第一个参数模数第二个参数的值 #
min(5, 10) (输出 5) 返回一个或多个值中的最小值 #
max(5, 10) (输出 10) 返回一个或多个值中的最大值 #

颜色定义函数

:- :-
rgb #
rgba #
argb #
hsl #
hsla #
hsv #
hsva #

类型函数

:- :-
isnumber 值是否为数字 #
isstring 值是否为字符串 #
iscolor 值是否为颜色值 #
iskeyword 值是否为 keyword #
isurl 值是否为 url 值 #
ispixel 值是否为像素值 #
isem 值是否为 em 值 #
ispercentage 值是否为 百分百 值 #
isunit 值是是否为指定单位的数字 #
isruleset 值是否为规则集 #
isdefined 值是否为 defined #

杂项函数

:- :-
color #
image-size #
image-width #
image-height #
convert #
data-uri #
default #
unit #
get-unit #
svg-gradient #

颜色通道函数

:- :-
hue #
saturation #
lightness #
hsvhue #
hsvsaturation #
hsvvalue #
red #
green #
blue #
alpha #
luma #
luminance #

色彩运算函数

:- :-
saturate #
desaturate #
lighten #
darken #
fadein #
fadeout #
fade #
spin #
mix #
tint #
shade #
greyscale #
contrast #

颜色混合功能

:- :-
multiply #
screen #
overlay #
softlight #
hardlight #
difference #
exclusion #
average #
negation #

另见