less学习笔记
导入
可以通过下面的形势引入 .less
文件, .less
后缀可带可不带:
@import "lib.less";
@import "lib";
如果导入一个CSS文件而且不想LESS对它进行处理,只需要使用.css
后缀就行:
@import "lib.css";
变量
less
的变量和sass
的变量类似(使用@
而不是$
),但是相比之下缺少默认变量(!default
)、特殊变量(将变量值作为属性值)和多值变量 :
@nice-blue: #5B83AD;
@light-blue: @nice-blue + #111;
#header { color: @light-blue; }
也可以用变量名定义为变量:
@fnord: "I am fnord.";
@var: 'fnord';
content: @@var;
嵌套
less
可以让我们以嵌套的方式编写层叠样式,同时与sass
一样,less
也可以在选择器嵌套中使用&
表示父元素选择器。
#header {
color: black;
.navigation {
font-size: 12px;
}
.logo {
width: 300px;
&:hover { text-decoration: none }
}
}
不过less
没有sass
中的属性嵌套的功能。
混合
不同于sass
中使用@mixin
声明混合,然后再通过@include
调用,在less
中只需直接把需要声明为混合的属性集作为一个class
,然后在需要的地方直接调用该class
。
无参数
.bordered {
border-top: dotted 1px black;
border-bottom: solid 2px black;
}
#menu a {
color: #111;
.bordered;
}
.post a {
color: red;
.bordered;
}
有参数
.border-radius (@radius) {
border-radius: @radius;
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
}
#header {
.border-radius(4px);
}
.button {
.border-radius(6px);
}
也可以设置默认值:
.border-radius (@radius: 5px) {
border-radius: @radius;
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
}
也可以定义不带参数属性集合,这样的好处是隐藏这个属性集合,不让它暴露到CSS中去,同时还能在其他的属性集合中引用:
.wrap () {
text-wrap: wrap;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
word-wrap: break-word;
}
pre { .wrap }
@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
根据不同的@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;
//第三个mixin任何参数都能匹配到
display: block;
}
我们也可以匹配多个参数:
.mixin (@a) {
color: @a;
}
.mixin (@a, @b) {
color: fade(@a, @b);
}
引导
当我们想根据表达式进行匹配,而非根据值和参数匹配时,导引就显得非常有用。
.mixin (@a) when (lightness(@a) >= 50%) {
background-color: black;
}
.mixin (@a) when (lightness(@a) < 50%) {
background-color: white;
}
//匹配所有
.mixin (@a) {
color: @a;
}
引序列使用逗号,
—分割,当且仅当所有条件都符合时,才会被视为匹配成功。
.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)) { ... }
最后,在导引序列中可以使用and
关键字实现与条件:
.mixin (@a) when (isnumber(@a)) and (@a > 0) { ... }
使用not
关键字实现或条件
.mixin (@b) when not (@b > 0) { ... }
运算
less
和sass
中的运算并没有什么差别:
//在复合属性中进行运算
border: (@width * 2) solid black;
命名空间
有时候,为了更好组织CSS或者单纯是为了更好的封装,将一些变量或者混合模块打包起来, 你可以像下面这样在#bundle
中定义一些属性集之后可以重复使用(可以理解为混合的集合):
#bundle {
.button () {
display: block;
border: 1px solid black;
background-color: grey;
&:hover { background-color: white }
}
.tab { ... }
.citation { ... }
}
例如需要引入.button
:
#header a {
color: orange;
#bundle > .button;
}
作用域
less
和lass
不同,确实是存在局部作用域的:
@var: red;
#page {
@var: white;
#header {
color: @var; // white
}
}
#footer {
color: @var; // red
}
字符串插值
变量可以用类似ruby
和php
的方式嵌入到字符串中,像@{name}
这样的结构:
@base-url: "http://assets.fnord.com";
background-image: url("@{base-url}/images/bg.png");
JavaScript 表达式
JavaScript
表达式也可以在.less
文件中使用. 可以通过反引号的方式使用:
@var: `"hello".toUpperCase() + '!'`;
它也可以访问JavaScript
环境:
@height: `document.body.clientHeight`;
这篇学习笔记是我该文档 写的,在这之前我先去了解了sass
,同时在学习less
的时候也是一边参照sass
笔记,一边看文档的。看完语法之后我发现两者可以说是很像又可以说有挺大差别,我个人认为sass
的功能较less
会更加强大一些,但是less
优在不需要怎么配置环境。选择哪个应该视情况而定。