less语法(一)变量与extend

WBOY
풀어 주다: 2016-07-21 14:53:06
원래의
1355명이 탐색했습니다.

摘要:

   作为 CSS 的一种扩展,Less 不仅完全兼容 CSS 语法,而且连新增的特性也是使用 CSS 语法。这样的设计使得学习 Less 很轻松,而且你可以在任何时候回退到 CSS。less文件是以less作为文件后缀名,HTML引用时可以像css那样引用,如下:

注意:本文描述的一切都是基于1.4.0版本,除非有特殊标明。

变量:

   变量的作用就是把值定义在一个地方,然后在各处使用,这样能让代码更易维护,如下:

复制代码
<span style="color: #800000;">// Variables
@link-color:        #428bca; // sea blue

// 用法
a:link </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> @link-color</span>;
}<span style="color: #800000;">
.widget </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> #fff</span>;<span style="color: #ff0000;">
  background</span>:<span style="color: #0000ff;"> @link-color</span>;
}
로그인 후 복사
复制代码

 

上面代码将颜色#428bca赋给一个变量@link-color,然后在color属性中使用变量,对应的css如下:

复制代码
<span style="color: #800000;">a:link </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> #428bca</span>;
}<span style="color: #800000;">
.widget </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> #fff</span>;<span style="color: #ff0000;">
  background</span>:<span style="color: #0000ff;"> #428bca</span>;
}
로그인 후 복사
复制代码

 

变量不仅可以用在属性值上,也可以用在选择元素名,属性名(1.6.0支持),url和import方法。如下:

选择元素名:

复制代码
<span style="color: #800000;">// Variables
@mySelector: banner;

// Usage
.@</span>{<span style="color: #ff0000;">mySelector</span>} {<span style="color: #ff0000;">
  font-weight</span>:<span style="color: #0000ff;"> bold</span>;<span style="color: #ff0000;">
  line-height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;">
  margin</span>:<span style="color: #0000ff;"> 0 auto</span>;
}
로그인 후 복사
复制代码

 

编译后为

<span style="color: #800000;">.banner </span>{<span style="color: #ff0000;">
  font-weight</span>:<span style="color: #0000ff;"> bold</span>;<span style="color: #ff0000;">
  line-height</span>:<span style="color: #0000ff;"> 40px</span>;<span style="color: #ff0000;">
  margin</span>:<span style="color: #0000ff;"> 0 auto</span>;
}
로그인 후 복사

 

url:

复制代码
<span style="color: #800000;">// Variables
@images: "../img";

// 用法
body </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> #444</span>;<span style="color: #ff0000;">
  background</span>:<span style="color: #0000ff;"> url("@{images</span>}<span style="color: #800000;">/white-sand.png");
}</span>
로그인 후 복사
复制代码

 

编译后

<span style="color: #800000;">body </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> #444</span>;<span style="color: #ff0000;">
  background</span>:<span style="color: #0000ff;"> url("../img/white-sand.png")</span>;
}
로그인 후 복사

 

@import:

<span style="color: #800000;">// Variables
@themes: "../../src/themes";

// Usage
@import "@</span>{<span style="color: #ff0000;">themes</span>}<span style="color: #800000;">/tidal-wave.less";</span>
로그인 후 복사

 

编译后

<span style="color: #800000;">@import "../../src/themes/tidal-wave.less";</span>
로그인 후 복사

 

属性名:

<span style="color: #800000;">@property: color;

.widget </span>{<span style="color: #ff0000;">
  @{property</span>}<span style="color: #800000;">: #0ee;
  background-@</span>{<span style="color: #ff0000;">property</span>}<span style="color: #800000;">: #999;
}</span>
로그인 후 복사

 

编译后

<span style="color: #800000;">.widget </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> #0ee</span>;<span style="color: #ff0000;">
  background-color</span>:<span style="color: #0000ff;"> #999</span>;
}
로그인 후 복사

 

 

变量的变量名也可以是变量,如下:

<span style="color: #800000;">@fnord:  "I am fnord.";
@var:    "fnord";
content: @@var;</span>
로그인 후 복사

 

编译后

<span style="color: #800000;">content: "I am fnord.";</span>
로그인 후 복사

 

 

延迟加载:

  变量支持延迟加载,所以你可以在变量定义之前使用。如下:

<span style="color: #800000;">.lazy-eval </span>{<span style="color: #ff0000;">
  width</span>:<span style="color: #0000ff;"> @var</span>;
}<span style="color: #800000;">

@var: @a;
@a: 9%;</span>
로그인 후 복사

 

或者

复制代码
<span style="color: #800000;">.lazy-eval-scope </span>{<span style="color: #ff0000;">
  width</span>:<span style="color: #0000ff;"> @var</span>;<span style="color: #ff0000;">
  @a</span>:<span style="color: #0000ff;"> 9%</span>;
}<span style="color: #800000;">

@var: @a;
@a: 100%;</span>
로그인 후 복사
复制代码

 

上面两个都会被编译成如下

