Table of Contents
Lazy loading:
Syntax:
Nested selectors:
Exact match:
nth:
Attribute selector:
Keyword all:
Variable selector:
@media:
Use extend to rewrite styles:
Reduce css code:
Home Web Front-end HTML Tutorial less syntax (1) variables and extend_html/css_WEB-ITnose

less syntax (1) variables and extend_html/css_WEB-ITnose

Jun 24, 2016 am 11:52 AM

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 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 purpose of variables is to define values ​​in one place and then use them everywhere, which makes the code easier to maintain, as follows:

// Variables@link-color:        #428bca; // sea blue// 用法a:link {  color: @link-color;}.widget {  color: #fff;  background: @link-color;}
Copy after login

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:

a:link {  color: #428bca;}.widget {  color: #fff;  background: #428bca;}
Copy after login

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:

// Variables@mySelector: banner;// Usage.@{mySelector} {  font-weight: bold;  line-height: 40px;  margin: 0 auto;}
Copy after login

After compilation, it is

.banner {  font-weight: bold;  line-height: 40px;  margin: 0 auto;}
Copy after login

url:

// Variables@images: "../img";// 用法body {  color: #444;  background: url("@{images}/white-sand.png");}
Copy after login

Compiled

body {  color: #444;  background: url("../img/white-sand.png");}
Copy after login

@import:

// Variables@themes: "../../src/themes";// Usage@import "@{themes}/tidal-wave.less";
Copy after login

After compilation

@import "../../src/themes/tidal-wave.less";
Copy after login

Attribute name:

@property: color;.widget {  @{property}: #0ee;  background-@{property}: #999;}
Copy after login

After compilation

.widget {  color: #0ee;  background-color: #999;}
Copy after login

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

@fnord:  "I am fnord.";@var:    "fnord";content: @@var;
Copy after login

After compilation

content: "I am fnord.";
Copy after login

Lazy loading:

Variables support delay loaded so you can use it before the variable is defined. As follows:

.lazy-eval {  width: @var;}@var: @a;@a: 9%;
Copy after login

or

.lazy-eval-scope {  width: @var;  @a: 9%;}@var: @a;@a: 100%;
Copy after login

Both of the above will be Compiled into the following

.lazy-eval-scope {  width: 9%;}
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 following

@var: 0;.class1 {  @var: 1;  .class {    @var: 2;    three: @var;    @var: 3;  }  one: @var;}
Copy after login

After compilation

.class1 .class {  three: 3;}.class {  one: 1;}
Copy after login

Extend:

The extension selector is less Pseudo-class selector, it will copy the current selector and define a new style, and the original inconvenience

nav ul {  &:extend(.inline);  background: blue;}.inline {  color: red;}
Copy after login

After compilation

nav ul {  background: blue;}.inline,nav ul {  color: red;}
Copy after login

Syntax:

.a:extend(.b) {}也可以这样使用.a {  &:extend(.b);}
Copy after login

.e:extend(.f) {}.e:extend(.g) {}// 上面等价于下面.e:extend(.f, .g) {}
Copy after login

Nested selectors:

.bucket {  tr {     color: blue;  }}.some-class:extend(.bucket tr) {}
Copy after login

Compiled

.bucket tr,.some-class {  color: blue;}
Copy after login

Exact match:

.a.class,.class.a,.class > .a {  color: blue;}.test:extend(.class) {} // 不会匹配任何选择
Copy after login

nth:

:nth-child(1n+3) {  color: blue;}.child:extend(n+3) {}
Copy after login

Compile After

:nth-child(1n+3) {  color: blue;}
Copy after login

Note: 1n 3 and n 3 are equivalent in css, but not equivalent in less .

Attribute selector:

[title=identifier] {  color: blue;}[title='identifier'] {  color: blue;}[title="identifier"] {  color: blue;}.noQuote:extend([title=identifier]) {}.singleQuote:extend([title='identifier']) {}.doubleQuote:extend([title="identifier"]) {}
Copy after login

Compiled

[title=identifier],.noQuote,.singleQuote,.doubleQuote {  color: blue;}[title='identifier'],.noQuote,.singleQuote,.doubleQuote {  color: blue;}[title="identifier"],.noQuote,.singleQuote,.doubleQuote {  color: blue;}
Copy after login

Note: less does not distinguish between single quotes and double quotes

Keyword all:

.a.b.test,.test.c {  color: orange;}.test {  &:hover {    color: green;  }}.replacement:extend(.test all) {}
Copy after login

After compilation

.a.b.test,.test.c,.a.b.replacement,.replacement.c {  color: orange;}.test:hover,.replacement:hover {  color: green;}
Copy after login

Variable selector:

@variable: .bucket;@{variable} { // interpolated selector  color: blue;}.some-class:extend(.bucket) {}// 不会匹配任何选择元素
Copy after login

.bucket {  color: blue;}.some-class:extend(@{variable}) {} // 不会匹配任何元素@variable: .bucket;
Copy after login

Note: extend does not match variables.

@media:

@media print {  .screenClass:extend(.selector) {} // extend inside media  .selector {     color: black;  }}.selector {   color: red;}@media screen {  .selector {      color: blue;  }}
Copy after login

After compilation

@media print {  .selector,  .screenClass {     color: black;  }}.selector {   color: red;}@media screen {  .selector {     color: blue;  }}
Copy after login

Note: extend can only match those defined earlier in @media, and those defined later will be ignored.

Use extend to rewrite styles:

During development, we will define some common styles, then add classes to individual styles, and use the back of css to overwrite the previous principles to implement the style. extend can also achieve this effect, as follows:

.animal {  background-color: black;  color: white;}.bear {  &:extend(.animal);  background-color: brown;}
Copy after login

Reduce css code:

.my-inline-block() {    display: inline-block;  font-size: 0;}.thing1 {  .my-inline-block;}.thing2 {  .my-inline-block;}
Copy after login

After compilation:

.thing1 {  display: inline-block;  font-size: 0;}.thing2 {  display: inline-block;  font-size: 0;}
Copy after login

Use extend

.my-inline-block {  display: inline-block;  font-size: 0;}.thing1 {  &:extend(.my-inline-block);}.thing2 {  &:extend(.my-inline-block);}
Copy after login

After compilation

.my-inline-block,.thing1,.thing2 {  display: inline-block;  font-size: 0;}
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

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 <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

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

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.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles