Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS Sass Basics (retired) Advanced Sass Concepts Creating Loops with @for and @each

The class print right color but cannot pass

I tried my code in sassMeister and it works fine but in Challenge it says, "There should be a class red_box with background of red and my code do the same but it throws me error"

@each $color in red,blue,green{
  .#{$color}_box{
     background-color:url("#{$color}");
  }
}
index.html
<!DOCTYPE html>
<html>
<head>
  <title>Sass Basics - Code Challenge</title>
  <link rel="stylesheet" type="text/css" href="page-style.css">
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <div class="item_1 red_box"></div>
  <div class="item_2 blue_box"></div>
  <div class="item_3 green_box"></div>
</body>
</html>
style.scss
/* Write your SCSS code below. */

@for $i from 1 through 3{
     .item_#{$i}{
       width: $i * 100px;
     }
}

@each $color in red,blue,green{
  .#{$color}_box{
     background-color:url("#{$color}");
  }
}

1 Answer

You are setting the background to a weird url thing. Remember you can use color values as backgrounds!

To fix that, you can just reference to the color value using the name of the variable:

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

I hope this helps :grin:

Happy coding! :tada:

:dizzy: ~Alex :dizzy: