less简介

less是一门css的预处理语言

  • less是一个css的增强版,通过less可以编写更少的代码实现更强大的样式
  • less中添加了许多的新特性:像对变量的支持、对mixin的支持…
  • less的语法大体上和css语法一致,但是less中增添了许多对css的扩展,所以浏览器无法直接执行less代码,要执行必须向将less转换为css,然后再由浏览器执行

1、安装插件

vscode中搜索less,点击安装

16.less简介 - 图1

2、编写less

html代码

使用快捷方式创建html代码

16.less简介 - 图2

回车生成html代码

  1. <div class="box1"></div>
  2. <div class="box2"></div>
  3. <div class="box3"></div>

less代码

创建style.less文件,编写less代码

  1. body {
  2. --height: calc(200px / 2);
  3. --width: 100px;
  4. div {
  5. height: var(--height);
  6. width: var(--width);
  7. }
  8. .box1 {
  9. background-color: #bfa;
  10. }
  11. .box2 {
  12. background-color: red;
  13. }
  14. .box3 {
  15. background-color: yellow;
  16. }
  17. }

Easy LESS插件会帮助我们在style.less所在目录下面生成一个相同名称的css文件

16.less简介 - 图3

查看生成的style.css代码

  1. body {
  2. --height: calc(200px / 2);
  3. --width: 100px;
  4. }
  5. body div {
  6. height: var(--height);
  7. width: var(--width);
  8. }
  9. body .box1 {
  10. background-color: #bfa;
  11. }
  12. body .box2 {
  13. background-color: red;
  14. }
  15. body .box3 {
  16. background-color: yellow;
  17. }

我们直接在HTML中引入生成的style.css

  1. <link rel="stylesheet" href="/css/style.css">

运行代码,查看效果

16.less简介 - 图4

3、less语法

less注释

less中的单行注释,注释中的内容不会被解析到css

css中的注释,内容会被解析到css文件中

  1. // `less`中的单行注释,注释中的内容不会被解析到`css`中
  2. /*
  3. `css`中的注释,内容会被解析到`css`文件中
  4. */

父子关系嵌套

less中,父子关系可以直接嵌套

  1. // `less`中的单行注释,注释中的内容不会被解析到`css`中
  2. /*
  3. `css`中的注释,内容会被解析到`css`文件中
  4. */
  5. body {
  6. --height: calc(200px / 2);
  7. --width: 100px;
  8. div {
  9. height: var(--height);
  10. width: var(--width);
  11. }
  12. .box1 {
  13. background-color: #bfa;
  14. .box2 {
  15. background-color: red;
  16. .box3 {
  17. background-color: yellow;
  18. }
  19. >.box4{
  20. background-color: green;
  21. }
  22. }
  23. }
  24. }

对应的css

  1. /*
  2. `css`中的注释,内容会被解析到`css`文件中
  3. */
  4. body {
  5. --height: calc(200px / 2);
  6. --width: 100px;
  7. }
  8. body div {
  9. height: var(--height);
  10. width: var(--width);
  11. }
  12. body .box1 {
  13. background-color: #bfa;
  14. }
  15. body .box1 .box2 {
  16. background-color: red;
  17. }
  18. body .box1 .box2 .box3 {
  19. background-color: yellow;
  20. }
  21. body .box1 .box2 > .box4 {
  22. background-color: green;
  23. }

变量

变量,在变量中可以存储一个任意的值

并且我们可以在需要时,任意的修改变量中的值

变量的语法:@变量名

  • 直接使用使用变量时,则以@变量名的形式使用即可
  • 作为类名、属性名或者一部分值使用时,必须以@{变量名}的形式使用
  • 可以在变量声明前就使用变量(可以但不建议)
  1. @b1:box1;
  2. @b2:box2;
  3. @b3:box3;
  4. @size:200px;
  5. @bc:background-color;
  6. @bi:background-image;
  7. @color:red;
  8. @path:image/a/b/c;
  9. .@{b1}{
  10. width: @size;
  11. height: $width;
  12. @{bc}: @color;
  13. @{bi}: url("@{path}/1.png");
  14. }
  15. .@{b2}{
  16. width: @size;
  17. height: $width;
  18. @{bc}: @color;
  19. @{bi}: url("@{path}/2.png");
  20. }
  21. .@{b3}{
  22. width: @size;
  23. height: $width;
  24. @{bc}: @color;
  25. @{bi}: url("@{path}/3.png");
  26. }

生成的css代码

  1. .box1 {
  2. width: 200px;
  3. height: 200px;
  4. background-color: red;
  5. background-image: url("image / a / b / c/1.png");
  6. }
  7. .box2 {
  8. width: 200px;
  9. height: 200px;
  10. background-color: red;
  11. background-image: url("image / a / b / c/2.png");
  12. }
  13. .box3 {
  14. width: 200px;
  15. height: 200px;
  16. background-color: red;
  17. background-image: url("image / a / b / c/3.png");
  18. }

注意:在中使用语法需要用引号包裹url``less

其他

  1. .p1{
  2. width: @size;
  3. height: $width;
  4. &-wrapper{
  5. background-color: peru;
  6. }
  7. // &:hover{
  8. // background-color: blue;
  9. // }
  10. :hover{
  11. background-color: blue;
  12. }
  13. }
  14. .p2:extend(.p1){
  15. color:@color;
  16. }
  17. .p3{
  18. .p1();
  19. }
  20. .p4(){
  21. width: @size;
  22. height: $width;
  23. }
  24. .p5{
  25. // .p4();
  26. .p4;
  27. }

生成的css代码

  1. .p1,
  2. .p2 {
  3. width: 200px;
  4. height: 200px;
  5. }
  6. .p1-wrapper {
  7. background-color: peru;
  8. }
  9. .p1 :hover {
  10. background-color: blue;
  11. }
  12. .p2 {
  13. color: red;
  14. }
  15. .p3 {
  16. width: 200px;
  17. height: 200px;
  18. }
  19. .p5 {
  20. width: 200px;
  21. height: 200px;
  22. }
  • & 拼接
  • 伪元素
  • :extend() 对当前选择器扩展指定选择器的样式(选择器分组)
  • .p1() 直接对指定的样式进行引用,这里就相当于将p1的样式在这里进行了复制(mixin 混合)
  • 使用类选择器时可以在选择器后边添加一个括号,这时我们实际上就创建了一个mixins混合函数

4、混合函数

在混合函数中可以直接设置变量,并且可以指定默认值

  1. .test(@w:200px, @h:100px, @bc:red){
  2. width: @w;
  3. height: @h;
  4. background-color: @bc;
  5. }
  6. .p6{
  7. // .test(200px, 100px, red); // 对应参数位传值
  8. // .test(@h:200px,@w:100px,@bc:red); // 写明对应属性,可变换顺序
  9. // .test();
  10. .test(300px);
  11. }

生成的css代码

  1. .p6 {
  2. width: 300px;
  3. height: 100px;
  4. background-color: red;
  5. }

其他

  • average混合函数
    生成的css代码

    1. .h1{
    2. color:average(red,yellow);
    3. }
    1. .h1 {
    2. color: #ff8000;
    3. }
  • darken混合函数
    生成的css代码

    1. body{
    2. background-color: darken(#bfa, 50%);
    3. }
    1. body{
    2. background-color: #22aa00;
    3. }

5、补充

创建all.less文件,将我们之前编写的less文件通过@import引入进来

可以通过import来将其他的less引入到当前的less

  1. @import "style.less";
  2. @import "syntax.less";

查看生成的all.css代码,会发现其他的内容囊括了两个less文件的内容

所以,我们可以利用@import来对less文件进行整合,然后引入生成的css文件使用即可

这样,每次修改的时候直接对某个模块的less文件进行修改,就会非常简单

如果我们观察过之前fontawesome源码文件,会发现其中也有less代码文件

16.less简介 - 图5

不同的less文件里都有其自己的职责,如

  • _animated.less中专门存放动画的混合函数
  • _variables.less中专门存放定义的变量

但是也有个问题,通过F12调试时显示的也是css中对应的行号

16.less简介 - 图6

如果我们要改,需要找一下,太麻烦了,能不能直接显示less中行号呢?这样我们直接定位到对应less中直接进行修改,维护起来也会比较方便

我们需要在Easy LESS插件中修改settings.json文件,在其中添加如下配置

  1. "less.compile": {
  2. "compress": true, // true => remove surplus whitespace
  3. "sourceMap": true, // true => generate source maps (.css.map files)
  4. "out": true // false => DON'T output .css files (overridable per-file, see below)
  5. }

修改完毕后,会发现多生成出来一个all.css.map文件,说明配置生效

16.less简介 - 图7

再刷新下页面,通过F12会发现变成了less文件对应的行号

16.less简介 - 图8

我们来逐一解释下配置的less.compile项中每一个属性的含义

  • compress 生成的css文件代码会被压缩(作用相当于我们之前安装的JS & CSS Minifier (Minify)插件的效果)
  • sourceMap 生成.css.map文件,通过F12可以查看了less文件对应行号
  • out 生成对应css文件(当然是需要了)