Reducing the number of variables used in a for loop

I'm currently grappling with utilizing a loop to generate customized CSS using predefined Less variables. As I am transitioning from SCSS to Less, I could benefit from an extra set of eyes as I am having trouble finding a suitable example.

Imagine you have Less code like this (although in reality, my less vars are stored in a separate file and imported using @import before the loop);

@spacing-xsmall: .25rem; // 4px
@spacing-small: .5rem; // 8px
@spacing-medium: 1rem; // 16px
@spacing-medium-large: 1.5rem; // 24px
@spacing-large: 2rem; // 32px
@spacing-xlarge: 2.5rem; // 40px
@spacing-xxlarge: 4rem; // 64px

@sizes: xsmall, small, medium, medium-large, large, xlarge, xxlarge;

.init-spacing(@i) when (@i > 0) {
  
  @size: extract(@sizes, @i);
  @space: ~"@spacing-@{size}";
  
  .margin-@{size} { margin: @space }

  .padding-@{size} { padding: @space; }
  
  .init-spacing(@i - 1);
}

.init-spacing(length(@sizes));

This produces an output like this;

.margin-xxlarge {
  margin: @spacing-xxlarge;
}
.padding-xxlarge {
  padding: @spacing-xxlarge;
}
....

But ideally, in the CSS output, I would prefer to display the actual variable VALUE instead, resulting in something like;

.margin-xxlarge {
  margin: 4rem;
}
.padding-xxlarge {
  padding: 4rem;
}
....

Is there a way to achieve this with Less? While I suspect that string interpolation might be causing issues, I am struggling to find a solution by reading the documentation.

Here's a Codepen link for experimenting with a reproducible situation illustrating the problem.

I attempted to follow this answer, but the output only displays something like

.class { margin: [object, object] }
. I came across similar solutions, but they didn't address my specific scenario where I need to loop through an array incorporating complex variables rather than simple index numbers. It seems like I am missing some crucial details to create the desired results in a more generic manner so that I can apply it to other comparable cases.

Answer №1

Have you considered using variable variables? It can streamline your CSS code.

@spacing-xsmall: .25rem; // 4px
@spacing-small: .5rem; // 8px
@spacing-medium: 1rem; // 16px
@spacing-medium-large: 1.5rem; // 24px
@spacing-large: 2rem; // 32px
@spacing-xlarge: 2.5rem; // 40px
@spacing-xxlarge: 4rem; // 64px

@sizes: xsmall, small, medium, medium-large, large, xlarge, xxlarge;

.init-spacing(@i) when (@i > 0) {
  
  @size: extract(@sizes, @i);
  @space: ~"spacing-@{size}";
  
  .margin-@{size} { margin: @@space }

  .padding-@{size} { padding: @@space; }
  
  .init-spacing(@i - 1);
}

.init-spacing(length(@sizes));

After applying this code in a preview, I got the following:

.margin-xxlarge {
  margin: 4rem;
}
.padding-xxlarge {
  padding: 4rem;
}
.margin-xlarge {
  margin: 2.5rem;
}
.padding-xlarge {
  padding: 2.5rem;
}
.margin-large {
  margin: 2rem;
}
.padding-large {
  padding: 2rem;
}
.margin-medium-large {
  margin: 1.5rem;
}
.padding-medium-large {
  padding: 1.5rem;
}
.margin-medium {
  margin: 1rem;
}
.padding-medium {
  padding: 1rem;
}
.margin-small {
  margin: 0.5rem;
}
.padding-small {
  padding: 0.5rem;
}
.margin-xsmall {
  margin: 0.25rem;
}
.padding-xsmall {
  padding: 0.25rem;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Update the available choices in one select field based on the choice made in another

I am working on a feature where users can select their country, either USA or Canada. Currently, I have all the American states listed in one dropdown menu. However, I want this dropdown to change to display Canadian provinces when users select Canada. Bel ...

Ways to align the navbar-brand at the center using bootstrap

I have attempted multiple techniques such as flex boxes, auto margins, and transforms to center this navbar-brand element on the page. However, it continues to align between the right side and a button. I want it to disregard the button positioning and ins ...

JS method for gradually reducing the opacity of various div elements

I currently have two divs with background images styled using CSS. My goal is to create a loop that fades them in and out continuously. While attempting to achieve this effect, I encountered some issues and the following code snippet doesn't seem to ...

Bootstrap: Utilizing a full width grid system with columns contained within a container

Looking to design a unique layout that spans the full width of the page, featuring a blue half on the left and a red half on the right. Next, I would like to insert text into the layout, but within a container element. Is this doable? UPDATE: Notice that ...

What steps do I need to follow to create a unique angular component that allows for customizable width using CSS styles?

The instructions on this website suggest that the width of the side-nav can be changed using CSS like so: md-sidenav { width: 200px; } This leads me to wonder, can I apply standard CSS properties such as width, position, etc... to custom components wi ...

Tips for preventing the appearance of two horizontal scroll bars on Firefox

Greetings, I am having an issue with double horizontal scroll bars appearing in Firefox but not in Internet Explorer. When the content exceeds a certain limit, these scroll bars show up. Can someone please advise me on how to resolve this problem? Is ther ...

Creating a Gap in R Shiny Interface Between Two Data Tables

I am currently working with R and Shiny, and I am attempting to display two dataTableOutput elements next to each other. To accomplish this, I am utilizing the fluidRow column feature, but I need to further customize the width of these two tables. Here is ...

Is it possible to conceal a link once it has been visited?

I am trying to figure out how to remove a link from a page once it has been visited. However, I am facing privacy restrictions with the :visited pseudo-class. Is there a way to achieve this without using display: none? For example: .someclass a:link {dis ...

Transforming RGBA, HSL, and HSLA color values into RGB format, or encapsulating them within a java.awt.Color instance

Seeking information on algorithms that can convert RGBA, HSL, and HSLA colors to RGB colors, or a Java library that performs this conversion and returns a java.awt.Color object. Any recommendations or assistance would be greatly appreciated! ...

Creating an element in react-draggable with two sides bound and two sides open - here's how!

At the moment, I am facing an issue where the element is restricted from moving outside of the container with the class of card-width-height. My goal is to have the right and bottom sides bounded within the container while allowing the element to move beyo ...

Tips for emphasizing the current element without disrupting existing styles

Is there a way to highlight an element with a border without causing it to shift? The script seems a bit glitchy when detecting mouse leaving the area. I'm unable to override parent element properties like border or outline. I also can't use pse ...

Setting up Next.js to work with Ant Design and utilize both Less and Sass with CSS modules

Can anyone guide me on how to integrate Next.js with Sass and CSS modules while also incorporating Ant Design using Less styles for a smaller build size? I have been struggling to enable both CSS modules and the Less loader simultaneously. The examples p ...

Issues with the input element: Transition, border-color, and :focus functionalities are not functioning as

Currently, I am in the process of developing a search box similar to Google's for an e-commerce platform. Unfortunately, I am facing difficulties with the border-color, transition, and :focus properties. To further elaborate on the issues: border-col ...

A collection of items that mysteriously affix themselves to the top of the page as

Unique Context In my webpage, I have a specific div element with the CSS property of overflow: auto. Within this scrolling div, there is structured content like this: <h3>Group A</h3> <ul> <li>Item 1</li> <li>I ...

Adjusting the size and appearance of ngDialog

Currently using the Ionic framework and encountering an issue with popup sizing. $scope.dialog = ngDialog.open( { template: 'popup.html' , className: 'ngdialog-theme-default' , controller: 'MyCtrl' ); By default, the popup o ...

How to align a list item to the right using CSS

I'm having trouble aligning items in a list to the right and adjusting their width based on content length. Unfortunately, I haven't been able to make it work despite multiple attempts. Take a look at my code snippet below: .messages { over ...

The Bootstrap dropdown menu is cut off and not fully visible on mobile devices

I am experiencing an issue with the mobile version of a website where the dropdown menu is not fully visible due to the position of a specific menu item and the number of items in the dropdown. https://i.sstatic.net/HmXeq.png The image below shows how I ...

The icons from Materialize CSS are not displaying properly in the Safari web browser

It has come to my attention that the icons in Materialized CSS are not displaying properly in Safari (v5.1.7). Despite conducting thorough research on this issue, I have been unable to find any documentation regarding browser compatibility with Safari. Can ...

Preserving proportions in CSS using both width and height measurements

My objective is to set an aspect ratio (for example, 4:3) for a DIV and all its children with the styles WIDTH:100% and HEIGHT:100%. Initially, this method works well by setting the parent's WIDTH:100% and then adding PADDING-BOTTOM: 75%; // (3/4)*1 ...

Arranging pictures in both vertical and horizontal alignment (CSS)

I've been attempting various methods to solve this issue, but none have proven successful. My goal is quite simple: align images both horizontally and vertically in a manner that fills empty spaces with photos. Similar to the layout here: The challe ...