变量(Variables)
@width: 10 px;
@height: @width+ 10 px;
#header {
width: @width,
height: @height
}
混入(Mixins)
. bordered {
border- top: dotted 1 px black;
border- bottom: solid 2 px black;
}
#id{
width: 100 px
}
#menu a {
color: #111 ;
. bordered ( ) ;
#id ( ) ;
}
嵌套(Nesting)
#header {
color: black;
. navigation {
font- size: 12 px;
}
. logo {
width: 300 px
}
& : after {
content: " " ;
dispaly: block;
font- size: 0 ;
height: 0 ;
clear: both;
visibility: hidden;
}
}
@规则嵌套和冒泡
. component {
width: 300 px;
@media ( min- width: 768 px) {
width: 600 px;
@media ( min- resolution: 192 dpi) {
background- image: url ( / img/ retina2x. png) ;
}
}
@media ( min- width: 1280 px) {
width: 800 px;
}
}
运算
@coversion- 1 : 5 cm+ 10 mm;
@conversion- 2 : 2 - 3 cm- 5 mm;
@incompatible- units: 2 + 5 px- 3 cm;
@base: 5 % ;
@filler: @base* 2 ;
@other: @base+ @filler;
@base: 2 cm* 3 mm;
@color: #224488 / 2 ;
calc()特例
@var : 50 vh/ 2 ;
width: calc ( 50 % + ( @val- 20 px) ) ;
转义 (Escaping)
@min768: ~ '(min-width:768px)' ;
. element {
@media @min768 {
font- size: 1.2 rem
}
}
@min768: ( min- width: 768 px) ;
. element {
@media @min768 {
font- size: 1.2 rem;
}
}
函数
逻辑函数
if
@some: foo;
div {
margin: if ( ( 2 > 1 ) , 0 , 3 px) ;
color: if ( ( iscolor ( @some) ) , @some, black) ;
}
div {
color: if ( 2 > 1 , blue, green) ;
}
boolean
@bg: black;
@bg- light: boolean ( luma ( @bg) > 50 % ) ;
div {
background: @bg;
color: if ( @bg- light, black, white) ;
}
String Function
escape(转义字符串)
escape('a=1');
//输出
a%3D1
e
@mscode: 'ms:alwaysHasItsOwnSyntax.For.Stuff()' ;
filter: e ( @mscode) ;
输出:
fileter: ms: alwaysHasItsOwnSyntax. For. Stuff ( ) ;
%
format- a- d: % ( "repetitions: %a file: %d" , 1 + 2 , "directory/file.less" ) ;
format- a- d- upper: % ( 'repetitions: %A file: %D' , 1 + 2 , "directory/file.less" ) ;
format- s: % ( "repetitions: %s file: %s" , 1 + 2 , "directory/file.less" ) ;
format- s- upper: % ( 'repetitions: %S file: %S' , 1 + 2 , "directory/file.less" ) ;
输出:
format- a- d: "repetitions: 3 file: " directory/ file. less"" ;
format- a- d- upper: "repetitions: 3 file: %22directory%2Ffile.less%22" ;
format- s: "repetitions: 3 file: directory/file.less" ;
format- s- upper: "repetitions: 3 file: directory%2Ffile.less" ;
replace
replace ( "Hello, Mars?" , "Mars\?" , "Earth!" ) ;
replace ( "One + one = 4" , "one" , "2" , "gi" ) ;
replace ( 'This is a string.' , "(string)\.$" , "new $1." ) ;
replace ( ~ "bar-1" , '1' , '2' ) ;
输出
"Hello,Earth!" ;
"2+2=4" ;
"This is a new string." ;
bar- 2 ;
List Functions
length
@list: 'banana' , 'tomato' , 'patato' , 'peach' ;
n: length ( @list) ;
输出:
n: 4 ;
extract
@list: apple, pear, coconut, orange;
value: extract ( @list, 3 ) ;
输出:
value: coconut;
range
value: range ( 10 px, 30 px, 10 ) ;
输出:
value: 10 px 20 px 30 px;
each
@selectors: blue, green, red;
each ( @selectors, {
. sel- @{ value} {
a: b;
}
} )
@set : {
one: blue;
two: green;
three: red;
}
. set {
each ( @set , {
@{ key} - @{ index} : @value;
} ) ;
}
. set - 2 ( ) {
one: blue;
two: green;
three: red;
}
. set - 2 {
each ( . set - 2 ( ) , . ( @v, @k, @i) {
@{ k} - @{ i} : @v;
} ) ;
}
Math Functions
ceil
ceil ( 2.4 ) => 3
floor
floor ( 2.6 ) => 2
percentage
percentage ( 0.5 ) => 50 %
round
rand ( 1.67 ) => 2
rand ( 1.67 , 1 ) => 1.7
sqrt
sqrt ( 25 cm) => 5 cm
sqrt ( 18.6 % ) => 4.312771730569565 %
abs
abs ( - 18.6 % ) => 18.6 %
sin
sin ( 1 ) => 0.8414709848078965
asin
asin ( 0 ) => 0 rad
cos
cos ( 1 ) => 0.5403023058681398
acos
acos ( 0.5403023058681398 ) => 1 red
tan
tan ( 1 ) => 1.5574077246549023
atan
atan ( 0 ) => 0 rad
pi
pi ( ) => 3.141592653589793
pow
pow ( 25 , 0.5 ) => 5
mod
mod ( 11 cm, 6 px) => 5 cm
min
min ( 3 px, 42 px, 1 px, 16 px) => 1 px
max
max ( 3 % , 42 % , 1 % , 16 % ) => 42 %
类型函数
isnumber
isnumber ( #ff0) => false
isnumber ( 7.8 % ) => true
isstring
isstring ( 'string' ) => true
iscolor
iscolor ( #ff0) => true
iscolor ( blue) => true
iskeyword
iskeyword ( keyword) => true
isurl
isurl ( url ( ... ) ) => true
ispixel
ispixel ( 56 px) => true
isem
isem ( 7.8 em) => true
ispercentage
ispercentage ( 7.8 % ) => true
isunit
isunit ( 11 px, px) => true
isruleset
@rules: { color: red}
isruleset ( @rules) => true
Misc Functions
color
color ( '#aaa' ) => #aaa
image-size
image- size ( 'file.png' ) => 10 px 10 px
image-width
image- width ( 'file.png' ) => 10 px
image-height
image- height ( 'file.png' ) => 10 px
convert
convert ( 9 s, 'ms' ) => 9400 ms
data-uri
data- uri ( '../data/image.jpg' ) => url ( '../data/image.jpg' )
default
. mixin ( 1 ) { x: 11 }
. mixin ( @x) when ( default ( ) ) { z: @x}
div { . mixin ( 3 ) } => div { z: 3 }
unit
unit ( 5 , px) => 5 px
unit ( 5 em) => 5
get-unit
get - unit ( 5 px) => px
get - unit ( 5 ) => nothing
svg-gradient
div {
background- image: svg- gradient ( to right, red, green 30 % , bule)
}
命名空间和访问符
#bundle ( ) {
. button {
display: block;
border: 1 px solid black;
background- color: grey;
& : hover {
background- color: white;
}
}
. tab { ... }
. citation { ... }
}
#head a {
color: orange;
#bundle. button ( ) ;
}
映射
#colors ( ) {
primary: blue,
secondary: green
}
. button {
color: #colors[ primary] ,
border: 1 px solid #colors[ secondary]
}
作用域
@var : red;
#page {
#header {
color: #val;
}
@val: white;
}
注释
导入
@import 'library' ;
@import 'typo.css' ;