Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

Gareth Edwards
Gareth Edwards
7,243 Points

Code challenge: creating loops with @for and @each.

The challenge states :

Use an @each loop to set the variable "$color" to the color red, then blue, and finally green. Use the loop to create three classes: ".red_box", ".blue_box", and ".green_box". The background color for each class should be set to the corresponding color.

@each $color in red, blue, green {
    .color.#{$color} {
    background-color: $color;
  }
}

I believe I'm doing somethin wrong by passing the background-color the value of the variable $color and I beleive I need to place ._box somewhere but I'm a little confused with this one.

The following error message is received: "Bummer! There should be a .red_box class with a red background. Did you set up the loop correctly?"

Any help would be greatly appreciated.

Many thanks. Gareth

2 Answers

Jorge Cruz
Jorge Cruz
19,305 Points

You didn't interpolate the class selector correctly.

@each $color in red, blue, green{
    .#{$color}_box{
    background-color:$color;
  }
}
Gareth Edwards
Gareth Edwards
7,243 Points

Ah yes, thanks for your help.