Looking to transform a SCSS associative array into variables and classes?

I need guidance on handling syntax for unquoting, declaring variables, and concatenating strings within a for loop. My objective is to utilize a map to generate color variables and corresponding classes for styling elements with font-color and background-color properties.

$color-collection: ('white': '#FFFFFF', 'black': '#000000', 'goldenrod': '#F59600');
@for $i from 0 to length($color-collection) {


//generate individual variable assignments for each color, e.g. $white: #FFFFFF;
//create CSS classes based on variable names with specified styles, e.g. .bg-color-white {background-color: $white;}

}

Answer №1

To start, it's important to utilize @each rather than @for.

Furthermore, within the loop itself, you can directly access the two necessary variables - one representing the key and the other the value.

In addition, we can employ Sass interpolation to incorporate the variable into a class name and unquote the value in a selector. Another option is using unqote($color-value), or simply inputting color values in the array without quotation marks.

Check out the example below:

$color-collection: ('white': '#FFFFFF', 'black': '#000000', 'goldenrod': '#F59600');
@each $color-name, $color-value in $color-collection {

  .bg-color-#{$color-name} {
    background-color: #{$color-value};
  }
//define a variable for each color name, such as $white: #FFFFFF;
//establish a class for each variable name with a style, like .bg-color-white {background-color: $white;}

}

If you are using libsass, here is some code that accommodates older Sass syntax:

$color-collection: ('white' '#FFFFFF'), ('black' '#000000'), ('goldenrod' '#F59600');
@each $color in $color-collection {
  $color-name: nth($color, 1);
  $color-value: nth($color, 2);
  .bg-color-#{$color-name} {
    background-color: #{$color-value};
  }
}

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

Tips for arranging two columns side by side with the .each method in Rails

How can I modify the code to display two columns of data in a table row using the '.each' method? Currently, the code only displays one column per row. <table> <% @lease.apartment.roommates.each do |roommate| %> <tr> ...

Tips for displaying active status on dropdown values when clicked with Angular8

My Angular8 application has a navigation bar with 3 dropdown options: Agent, Prospect, and Competitor. When I click on Agents, it does not get highlighted. I want any dropdown option I select to remain highlighted unless I manually choose another option. ...

Issue resolved: Mysterious fix found for background images not displaying in NextJS React components

I am having trouble displaying a background image on an element in NextJs using Typescript and Tailwind. I do not believe it is a TypeScript issue since I am not receiving any errors or IntelliSense warnings. Below is the code I am working with: var classn ...

Customize the Address Bar with Google Extension

I recently developed a unique Chrome extension that modifies the appearance of the new tab page by incorporating my site's index.html. However, I am facing an issue where the address bar displays https://example.com/index.html/. Is there a way to have ...

What steps can I take to fix the issues on Safari that are being caused by three js on my website?

For my friend's birthday, I decided to surprise him by creating a web page for his online radio station. Being new to JavaScript, it was a great learning opportunity for me. After much experimenting, I finally got the page to work smoothly on desktop ...

Strategies for managing recurring identical HTML content

Within my website, I find myself repeating similar content frequently on the same page. For example: <a href="link1"><img src="path1" alt="name1"></a> <a href="link2"><img src="path2" alt="name2"></a> <a href="link3" ...

Can you explain the concept of Single Page Application (SPA) in simple terms?

Let me explain my understanding of Single Page Applications (SPAs). An SPA is an application where the user experiences seamless navigation as if they are staying on the same page even when triggering events. Instead of reloading the entire page, SPAs use ...

What is the best method for efficiently wrapping contents within a div container?

On the website cjshayward.com/index_new.html, there is a div wrapped around the content of the body, which is approximately 1000 pixels wide. In Chrome and Firefox, the wrapper works perfectly for the top section of about 100 pixels. Further down the page, ...

Playing back an Audio object using JavaScript

I'm facing an issue with replaying an audio sound every time the game starts in my Cocos2d javascript code. The sound plays correctly the first time, but subsequent attempts to play it result in no sound being heard. Below is my code snippet: var my ...

What are some ways to improve the precision of longitude and latitude data?

I have encountered an issue while using MapBox. When a user clicks on the map, I am receiving longitude and latitude coordinates that are not very accurate. For example, when I clicked in the United Kingdom, the values pointed me to the middle of the sea a ...

Xpath merging HTML elements or nodes

<tr id="computer-report-tbl-row-0" ng-repeat="item in table.data.items" class="ng-scope" style=""> <td id="column-name-0" class="table-cell-md" sc-ellipsis-title="" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;"> ...

Dividing HTML and JavaScript using Vue

Is there a way to separate HTML from data/methods in VueJS to avoid having one long file with both? I tried moving the contents of my <script> section into a new file called "methods.js" and importing it using: <script src="methods.js"> Note ...

Using HTML5 date input on an android browser

As I develop a web page catering to Android users, I have incorporated a date form control for users to input dates. However, I have noticed that on most devices, the native date selector does not pop up. Interestingly, on certain Samsung devices like the ...

What's causing jQuery to make the entire page malfunction?

I am experiencing an issue where a function is not recognized as being in scope when I have a reference to jQuery defined. Oddly enough, if I comment out that reference, the function call works just fine. I have other pages set up the same way with jQuer ...

Is there a way to break on a hyphen instead of breaking on the last letter?

Is there a way to make the browser wrap on hyphens instead of cutting off the last letter on a line? I haven't noticed any difference between word-break and word-wrap. Check out this live example: <div style="word-break:break-normal;font-size:14 ...

Struggling to make `align-items="baseline"` function properly

I'm encountering an issue with alignment in my sandbox project. Specifically, I want the bottom of texts with different font sizes to be on the same y axis but I can't seem to figure out how to make it happen. Check out this picture to see exact ...

The navigation bar initially fills the width of the screen, but does not adjust to match the width of the table when scrolling

I've spent the last 2 hours playing around with CSS, using width:100% and width:100vw, but nothing seems to be working. Currently, the navigation bar fits perfectly across the screen on desktop browsers, so there doesn't seem to be an issue ther ...

The front end button stubbornly refuses to comply and redirect to another page

I am having trouble getting my button element to redirect my page to another page. I have tried using an onclick function inside the button tag with window.location.href, as well as creating a separate function for the redirect, but nothing seems to be wor ...

Unable to correctly declare and display UTF-8 character within JSON syntax

In my JSON object, there is an attribute that contains a unique special character - I've attempted to save the string in encoded UTF-8 as "\xF0\x9F\x94\x94" or tried displaying it using its HEX value - String.fromCharCode(0x1F514 ...

Adjust the size of the pagination for subsequent pages in posts

Could someone please assist me with changing the size of post pagination that includes the code --nextpage--? I've attempted the following code: .td-post-content .page-nav a { font-size: 16px; } Unfortunately, it isn't working as expected. App ...