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

Lucas Santos
Lucas Santos
19,315 Points

SASS interpolation question

So in this example:

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

Why is it that in the width: $i * 100px; it is just $i and not #{$i}

also the other way around why can't we just use .item_$i instead of .item_#{$i}

1 Answer

Mate Vegh
Mate Vegh
25,607 Points

Sass variables can only store values. For dynamic selectors like in this case, you need to use interpolation syntax, otherwise it will output an error.

For instance:

$selector: item;
$value: red;

// Right:
.#{$selector} {
  background: $value;
}

// Wrong:
.$selector {
  background: $value;
}
Lucas Santos
Lucas Santos
19,315 Points

So interpolation is specifically for selectors rite.. other than that you can use an argument with out interpolation to calculate values like the width correct?

Mate Vegh
Mate Vegh
25,607 Points

You could also store a property into a variable, than use it with interpolation. For instance:

$bg: background;

.item {
  #{$bg}: red;
}

But yes, you are right, interpolation is used for selectors most of the time, and regular variables are used for values.

Lucas Santos
Lucas Santos
19,315 Points

I see thanks Matt appreciate it!