Less学习笔记

视频教程地址:http://www.imooc.com/learn/102css

 

1、什么是Lesshtml

    css的Less比如是js的Jquery,可让人们更方遍快捷的使用css,使css代码更简洁,能够减小重复的代码,减小开发人员的工做量。浏览器

  Less CSS是一种动态样式语言,属于CSS预处理语言的一种,它使用相似CSS的语法,为CSS赋予了动态语言的特性,如变量、继承、运算、函数等,更方便CSS的编写和维护。app

   Less中文手册:less.bootcss.comless

 

2、编译工具koa

  1.Koala:国人开发的LESS/SASS编译工具函数

   下载地址:http://koala-app.com/index-zh.html工具

  2.Node.js库spa

  3.浏览器端使用调试

 

3、koala配置及使用

  1.新建后缀为.less文件:index.less

   头部写上:@charset "utf-8";   //设定字符集

  2.把文件夹拖到koala中,设置输出路径为style下的index.css

     使用koala进行编译,而后就生成了一个index.css文件。

  3.以后咱们只要编辑index.less文件便可。

 

4、注释

  1./*编译后会被保留*/

  2.//编译后不会被保留

 

5、变量

  1.设定变量:

  @text_width: 300px;

  2.使用变量:示例以下

  <div class="box"></div>

  .box{
    width: @text_width;
    height: @text_width;
    background: #000;
  }

 

6、混合

  1.混合简介:与原有的在class中新追加class的方法有所不一样(原有<div class="box border"></div>),使用Less的混合功能后,只要声明.border后,在.box中加  入.border;便可。

  使用示例一:

  <div class="box"></div>

  .box{
    width: @text_width;
    height: @text_width;
    background: #000;

    .border;
  }

  .border{

    border: 5px solid yellow;

  }

  使用示例二:新增一个box2,可是它和box1只有一点点细微的区别,能够直接拿来box1使用,而后加上它本身的样式。

  .box2{

    .box1;

    margin-left: 100px;

  }

  2.混合是能够带参数的

  在index.less中使用带参数的值,在border_02后面加一个(),里面声明一个变量;

  而后传入参数30px。

  

  在编译后的index.css文件中,带参数的变量不显示

  

  3.混合也能够带默认值

  在border_03后面加一个(),里面声明一个变量,变量后面附一个初始值“:10px”;

  若是不想用默认值,只要在text_hunhe下的border_03的()中写入值便可。

  默认值必须带(),否则会报错。

  

  4.混合高级使用示例,兼容性也可使用

  <div class="radius_test"></div>

  

  在编译后的index.css文件中,展现以下

  

 

7、匹配模式

  1.简介:至关于js中的if,但不彻底是

  2.示例

  先介绍一个画倒三角的方法

  <div class="sanjiao"></div>

  .sanjiao{
    width: 0;
    height: 0;
    overflow: hidden;
    border-width: 10px;
    border-color: red transparent transparent transparent;
    border-style: solid dashed dashed dashed;
  }

  匹配模式示例一:

  .triangle(top,@w:5px,@c:#ccc){

    border-width: @w;

    border-color: @c transparent transparent transparent;

    border-style: solid dashed dashed dashed;

  }

  .triangle(right,@w:5px,@c:#ccc){

    border-width: @w;

    border-color:  transparent @c transparent transparent;

    border-style:  dashed solid dashed dashed;

  }

  .triangle(bottom,@w:5px,@c:#ccc){

    border-width: @w;

    border-color:  transparent transparent @c transparent;

    border-style:  dashed dashed solid dashed;

  }

  .triangle(left,@w:5px,@c:#ccc){

    border-width: @w;

    border-color:  transparent transparent transparent @c;

    border-style:  dashed dashed dashed solid;

  }

  .triangle(@_,@w:5px,@c:#ccc){    // @_ 是固定格式,表示无论你匹配到谁,都会带上这些内容

    width: 0;
    height: 0;
    overflow: hidden;

  }

  .sanjiao{

    .triangle(right,100px);

  }

  匹配模式示例二:

  匹配模式定位

  .pos(r){position:relative;}
  .pos(a){position:absolute;}
  .pos(f){position:fixed;}
  .posi{
    width: 100px;
    height: 100px;
    background: blue;
    .pos(r);
  }

 

8、运算

  1.less中的运算能够是任何数字、颜色、变量,运算要包裹在括号中。如:+ - * /

  2.示例以下:

  @test_01:300px;

  .box_02{

    width: (@test_01 - 20) * 5;   //未强制规定必需要带px单位,只要有一个带便可

    color: #ccc - 10;                      //不太经常使用到

  }

 

9、嵌套规则

  1.示例

   

  原css 

  .list{}
  .list li{}
  .list a{}
  .list span{}

  使用Less嵌套:

  .list{
    width: 600px;
    margin: 30px auto;
    padding: 0;
    list-style: none;
    font-size: 16px;    li{

      height: 30px;
      line-height: 30px;
      background: blue;
      margin-bottom: 5px;
      padding: 0 10px;
    }
    a{
      float: left;
      color: #000;

      &:hover{    //&表明上一层选择器

        color: red;

      }
    }
    span{
      float: right;
    }
  }

 

10、arguments变量

  .border_arg(@w:30px,@c:#ccc,@xx:solid){border:@arguments}     //省了一点点事儿,懒人必备

  .test_arguments{.border_arg(40px);}

 

11、避免编译、!important

  1.避免编译:在咱们须要输入一些不正确的css语法或者使用一些less不认识的专有语法时使用。在字符串前加上一个~便可

  .test_03{
    width: ~'calc(300px - 30px)';    //~'...'避免编译,把单引号里的内容按照原样传给浏览器
  }

  2.!important关键字:适合用来调试,通常不会用到

  .test_important{

    .box !important;    //给全部样式加上!important

  }

相关文章
相关标签/搜索