Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8681611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:33:18+00:00 2026-06-12T21:33:18+00:00

I am trying to write a loop that will cycle through colors and condense

  • 0

I am trying to write a loop that will cycle through colors and condense the amount of code in my scss file. Here is a simple example of what I have:

$color1: blue;
$color2: red;
$color3: white;
$color4: black;

.color1-bg { background-color: $color1; }
.color2-bg { background-color: $color2; }

.color1-border { border-color: $color1; }
.color2-border { border-color: $color2; }

And so on.

I am looking for a way to write a loop so that when I use the variable to generate the class, it has the name of the variable rather than its value:

@each $color in $colour1, $colour2, $colour3, $colour4 {
    .#{$color}-bg { background-color: $color; }
    .#{$color}-border { border-color: $color; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:33:21+00:00Added an answer on June 12, 2026 at 9:33 pm

    You can’t access the name of the variable directly, you’d have to store it as an additional value.

    Sass 3.2 and older (list of lists)

    Sass doesn’t have mappings yet, so the next best thing is a list of lists.

    $color1: ('color1', blue);
    $color2: ('color2', red);
    $color3: ('color3', white);
    $color4: ('color4', black);
    
    @each $color in $color1, $color2, $color3, $color4 {
        $name: nth($color, 1);
        $value: nth($color, 2);
        .#{$name}-bg { background-color: $value; }
        .#{$name}-border { border-color: $value; }
    }
    

    Sass 3.3 and newer (mappings)

    Starting with 3.3, you have access to mappings. It’s functionally the same as the list of lists, but syntactically more concise.

    $colors:
        ( 'color1': blue
        , 'color2': red
        , 'color3': white
        , 'color4': black
        );
    
    @each $name, $value in $colors {
        .#{$name}-bg { background-color: $value; }
        .#{$name}-border { border-color: $value; }
    }
    

    Alternately

    Or if your color names are really “color1”, “color2”, etc., you can also just construct the name on the fly rather than specifying them explicitly:

    $color1: blue;
    $color2: red;
    $color3: white;
    $color4: black;
    
    $num: 0;
    @each $color in ($color1, $color2, $color3, $color4) {
        $num: $num + 1;
        .color#{$num}-bg { background-color: $color; }
        .color#{$num}-border { border-color: $color; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write code that will loop through all cells in a
I'm trying to write a simple batch that will loop through every line in
I am trying to write some code that will loop through a list and
Trying to write a batch file that will loop through all the files in
I'm trying to write a unit test that will loop through all action methods
I am trying to write some code that will open a file, read its
I am trying to write code that will cycle between rendering an animation and
I'm trying to write a program that will loop through cells of a specific
I am currently trying to write a script that will loop through multiple directories.
Trying to write a couple of functions that will encrypt or decrypt a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.