Home > Web Front-end > CSS Tutorial > How can I use variables to dynamically create property names in LESS?

How can I use variables to dynamically create property names in LESS?

Barbara Streisand
Release: 2024-11-14 21:59:02
Original
930 people have browsed it

How can I use variables to dynamically create property names in LESS?

Using variables in property names in LESS (dynamic properties / property name interpolation)

LESS doesn't currently support dynamically inserted properties, despite some discussions on the topic on Stack Overflow.

Workaround #1: Inject Dynamically Generated Properties into Property Value

This workaround injects dynamically created properties into a hard-coded property value:

.vendors(@property, @value, @pre: ect) {
  -inj:~"@{pre}; -webkit-@{property}: @{value}; -moz-@{property}: @{value}; -ms-@{property}: @{value}; -o-@{property}: @{value}; @{property}: @{value}";
}
Copy after login

Workaround #2: Inject Dynamically Generated Properties into the Name of the Following Class (LESS < 1.4.0)

This workaround constructs a virtual class or ruleset that includes the vendors and recursively builds the next class:

.vendors(@property, @value, @rest:&quot;&quot;) {
  @inject:~&quot;@{rest} -webkit-@{property}: @{value}; -moz-@{property}: @{value}; -ms-@{property}: @{value}; -o-@{property}: @{value}; @{property}: @{value};&quot;;
}

.test(@nextclass){
  .vendors(transform, &quot;matrix(2,0,0,2,20,20)&quot;);
  .vendors(transform-origin,&quot;10px 10px&quot;, @inject);
  (~&quot;{@{inject}} .@{nextclass}&quot;){/*next class properties*/};
}
Copy after login

Workaround #3: Inject Dynamically Generated Properties into the Name of the Following Class (LESS 1.4.0 )

This version uses recursion to overcome limitations in LESS 1.4.0:

@nl: `"\n\t"`;

.multi(@props,@vals,@i,@inj) {
  @property: extract(@props, 1);
  @value: extract(@vals, 1);
  @inject:~"@{inj}@{nl}-webkit-@{property}: @{value};@{nl}-moz-@{property}: @{value};@{nl}-ms-@{property}: @{value};@{nl}-o-@{property}: @{value};@{nl}@{property}: @{value};";
}

.multi(@props,@vals,@i,@inj:"") when (@i > 0) {
  @property: extract(@props, @i);
  @value: extract(@vals, @i);
  @injnext:~"@{inj}@{nl}-webkit-@{property}: @{value};@{nl}-moz-@{property}: @{value};@{nl}-ms-@{property}: @{value};@{nl}-o-@{property}: @{value};@{nl}@{property}: @{value};";
  .multi(@props,@vals,(@i - 1),@injnext);
}

In LESS versions 1.6 and above, property name interpolation is implemented natively, so no workarounds are needed.

The above is the detailed content of How can I use variables to dynamically create property names in LESS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template