LESS语法备忘

变量

很容易理解:php

@nice-blue: #5B83AD; @light-blue: @nice-blue + #111; #header { color: @light-blue; } 

输出:css

#header { color: #6c94be; } 

甚至能够用变量名定义为变量:web

@fnord: "I am fnord."; @var: 'fnord'; content: @@var; 

解析后:编程

content: "I am fnord."; 

请注意 LESS 中的变量为彻底的 ‘常量’ ,因此只能定义一次.ruby

混合

在 LESS 中咱们能够定义一些通用的属性集为一个class,而后在另外一个class中去调用这些属性. 下面有这样一个class:less

.bordered { border-top: dotted 1px black; border-bottom: solid 2px black; } 

那若是咱们如今须要在其余class中引入那些通用的属性集,那么咱们只须要在任何class中像下面这样调用就能够了:编程语言

#menu a { color: #111; .bordered; } .post a { color: red; .bordered; } 

.bordered class里面的属性样式都会在 #menu a.post a中体现出来:函数式编程

#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; } 

任何 CSS class, id 或者 元素 属性集均可以以一样的方式引入.函数

带参数混合

在 LESS 中,你还能够像函数同样定义一个带参数的属性集合:post

.border-radius (@radius) { border-radius: @radius; -moz-border-radius: @radius; -webkit-border-radius: @radius; } 

而后在其余class中像这样调用它:

#header { .border-radius(4px); } .button { .border-radius(6px); } 

咱们还能够像这样给参数设置默认值:

.border-radius (@radius: 5px) { border-radius: @radius; -moz-border-radius: @radius; -webkit-border-radius: @radius; } 

因此如今若是咱们像这样调用它的话:

#header { .border-radius; } 

radius的值就会是5px.

你也能够定义不带参数属性集合,若是你想隐藏这个属性集合,不让它暴露到CSS中去,可是你还想在其余的属性集合中引用,你会发现这个方法很是的好用:

.wrap () { text-wrap: wrap; white-space: pre-wrap; white-space: -moz-pre-wrap; word-wrap: break-word; } pre { .wrap } 

输出:

pre { text-wrap: wrap; white-space: pre-wrap; white-space: -moz-pre-wrap; word-wrap: break-word; } 

@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: 2px 5px 1px #000; -moz-box-shadow: 2px 5px 1px #000; -webkit-box-shadow: 2px 5px 1px #000; 

模式匹配和导引表达式

有些状况下,咱们想根据传入的参数来改变混合的默认呈现,好比下面这个例子:

