Table of Contents
Namespace:
Keyword !important:
Keyword @arguments:
Keyword @reset:
Pattern matching:
Use the expression:
Home Web Front-end HTML Tutorial less syntax (2) mixed attributes_html/css_WEB-ITnose

less syntax (2) mixed attributes_html/css_WEB-ITnose

Jun 24, 2016 am 11:52 AM

Summary:

We introduced the variables and extend syntax of less before. Today we are studying the mixed attributes (Mixin). Mixing can be said to be another feature of less. You can define common properties together and then call this mixing property directly when using it.

Mixing:

In LESS we can define some common attribute sets as a selector, and then call these attributes in another selector. For example:

.a, #b {  color: red;}.mixin-class {  .a();}.mixin-id {  #b();}
Copy after login

After compilation

.a, #b {  color: red;}.mixin-class {  color: red;}.mixin-id {  color: red;}
Copy after login

Note: When calling mix, you can add parentheses or not. The following one is also correct:

.a, #b {  color: red;}.mixin-class {  .a;}.mixin-id {  #b;}
Copy after login

If you only want to define a mix, you can add brackets after the selector, as follows:

.my-mixin {  color: black;}.my-other-mixin() {  background: white;}.class {  .my-mixin;  .my-other-mixin;}
Copy after login

After compilation, the bracketed .my-other-mixin() will not be compiled.

.my-mixin {  color: black;}.class {  color: black;  background: white;}
Copy after login

Any CSS class, id or element attribute set can be introduced in the same way. Selectors can be nested within universal selectors.

Namespace:

If you want to mix attributes in a more complex selector, you can stack multiple IDs or classes. As follows:

#outer {  .inner {    color: red;  }}
Copy after login

If you want to use this mixed attribute, you can do this. The following four are equivalent

.c{    #outer > .inner;}.c{    #outer > .inner();}.c{    #outer.inner;}.c{    #outer.inner();}
Copy after login

You can define the mix properties under an id to avoid conflicts with other mixes.

Keyword !important:

If you add the !important keyword after using a mixed attribute, all attributes in the mix will be added with the keyword !important. For example:

.foo (@bg: #f5f5f5, @color: #900) {  background: @bg;  color: @color;}.unimportant {  .foo(1);}.important {  .foo(2) !important;}
Copy after login

Compiled

.unimportant {  background: #f5f5f5;  color: #900;}.important {  background: #f5f5f5 !important;  color: #900 !important;}
Copy after login

Mixing with parameters:

Mixed properties can also pass parameters through parentheses, as follows:

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

We only need to pass one parameter when using it, as follows:

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

Of course, we can also give the parameter a default value, so that we can pass a value or not when using it. As follows:

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

If we do not pass a value, the default value of 5px will be used.

Of course we can also pass multiple parameters, as follows:

.mixin(@color) {  color-1: @color;}.mixin(@color; @padding:2) {  color-2: @color;  padding-2: @padding;}.mixin(@color; @padding; @margin: 2) {  color-3: @color;  padding-3: @padding;  margin: @margin @margin @margin @margin;}.some .selector div {  .mixin(#008000);}
Copy after login

After compilation

.some .selector div {  color-1: #008000;  color-2: #008000;  padding-2: 2;}
Copy after login

From the compilation results, we can see that less also has the feature of function overloading. When we define the same mix attribute name with different parameters, and then call .mixin(#008000);, both the first and second mixes will match, but the third one lacks the value of the parameter @padding, so the third mix will not be referenced. property.

We can not only pass multiple values, but also specify attribute names to pass values, as follows:

.mixin(@color: black; @margin: 10px; @padding: 20px) {  color: @color;  margin: @margin;  padding: @padding;}.class1 {  .mixin(@margin: 20px; @color: #33acfe);}.class2 {  .mixin(#efca44; @padding: 40px);}
Copy after login

Keyword @arguments:

@arguments has a special meaning, similar to arguments in js. It contains all parameters passed to mixed properties, as follows:

.box-shadow(@x: 0; @y: 0; @blur: 1px; @color: #000) {  -webkit-box-shadow: @arguments;     -moz-box-shadow: @arguments;          box-shadow: @arguments;}.big-block {  .box-shadow(2px; 5px);}
Copy after login

After compilation

.big-block {  -webkit-box-shadow: 2px 5px 1px #000;     -moz-box-shadow: 2px 5px 1px #000;          box-shadow: 2px 5px 1px #000;}
Copy after login

Keyword @reset:

Different from @arguments, @reset contains parameters other than those specified External parameters, for example:

.mixin(@a; @rest...) {   // @rest包含了@a之后的参数   // @arguments包含了所有参数}
Copy after login

Pattern matching:

Sometimes you want the mix to be different based on the parameters you pass in Things, such as:

.mixin(dark; @color) {  color: darken(@color, 10%);}.mixin(light; @color) {  color: lighten(@color, 10%);}.mixin(@_; @color) {  display: block;}.class {  .mixin(@switch; #888);}
Copy after login

For .class, you assign different values ​​to the variable @switch, and different mixed properties will be called, such as

@switch: light;
Copy after login

Compiled

.class {  color: #a2a2a2;  display: block;}
Copy after login

Using Mixin as a function:

When we put When a mixin is used as a function, the variables in the function are available after the function is called, unless the element calling the mixin attribute defines the same variable itself. For example:

.mixin() {  @width:  100%;  @height: 200px;}.caller {  .mixin();  width:  @width;  height: @height;}
Copy after login

After compilation,

.caller {  width:  100%;  height: 200px;}
Copy after login

Use the expression:

.average(@x, @y) {  @average: ((@x + @y) / 2);}div {  .average(16px, 50px); // "call" the mixin  padding: @average;    // use its "return" value}
Copy after login

After compilation

div {  padding: 33px;}
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)

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

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

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.

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