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 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: