Less开发常用知识点归纳

桃扇骨 2023-06-06 10:37 50阅读 0赞

Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量、Mixin、函数等特性,使 CSS 更易维护和扩展,可以运行在 Node 或浏览器端。

1.注释

  1. /*注释编译后依然显示*/
  2. //编译后不显示

2.变量
示例:

  1. //定义变量
  2. @width:300px;
  3. //用法
  4. div{
  5. width: @width;
  6. }
  7. p{
  8. width: @width;
  9. }

3.混合

  1. .base{
  2. width: @width;
  3. height: 100px;
  4. }
  5. div{
  6. background: pink;
  7. .base
  8. }
  9. p{
  10. background: green;
  11. .base;
  12. }

4.混合带参,参数带有默认值

  1. .border(@px:1px,@s:solid,@c:red){
  2. border:@px @s @c;
  3. }
  4. p{
  5. background: green;
  6. .base;
  7. .border(3px);
  8. }

5.嵌套

  1. ul{
  2. width: @width+50px;
  3. list-style: none;
  4. margin: 30px auto;
  5. li{
  6. background: pink;
  7. margin-top: 10px;
  8. line-height: 40px;
  9. height: 40px;
  10. text-align: center;
  11. //使用&引用父选择器
  12. &:hover a{
  13. color: white;
  14. }
  15. }
  16. a{
  17. text-decoration: none;
  18. color: black;
  19. }
  20. }

6.运算(加减乘除均可)

  1. @width:100px;
  2. .one{
  3. width: @width+100px; //200px
  4. }

7.@arguments(arguments表示传进来的所有参数)

  1. .border(@px:1px,@s:solid,@c:red){
  2. border: @arguments;
  3. }
  4. p{
  5. background: green;
  6. .base;
  7. .border(3px);
  8. }

8.@import命令,导入其他模块(.css .html .js .less …),若导入的是less文件,less后缀可以省略

  1. @import 'css2.less';
  2. @import 'css3';

9.匹配

  1. .base(a){
  2. width: 100px;
  3. height: 200px;
  4. background-color: red;
  5. }
  6. .base(b){
  7. width: 100px;
  8. height: 200px;
  9. background-color:green;
  10. }
  11. div{
  12. .base(b);
  13. }

10.作用域

less中变量存在作用域,变量的作用域与编程语言一样。

发表评论

表情:
评论列表 (有 0 条评论,50人围观)

还没有评论,来说两句吧...

相关阅读