Interpolating Sass variables within every loop iteration

I am attempting to incorporate a variable within an each loop, but it does not appear to be functioning correctly.

$font-size-40: .5rem;
$font-size-50: .625rem;
$font-size-60: .75rem;
$fonts: 40 50 60;

@each $font in $fonts {
    .font-size-#{$font} {
        font-size: $font-size-#{$font};
    }
}

Answer №1

$texts: (
    'text-size-20' : 20,
    'text-size-30' : 30,
    'text-size-40' : 40
);

@each $label, $size in $texts {
    .#{$label} {
        text-size: #{$size};
    }
}

Answer №2

I implemented it this way and it's functioning perfectly using the key-value approach.

$font-size-40: .5rem;
$font-size-50: .625rem;
$font-size-60: .75rem;

$fonts: (
    '40': $font-size-40,
    '50': $font-size-50,
    '60': $font-size-60,
);

@each $font, $prop in $fonts {
    .font-size-#{$font} {
        font-size: #{$prop};
    }
}

However, I am curious as to why we are unable to use it like:- $font-size-#{$font}

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

Enhance tables by incorporating sorting arrows onto <th> elements, reminiscent of table sorter

How can I add double arrows (up and down) to my table similar to the functionality in the tablesorter plugin? When I use this fiddle, none of the arrows appear despite working fine on my original table. I attempted the following: $("table th").addClass( ...

Unusual behavior observed in Google Font when using display=swap

After attempting to integrate a Google Font API into my website by adding the following code to my HTML: <link href="https://fonts.googleapis.com/css?family=Playfair+Display|Source+Serif+Pro|Suwannaphum&display=swap" rel="stylesheet"> I encount ...

What is the process for transferring sigmajs gephi export files onto a server? [The files consist of javascript, css, and index.html that need to be directed to]

Struggling to upload my sigmajs export from gephi onto a website, particularly my GitHub pages. I'm encountering difficulties in getting it uploaded correctly. Could someone provide guidance on the process? I've already uploaded the folder and a ...

CSS Flexbox with 3 columns and a grid layout of 4 by 4, with one large item included

I am currently exploring how to implement this layout using Flexbox. So far, I have only managed to come up with the following code snippet: https://i.sstatic.net/mKVlu.jpg Is there a way to achieve this purely through CSS without altering the HTML struc ...

Use jQuery to assign a fresh class to the navigation panel

I'm currently making changes to a WordPress site locally. I'm utilizing jquery.dropotron from ajlkn for the menu, which is working well. However, I'm facing an issue when trying to assign a new class to the 7th child of the navPanel. Somethi ...

enhancing the appearance of the initial sentence in the closing passage through CSS styling

Is there a way to make only the first sentence in the final paragraph bold using CSS? Specifically, I would like to add extra padding to the top of this last paragraph so that it wraps around an image. However, when I increase the bottom padding of the flo ...

Ways to align a flex child in the middle depending on the width of the

I'm currently working on centering the navigation menu horizontally by aligning it with the width of the header tags using tailwind CSS. However, I've run into an issue where it only centers based on the navigation tags' width. <!DOCTYPE ...

Creating a personalized, perfectly centered container with a specific width in Twitter Bootstrap

I've been struggling with a unique challenge for the past day without success. I can't seem to figure out where I am making a mistake. My goal is to design a fixed container layout using Bootstrap, which is precisely 33em wide and horizontally c ...

What is the best way to place a button within a line of text

I am trying to achieve a layout similar to this: -------------------button------------------ I can add text inside the line, but it doesn't look good when I add a button. Any suggestions on how I can improve this? ...

Managing both clicking and hovering events on a single element, ensuring that the popup modal remains open as long as it is being hovered over

After successfully implementing click and hover functionality on an element, I was able to position the popup relative to the mouse pointer based on a previous solution. However, I am now facing an issue where I want the popup modal to be fixed in a specif ...

Displaying or hiding table columns in Vue.js Element-UI el-table based on screen width: a step-by-step guide

Here is my code snippet for generating a table: <el-table :data="tableData" empty-text="No data to display :( " v-if="window.width > 855"> <el-table-column v-for="column in tableColumnsMd" :key=&q ...

Scroll within the inner container

I am currently working on creating panels, each with a header and content. My goal is to allow scrolling only within the content area. Here is the CSS I have been using: .content-div{ float:left; height: 400px; overflow-x:hidden; overflow-y:hidden; ...

What is the best way to stack columns on small screens in a single row?

While utilizing the default bootstrap col classes, I noticed that when I reduce the grid to mobile size, the columns span the entire width of the row. This results in a long vertical list of items on mobile devices, even though there is sufficient space on ...

Making Angular Treeview nodes more visually appealing with a bell symbol

I am faced with the task of enhancing a tree view display from (http://alexsuleap.github.io/) by updating a node with a bell icon only after other relevant data on the page has been loaded. The specific icon that needs to be displayed is "glyphicon glyphic ...

Surprising "ms" unit spotted in the unit-allowed-list while working with react scss

Encountered an error saying Unexpected unit "ms" unit-allowed-list while attempting to commit the code. The specific errors are: 31:24 ✖ Unexpected unit "ms" unit-allowed-list 42:25 ✖ Unexpected unit "ms" unit-a ...

Arranging my table

I've been trying to adjust the position of a table in my project that contains an image. Currently, it is aligned to the left and I want to move it to the right. Here is the code for my table: <TD> <IMG SRC='http://farm8.staticflickr.co ...

Utilizing margins in CSS for various div elements

Update: I have included Javascript and Masonry tags in my project. Despite having modules of the same size, I am exploring how masonry can assist me. However, I find it a bit puzzling at the moment as I am not aiming to align elements of different sizes. I ...

Ways to conceal HTML tags within a text box

Currently, I am utilizing PHP to extract content from a text file and display it in a textbox. However, I am interested in finding a way to conceal the HTML tags within the textbox (as shown in the image) without deleting them, making them invisible to use ...

How can one get rid of a sudden strong beginning?

Recently, I delved into the world of CSS animation and encountered a frustrating issue. I've tried numerous methods and workarounds to achieve a smoothly looping animation without interruptions. Despite my efforts, I have not been able to replicate th ...

Is there a way to activate a click event when I click on a button that is located outside of the dialog element?

In my Vue 3 app, I am using the native HTML dialog element as a modal. I have managed to position a button outside of the modal with absolute positioning. However, I am facing an issue where I cannot trigger a click event (displayNextModal) when clicking ...