一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量、Mixin、函数等特性,使 CSS 更易维护和扩展。
1 | npm install -g less |
用法
代码用法
1 | var less = require('less'); |
配置
1 | var less = require('less'); |
浏览器端用法
1 | <!-- 确保 .less 在 less.js 前 --> |
或者
1 | <script src="less.js" data-poll="1000" data-relative-urls="false"></script> |
cdn
1 | <script src="//cdnjs.cloudflare.com/ajax/libs/less.js/2.5.3/less.min.js"></script> |
规则
1 | @base: #f938ab; |
=>
1 | .box { |
变量
1 | @nice-blue: #5B83AD; |
=>
1 | #header { |
属性
1 | @property: color; |
=>
1 | .widget { |
懒加载
1 | .lazy-eval { |
=>
1 | .lazy-eval-scope { |
默认变量
1 | // library |
层叠
1 | #header { |
=>
1 | #header { |
操作
1 | // numbers are converted into the same units |
转义
1 | .weird-element { |
=>
1 | .weird-element { |
函数
1 | @base: #f04615; |
命名空间和访问器
1 | #bundle { |
作用域
1 | @var: red; |
注释
1 | /* One hell of a block |
Importing
1 | @import "library"; // library.less |
扩展
1 | nav ul { |
=>
1 | nav ul { |
1 | .a:extend(.b) {} |
扩展层叠
1 | .bucket { |
=>
1 | .bucket tr, |
1 | .bucket { |
=>
1 | tr .bucket, |
1 | *.class { |
=>
1 | *.class { |
nth表单式
1 | :nth-child(1n+3) { |
=>
1 | :nth-child(1n+3) { |
扩展全部
1 | .a.b.test, |
=>
1 | .a.b.test, |
@media
1 | @media print { |
=>
1 | @media print { |
1 | @media screen { |
=>
1 | @media screen and (min-width: 1023px) { |
1 | @media screen { |
=>
1 | @media screen { |
重复检测
1 | .alert-info, |
=>
1 | .alert-info, |
混合 Mixins
1 | .a, #b { |
=>
1 | .a, #b { |
混合函数
1 | .mixin() { |
=>
1 | .caller { |
将规则集传递到混合
1 | // declare detached ruleset |
=>
1 | .top { |
局部
1 | @detached-ruleset: { |
=>
1 | selector { |
导入指令
1 | .foo { |
导入选项
reference: use a Less file but do not output itinline: include the source file in the output but do not process itless: treat the file as a Less file, no matter what the file extensioncss: treat the file as a CSS file, no matter what the file extensiononce: only include the file once (this is default behavior)multiple: include the file multiple timesoptional: continue compiling when file is not found
混入守卫
1 | .mixin (@a) when (lightness(@a) >= 50%) { |
=>
1 | .class1 { |
CSS守卫
1 | .my-optional-style() when (@my-option = true) { |
循环
1 | .loop(@counter) when (@counter > 0) { |
=>
1 | div { |
合并
1 | .mixin() { |
=>
1 | .myclass { |
空间
1 | .mixin() { |
=>
1 | .myclass { |
父选择器
1 | a { |
=>
1 | a { |
&
1 | .link { |
=>
1 | .link + .link { |
函数手册
杂项函数
colorimage-sizeimage-widthimage-heightconvertdata-uridefaultunitget-unitsvg-gradient
字符串函数
escapee%formatreplace
列表函数
lengthextract
数学函数
ceilfloorpercentageroundsqrtabssinasincosacostanatanpipowmodminmax
类型函数
isnumberisstringiscoloriskeywordisurlispixelisemispercentageisunitisruleset
颜色定义函数
rgbrgbaargbhslhslahsvhsva
颜色通道函数
huesaturationlightnesshsvhuehsvsaturationhsvvalueredgreenbluealphalumaluminance
颜色操作函数
saturatedesaturatelightendarkenfadeinfadeoutfadespinmixtintshadegreyscalecontrast
颜色混合函数
multiplyscreenoverlaysoftlighthardlightdifferenceexclusionaveragenegation