Using dynamic properties and property name interpolation in LESS
Some programming frameworks like CSS SASS supports dynamic injection of property names using the @ syntax, allowing you to inject the names and values of properties into a selector. It's a great way to override common properties across multiple rules, apply transforms, and manage complex styles without directly declaring them.
Unfortunately, LESS lacks such a feature, but it does offer some workarounds using various combinations of parametric mix-ins, variable injection (e() function) and pattern matching (@{ }) syntax.
Option 1: Using the e() Function to Inject Property Names
This method involves injecting the dynamically created property names into the value of another property. Though ungraceful, it does the job.
1 2 3 4 5 6 7 |
|
Option 2: Injecting Properties into Following Class Names (Less v1.3.3)
This method involves defining a vendor mix-in and then constructing a virtual class/ruleset that includes the vendor properties. The next class is then constructed using the ~ syntax to bind the two vendor blocks together.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Option 3: Injecting Properties using Recursion (Less v1.4.0 )
This method addresses the limitations of the previous one when using less v1.4 , introducing recursion to construct vendor blocks and inject them into a final variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
These workarounds provide a way to create dynamic property names in LESS, although it's not as elegant as using the built-in features of other frameworks. With the release of LESS 1.6, dynamic property name interpolation has been implemented, making this process much simpler. The syntax is similar to Option 2, using @{ } for property names.
The above is the detailed content of How can you use dynamic property names in LESS, given that it doesn't have a built-in feature for dynamic injection?. For more information, please follow other related articles on the PHP Chinese website!