<span style="color: #800000;">.lazy-eval-scope </span>{<span style="color: #ff0000;">
  width</span>:<span style="color: #0000ff;"> 9%</span>;
}
로그인 후 복사

 

问什么第二个也会被编译成上面的css,这是因为当一个变量被定义两次时,最后一次定义的生效。就类似于css中,对同一个元素定义不同的css样式,最后定义的生效。再比如下面这个

复制代码
<span style="color: #800000;">@var: 0;
.class1 </span>{<span style="color: #ff0000;">
  @var</span>:<span style="color: #0000ff;"> 1</span>;<span style="color: #ff0000;">
  .class {
    @var</span>:<span style="color: #0000ff;"> 2</span>;<span style="color: #ff0000;">
    three</span>:<span style="color: #0000ff;"> @var</span>;<span style="color: #ff0000;">
    @var</span>:<span style="color: #0000ff;"> 3</span>;
  }<span style="color: #800000;">
  one: @var;
}</span>
로그인 후 복사
复制代码

编译后

<span style="color: #800000;">.class1 .class </span>{<span style="color: #ff0000;">
  three</span>:<span style="color: #0000ff;"> 3</span>;
}<span style="color: #800000;">
.class </span>{<span style="color: #ff0000;">
  one</span>:<span style="color: #0000ff;"> 1</span>;
}
로그인 후 복사

 

Extend: 

  扩展选择器是less的伪类选择器,他会复制当前选择器,定义新的样式,而原来的不便

复制代码
<span style="color: #800000;">nav ul </span>{<span style="color: #ff0000;">
  &</span>:<span style="color: #0000ff;">extend(.inline)</span>;<span style="color: #ff0000;">
  background</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">
.inline </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> red</span>;
}
로그인 후 복사
复制代码

 

编译后

复制代码
<span style="color: #800000;">nav ul </span>{<span style="color: #ff0000;">
  background</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">
.inline,
nav ul </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> red</span>;
}
로그인 후 복사
复制代码

语法:

<span style="color: #800000;">.a:extend(.b) </span>{}<span style="color: #800000;">
也可以这样使用
.a </span>{<span style="color: #ff0000;">
  &</span>:<span style="color: #0000ff;">extend(.b)</span>;
}
로그인 후 복사

 

 

<span style="color: #800000;">.e:extend(.f) </span>{}<span style="color: #800000;">
.e:extend(.g) </span>{}<span style="color: #800000;">
// 上面等价于下面
.e:extend(.f, .g) </span>{}
로그인 후 복사

 

嵌套选择器:

<span style="color: #800000;">.bucket </span>{<span style="color: #ff0000;">
  tr { 
    color</span>:<span style="color: #0000ff;"> blue</span>;
  }<span style="color: #800000;">
}
.some-class:extend(.bucket tr) </span>{}
로그인 후 복사

 

编译后

<span style="color: #800000;">.bucket tr,
.some-class </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}
로그인 후 복사

 

精确匹配:

<span style="color: #800000;">.a.class,
.class.a,
.class > .a </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">
.test:extend(.class) </span>{}<span style="color: #800000;"> // 不会匹配任何选择</span>
로그인 후 복사

 

nth:

<span style="color: #800000;">:nth-child(1n+3) </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">
.child:extend(n+3) </span>{}
로그인 후 복사

 

编译后

<span style="color: #800000;">:nth-child(1n+3) </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}
로그인 후 복사

 

注意:1n+3与n+3在css中是等价的,但是在less中不等价。

属性选择器:

复制代码
<span style="color: #800000;">[title=identifier] </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">
[title='identifier'] </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">
[title="identifier"] </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">

.noQuote:extend([title=identifier]) </span>{}<span style="color: #800000;">
.singleQuote:extend([title='identifier']) </span>{}<span style="color: #800000;">
.doubleQuote:extend([title="identifier"]) </span>{}
로그인 후 복사
复制代码

 

编译后

复制代码
<span style="color: #800000;">[title=identifier],
.noQuote,
.singleQuote,
.doubleQuote </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">

[title='identifier'],
.noQuote,
.singleQuote,
.doubleQuote </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">

[title="identifier"],
.noQuote,
.singleQuote,
.doubleQuote </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}
로그인 후 복사
复制代码

 

注意:less中不区分单引号双引号

关键字all:

复制代码
<span style="color: #800000;">.a.b.test,
.test.c </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> orange</span>;
}<span style="color: #800000;">
.test </span>{<span style="color: #ff0000;">
  &</span>:<span style="color: #0000ff;">hover {
    color: green</span>;
  }<span style="color: #800000;">
}

.replacement:extend(.test all) </span>{}
로그인 후 복사
复制代码

 

编译后

复制代码
<span style="color: #800000;">.a.b.test,
.test.c,
.a.b.replacement,
.replacement.c </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> orange</span>;
}<span style="color: #800000;">
.test:hover,
.replacement:hover </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> green</span>;
}
로그인 후 복사
复制代码

 

变量选择器:

<span style="color: #800000;">@variable: .bucket;
@</span>{<span style="color: #ff0000;">variable</span>} {<span style="color: #ff0000;"> // interpolated selector
  color</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">
.some-class:extend(.bucket) </span>{}<span style="color: #800000;">// 不会匹配任何选择元素</span>
로그인 후 복사
<span style="color: #800000;">.bucket </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}<span style="color: #800000;">
.some-class:extend(@</span>{<span style="color: #ff0000;">variable</span>}<span style="color: #800000;">) </span>{}<span style="color: #800000;"> // 不会匹配任何元素
@variable: .bucket;</span>
로그인 후 복사

 

注意:extend不匹配变量。

@media:

复制代码
<span style="color: #800000;">@media print </span>{<span style="color: #ff0000;">
  .screenClass</span>:<span style="color: #0000ff;">extend(.selector) {</span>}<span style="color: #800000;"> // extend inside media
  .selector </span>{<span style="color: #ff0000;"> 
    color</span>:<span style="color: #0000ff;"> black</span>;
  }<span style="color: #800000;">
}
.selector </span>{<span style="color: #ff0000;"> 
  color</span>:<span style="color: #0000ff;"> red</span>;
}<span style="color: #800000;">
@media screen </span>{<span style="color: #ff0000;">
  .selector {  
    color</span>:<span style="color: #0000ff;"> blue</span>;
  }<span style="color: #800000;">
}</span>
로그인 후 복사
复制代码

 

编译后

复制代码
<span style="color: #800000;">@media print </span>{<span style="color: #ff0000;">
  .selector,
  .screenClass { 
    color</span>:<span style="color: #0000ff;"> black</span>;
  }<span style="color: #800000;">
}
.selector </span>{<span style="color: #ff0000;"> 
  color</span>:<span style="color: #0000ff;"> red</span>;
}<span style="color: #800000;">
@media screen </span>{<span style="color: #ff0000;">
  .selector { 
    color</span>:<span style="color: #0000ff;"> blue</span>;
  }<span style="color: #800000;">
}</span>
로그인 후 복사
复制代码

 

注意:extend只能匹配@media中前面定义的,在后面定义的将忽略。

使用extend重写样式:

在开发中我们会定义一些通用样式,然后单独样式在添加class,使用css的后面覆盖前面的原理来实现样式。extend也可以实现这种效果,如下:

复制代码
<span style="color: #800000;">.animal </span>{<span style="color: #ff0000;">
  background-color</span>:<span style="color: #0000ff;"> black</span>;<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> white</span>;
}<span style="color: #800000;">
.bear </span>{<span style="color: #ff0000;">
  &</span>:<span style="color: #0000ff;">extend(.animal)</span>;<span style="color: #ff0000;">
  background-color</span>:<span style="color: #0000ff;"> brown</span>;
}
로그인 후 복사
复制代码

减少css代码:

复制代码
<span style="color: #800000;">.my-inline-block() </span>{<span style="color: #ff0000;">
    display</span>:<span style="color: #0000ff;"> inline-block</span>;<span style="color: #ff0000;">
  font-size</span>:<span style="color: #0000ff;"> 0</span>;
}<span style="color: #800000;">
.thing1 </span>{<span style="color: #ff0000;">
  .my-inline-block;
</span>}<span style="color: #800000;">
.thing2 </span>{<span style="color: #ff0000;">
  .my-inline-block;
</span>}
로그인 후 복사
复制代码

 

编译后:

复制代码
<span style="color: #800000;">.thing1 </span>{<span style="color: #ff0000;">
  display</span>:<span style="color: #0000ff;"> inline-block</span>;<span style="color: #ff0000;">
  font-size</span>:<span style="color: #0000ff;"> 0</span>;
}<span style="color: #800000;">
.thing2 </span>{<span style="color: #ff0000;">
  display</span>:<span style="color: #0000ff;"> inline-block</span>;<span style="color: #ff0000;">
  font-size</span>:<span style="color: #0000ff;"> 0</span>;
}
로그인 후 복사
复制代码

 

使用extend

复制代码
<span style="color: #800000;">.my-inline-block </span>{<span style="color: #ff0000;">
  display</span>:<span style="color: #0000ff;"> inline-block</span>;<span style="color: #ff0000;">
  font-size</span>:<span style="color: #0000ff;"> 0</span>;
}<span style="color: #800000;">
.thing1 </span>{<span style="color: #ff0000;">
  &</span>:<span style="color: #0000ff;">extend(.my-inline-block)</span>;
}<span style="color: #800000;">
.thing2 </span>{<span style="color: #ff0000;">
  &</span>:<span style="color: #0000ff;">extend(.my-inline-block)</span>;
}
로그인 후 복사
复制代码

 

编译后

<span style="color: #800000;">.my-inline-block,
.thing1,
.thing2 </span>{<span style="color: #ff0000;">
  display</span>:<span style="color: #0000ff;"> inline-block</span>;<span style="color: #ff0000;">
  font-size</span>:<span style="color: #0000ff;"> 0</span>;
}
로그인 후 복사

 

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