.mixin (@s, @color) { ... } .class { .mixin(@switch, #888); } 

若是想让.mixin根据不一样的@switch值而表现各异,以下这般设置:

.mixin (dark, @color) { color: darken(@color, 10%); } .mixin (light, @color) { color: lighten(@color, 10%); } .mixin (@_, @color) { display: block; } 

如今,若是运行:

@switch: light; .class { .mixin(@switch, #888); } 

就会获得下列CSS:

.class { color: #a2a2a2; display: block; } 

如上,.mixin就会获得传入颜色的浅色。若是@switch设为dark,就会获得深色。

具体实现以下:

  • 第一个混合定义并未被匹配,由于它只接受dark作为首参
  • 第二个混合定义被成功匹配,由于它只接受light
  • 第三个混合定义被成功匹配,由于它接受任意值

只有被匹配的混合才会被使用。变量能够匹配任意的传入值,而变量之外的固定值就仅仅匹配与其相等的传入值。

咱们也能够匹配多个参数:

.mixin (@a) { color: @a; } .mixin (@a, @b) { color: fade(@a, @b); } 

Now if we call .mixin with a single argument, we will get the output of the first definition, but if we call it with two arguments, we will get the second definition, namely @a faded to @b.

引导

当咱们想根据表达式进行匹配,而非根据值和参数匹配时,导引就显得很是有用。若是你对函数式编程很是熟悉,那么你极可能已经使用过导引。

为了尽量地保留CSS的可声明性,LESS经过导引混合而非if/else语句来实现条件判断,由于前者已在@media query特性中被定义。

以此例作为开始:

.mixin (@a) when (lightness(@a) >= 50%) { background-color: black; } .mixin (@a) when (lightness(@a) < 50%) { background-color: white; } .mixin (@a) { color: @a; } 

when关键字用以定义一个导引序列(此例只有一个导引)。接下来咱们运行下列代码:

.class1 { .mixin(#ddd) } .class2 { .mixin(#555) } 

就会获得:

.class1 { background-color: black; color: #ddd; } .class2 { background-color: white; color: #555; } 

导引中可用的所有比较运算有: > >= = =< <。此外,关键字true只表示布尔真值,下面两个混合是相同的:

.truth (@a) when (@a) { ... } .truth (@a) when (@a = true) { ... } 

除去关键字true之外的值都被视示布尔假:

.class { .truth(40); // Will not match any of the above definitions. } 

导引序列使用逗号‘,’—分割,当且仅当全部条件都符合时,才会被视为匹配成功。

.mixin (@a) when (@a > 10), (@a < -10) { ... } 

导引能够无参数,也能够对参数进行比较运算:

@media: mobile; .mixin (@a) when (@media = mobile) { ... } .mixin (@a) when (@media = desktop) { ... } .max (@a, @b) when (@a > @b) { width: @a } .max (@a, @b) when (@a < @b) { width: @b } 

最后,若是想基于值的类型进行匹配,咱们就可使用is*函式:

.mixin (@a, @b: 0) when (isnumber(@b)) { ... } .mixin (@a, @b: black) when (iscolor(@b)) { ... } 

下面就是常见的检测函式:

  • iscolor
  • isnumber
  • isstring
  • iskeyword
  • isurl

若是你想判断一个值是纯数字,仍是某个单位量,可使用下列函式:

  • ispixel
  • ispercentage
  • isem

最后再补充一点,在导引序列中可使用and关键字实现与条件:

.mixin (@a) when (isnumber(@a)) and (@a > 0) { ... } 

使用not关键字实现或条件

.mixin (@b) when not (@b > 0) { ... } 

嵌套规则

LESS 可让咱们以嵌套的方式编写层叠样式. 让咱们先看下下面这段 CSS:

#header { color: black; } #header .navigation { font-size: 12px; } #header .logo { width: 300px; } #header .logo:hover { text-decoration: none; } 

在 LESS 中, 咱们就能够这样写:

#header { color: black; .navigation { font-size: 12px; } .logo { width: 300px; &:hover { text-decoration: none } } } 

或者这样写:

#header { color: black; .navigation { font-size: 12px } .logo { width: 300px; &:hover { text-decoration: none } } } 

代码更简洁了,并且感受跟DOM结构格式有点像.

注意 & 符号的使用—若是你想写串联选择器,而不是写后代选择器,就能够用到&了. 这点对伪类尤为有用如 :hover:focus.

例如:

.bordered { &.float { float: left; } .top { margin: 5px; } } 

会输出

.bordered.float { float: left; } .bordered .top { margin: 5px; } 

运算

任何数字、颜色或者变量均可以参与运算. 来看一组例子:

@base: 5%; @filler: @base * 2; @other: @base + @filler; color: #888 / 4; background-color: @base-color + #111; height: 100% / 2 + @filler; 

LESS 的运算已经超出了咱们的指望,它可以分辨出颜色和单位。若是像下面这样单位运算的话:

@var: 1px + 5; 

LESS 会输出 6px.

括号也一样容许使用:

width: (@var + 5) * 2; 

而且能够在复合属性中进行运算:

border: (@width * 2) solid black; 

Color 函数

LESS 提供了一系列的颜色运算函数. 颜色会先被转化成 HSL 色彩空间, 而后在通道级别操做:

lighten(@color, 10%); // return a color which is 10% *lighter* than @color darken(@color, 10%); // return a color which is 10% *darker* than @color saturate(@color, 10%); // return a color 10% *more* saturated than @color desaturate(@color, 10%); // return a color 10% *less* saturated than @color fadein(@color, 10%); // return a color 10% *less* transparent than @color fadeout(@color, 10%); // return a color 10% *more* transparent than @color fade(@color, 50%); // return @color with 50% transparency spin(@color, 10); // return a color with a 10 degree larger in hue than @color spin(@color, -10); // return a color with a 10 degree smaller hue than @color mix(@color1, @color2); // return a mix of @color1 and @color2 

使用起来至关简单:

@base: #f04615; .class { color: saturate(@base, 5%); background-color: lighten(spin(@base, 8), 25%); } 

你还能够提取颜色信息:

hue(@color); // returns the `hue` channel of @color saturation(@color); // returns the `saturation` channel of @color lightness(@color); // returns the 'lightness' channel of @color 

若是你想在一种颜色的通道上建立另外一种颜色,这些函数就显得那么的好用,例如:

@new: hsl(hue(@old), 45%, 90%); 

@new 将会保持 @old色调, 可是具备不一样的饱和度和亮度.

Math 函数

LESS提供了一组方便的数学函数,你可使用它们处理一些数字类型的值:

round(1.67); // returns `2` ceil(2.4); // returns `3` floor(2.6); // returns `2` 

若是你想将一个值转化为百分比,你可使用percentage 函数:

percentage(0.5); // returns `50%` 

命名空间

有时候,你可能为了更好组织CSS或者单纯是为了更好的封装,将一些变量或者混合模块打包起来, 你能够像下面这样在#bundle中定义一些属性集以后能够重复使用:

#bundle { .button () { display: block; border: 1px solid black; background-color: grey; &:hover { background-color: white } } .tab { ... } .citation { ... } } 

你只须要在 #header a中像这样引入 .button:

#header a { color: orange; #bundle > .button; } 

做用域

LESS 中的做用域跟其余编程语言很是相似,首先会从本地查找变量或者混合模块,若是没找到的话会去父级做用域中查找,直到找到为止.

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

注释

CSS 形式的注释在 LESS 中是依然保留的:

/* Hello, I'm a CSS-style comment */ .class { color: black } 

LESS 一样也支持双斜线的注释, 可是编译成 CSS 的时候自动过滤掉:

// Hi, I'm a silent comment, I won't show up in your CSS .class { color: white } 

Importing

你能够在main文件中经过下面的形势引入 .less 文件, .less 后缀可带可不带:

@import "lib.less"; @import "lib"; 

若是你想导入一个CSS文件并且不想LESS对它进行处理,只须要使用.css后缀就能够:

@import "lib.css"; 

这样LESS就会跳过它不去处理它.

字符串插值

变量能够用相似ruby和php的方式嵌入到字符串中,像@{name}这样的结构:

@base-url: "http://assets.fnord.com"; background-image: url("@{base-url}/images/bg.png"); 

避免编译

有时候咱们须要输出一些不正确的CSS语法或者使用一些 LESS不认识的专有语法.

要输出这样的值咱们能够在字符串前加上一个 ~, 例如:

.class { filter: ~"ms:alwaysHasItsOwnSyntax.For.Stuff()"; } 

咱们能够将要避免编译的值用 “”包含起来,输出结果为:

.class { filter: ms:alwaysHasItsOwnSyntax.For.Stuff(); } 

JavaScript 表达式

JavaScript 表达式也能够在.less 文件中使用. 能够经过反引号的方式使用:

@var: `"hello".toUpperCase() + '!'`; 

输出:

@var: "HELLO!"; 

注意你也能够同时使用字符串插值和避免编译:

@str: "hello"; @var: ~`"@{str}".toUpperCase() + '!'`; 

输出:

@var: HELLO!; 

它也能够访问JavaScript环境:

@height: `document.body.clientHeight`; 

若是你想将一个JavaScript字符串解析成16进制的颜色值, 你可使用 color 函数:

@color: color(`window.colors.baseColor`); @darkcolor: darken(@color, 10%); 
相关文章
相关标签/搜索