Table of Contents
Summary:
Variables:
Lazy loading:
Extend:
语法:
嵌套选择器:
精确匹配:
nth:
属性选择器:
关键字all:
变量选择器:
@media:
使用extend重写样式:
减少css代码:
Home Web Front-end HTML Tutorial less syntax (1) variables and extend

less syntax (1) variables and extend

Jul 21, 2016 pm 02:53 PM

Summary:

As an extension of CSS, Less is not only fully compatible with CSS syntax, but also uses CSS syntax for new features. This design makes learning Less easy, and you can fall back to CSS at any time. The less file has less as the file suffix. When quoting in HTML, it can be quoted like css, as follows:

Note: Everything described in this article is based on version 1.4.0, unless otherwise noted.

Variables:

The function of a variable is to define the value in one place and then use it everywhere, which makes the code easier to maintain, as follows:

less syntax (1) variables and extend
<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>;
}
Copy after login
less syntax (1) variables and extend

The above code assigns the color #428bca to a variable @link-color, and then uses the variable in the color attribute. The corresponding css is as follows:

less syntax (1) variables and extend
<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>;
}
Copy after login
less syntax (1) variables and extend

Variables can be used not only in attribute values, but also in selecting element names, attribute names (supported in 1.6.0), url and import methods. As follows:

Select element name:

less syntax (1) variables and extend
<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>;
}
Copy after login
less syntax (1) variables and extend

is compiled to

<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>;
}
Copy after login

url:

less syntax (1) variables and extend
<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>
Copy after login
less syntax (1) variables and extend

After compilation

<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>;
}
Copy after login

@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>
Copy after login

After compilation

<span style="color: #800000;">@import "../../src/themes/tidal-wave.less";</span>
Copy after login

Attribute name:

<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>
Copy after login

After compilation

<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>;
}
Copy after login

The variable name of a variable can also be a variable, as follows:

<span style="color: #800000;">@fnord:  "I am fnord.";
@var:    "fnord";
content: @@var;</span>
Copy after login

After compilation

<span style="color: #800000;">content: "I am fnord.";</span>
Copy after login

Lazy loading:

Variables support lazy loading, so you can use them before they are defined. As follows:

<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>
Copy after login

or

less syntax (1) variables and extend
<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>
Copy after login
less syntax (1) variables and extend

The above two will be compiled into the following

<span style="color: #800000;">.lazy-eval-scope </span>{<span style="color: #ff0000;">
  width</span>:<span style="color: #0000ff;"> 9%</span>;
}
Copy after login

Ask why the second one will also be compiled into the above css. This is because when a variable is defined twice, the last definition takes effect. Just like in CSS, different CSS styles are defined for the same element, and the last defined one takes effect. Another example is the one below

less syntax (1) variables and extend
<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>
Copy after login
less syntax (1) variables and extend

After compilation

<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>;
}
Copy after login

Extend:

The extension selector is a pseudo-class selector of less. It will copy the current selector and define a new style without the original inconvenience

less syntax (1) variables and extend
<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>;
}
Copy after login
less syntax (1) variables and extend

 

编译后

less syntax (1) variables and extend
<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>;
}
Copy after login
less syntax (1) variables and extend

语法:

<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>;
}
Copy after login

 

 

<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>{}
Copy after login

 

嵌套选择器:

<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>{}
Copy after login

 

编译后

<span style="color: #800000;">.bucket tr,
.some-class </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}
Copy after login

 

精确匹配:

<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>
Copy after login

 

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>{}
Copy after login

 

编译后

<span style="color: #800000;">:nth-child(1n+3) </span>{<span style="color: #ff0000;">
  color</span>:<span style="color: #0000ff;"> blue</span>;
}
Copy after login

 

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

属性选择器:

less syntax (1) variables and extend
<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>{}
Copy after login
less syntax (1) variables and extend

 

编译后

less syntax (1) variables and extend
<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>;
}
Copy after login
less syntax (1) variables and extend

 

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

关键字all:

less syntax (1) variables and extend
<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>{}
Copy after login
less syntax (1) variables and extend

 

编译后

less syntax (1) variables and extend
<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>;
}
Copy after login
less syntax (1) variables and extend

 

变量选择器:

<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>
Copy after login
<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>
Copy after login

 

注意:extend不匹配变量。

@media:

less syntax (1) variables and extend
<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>
Copy after login
less syntax (1) variables and extend

 

编译后

less syntax (1) variables and extend
<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>
Copy after login
less syntax (1) variables and extend

 

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

使用extend重写样式:

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

less syntax (1) variables and 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>;
}
Copy after login
less syntax (1) variables and extend

减少css代码:

less syntax (1) variables and 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;">
  .my-inline-block;
</span>}<span style="color: #800000;">
.thing2 </span>{<span style="color: #ff0000;">
  .my-inline-block;
</span>}
Copy after login
less syntax (1) variables and extend

 

编译后:

less syntax (1) variables and extend
<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>;
}
Copy after login
less syntax (1) variables and extend

 

使用extend

less syntax (1) variables and 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>;
}
Copy after login
less syntax (1) variables and extend

 

编译后

<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>;
}
Copy after login

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles