less的使用心得(干货)

安装

  1. node安装
$ npm install -g less
  1. 可以在编辑器中安装


    less配置.jpg

    less监听转译、.jpg
  2. 可以引入编译 js 转css时会稍卡顿

<link rel="stylesheet/less" type="text/css" href="styles.less" />
<script src="less.js" type="text/javascript"></script>

优点

  1. 结构清晰,便于扩展(嵌套功能)
  2. 可以方便地屏蔽浏览器私有语法差异。(函数功能)
  3. 可以轻松实现多重继承(混合功能)
  4. 完全兼容 CSS 代码
  5. 减少重复的机械劳动,便于维护(变量+函数+导入)

使用

1.变量
  • 一般样式做变量
@nice-blue: #5B83AD;
@light-blue: @nice-blue + #111;//支持变量的运算

#header {
  color: @light-blue;
  background:@nice-blue;
}
#footer{
  background:@nice-blue;
}

编译为:

#header {
  color: #6c94be;
  background: #5B83AD;
}
#footer{
  background: #5B83AD;
}
  • 文件路径作为变量
@images: "../img";

// Usage
body {
  color: #444;
  background: url("@{images}/white-sand.png");
}

编译为

body {
  color: #444;
  background: url("../img/white-sand.png");
}
  • 变量的作用域
@var: red;

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

编译为(变量@var在#page中被更改)

#page #header {
    color: white;//不再是red
  }
2.混合(Mixins)
  • 普通混合
//border的公共样式
.bordered {
  border-top: dotted 1px black;
  border-bottom: solid 2px black;
}
#menu a {
  color: #111;
  .bordered;
}
.post a {
  color: red;
  .bordered;
}

编译为

#menu a {
  color: #111;
  border-top: dotted 1px black;
  border-bottom: solid 2px black;
}
.post a {
  color: red;
  border-top: dotted 1px black;
  border-bottom: solid 2px black;
}
  • 引用被嵌套的
#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
}

编译为

#header a {
  color: orange;
  display: block;
    border: 1px solid black;
    background-color: grey;
}
#header a:hover{
   background-color: white
}
3.嵌套
  • 一般写法和伪类写法
#header {
  color: black;
  .navigation {
    font-size: 12px;
  }
  .logo {
    width: 300px;
    &:after {//伪类的写法
      content: " ";
      display: block;
      font-size: 0;
      height: 0;
      clear: both;
      visibility: hidden;
    }
  }
}

编译为

#header {
  color: black;
}
#header .navigation {
  font-size: 12px;
}
#header .logo {
  width: 300px;
}
#header .logo:after{
  content: " ";
  display: block;
  font-size: 0;
  height: 0;
  clear: both;
  visibility: hidden;
}
  • 媒体查询的写法
.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.函数
  • 普通带参数函数
.border-radius (@radius) {
  border-radius: @radius;
  -moz-border-radius: @radius;
  -webkit-border-radius: @radius;
}
#header {
  .border-radius(4px);
}
  • 给参数设置默认值
.border-radius (@radius: 5px) {
  border-radius: @radius;
  -moz-border-radius: @radius;
  -webkit-border-radius: @radius;
}
#header {
  .border-radius;  //没有传参就是使用默认值
}
  • @arguments 变量
    @arguments包含了所有传递进来的参数. 如果你不想单独处理每一个参数的话就可以像这样写:
.box-shadow (@x: 0, @y: 0, @blur: 1px, @color: #000) {
  box-shadow: @arguments;
  -moz-box-shadow: @arguments;
  -webkit-box-shadow: @arguments;
}
.box-shadow(2px, 5px);

编译为

.box-shadow{
  box-shadow: 2px 5px 1px #000;
  -moz-box-shadow: 2px 5px 1px #000;
  -webkit-box-shadow: 2px 5px 1px #000;
}
  • when引导
.mixin (@a) when (lightness(@a) >= 50%) {//执行.mixin (@a)函数 如果变量>=50%,就加上这个属性background-color: black;
  background-color: black;
}
.mixin (@a) when (lightness(@a) < 50%) {//执行.mixin (@a)函数 如果变量<50%,就加上这个属性background-color: white;
  background-color: white;
}
.mixin (@a) {
  color: @a;
}
//class1,class2给添加样式
.class1 { .mixin(#ddd) }
.class2 { .mixin(#555) }

编译为

.class1 {
  background-color: black;
  color: #ddd;
}
.class2 {
  background-color: white;
  color: #555;
}
5.运算
//能够进行单位换算
@conversion-1: 5cm + 10mm; // result is 6cm
@conversion-2: 2 - 3cm - 5mm; // result is -1.5cm

// 能够忽略单位进行计算
@incompatible-units: 2 + 5px - 3cm; // result is 4px

// 相同单位能够进行乘除运算,不同单位不能乘除运算
@base: 5%;
@filler: @base * 2; // result is 10%
@other: @base + @filler; // result is 15%
6.导入
  • “导入”的工作方式和你预期的一样。你可以导入一个 .less 文件,此文件中的所有变量就可以全部使用了。如果导入的文件是 .less 扩展名,则可以将扩展名省略掉:
@import "library"; // library.less
@import "typo.css";

参考:http://www.bootcss.com/p/lesscss/#docs

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容