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

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

Máté Végh
Máté Végh
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?

Máté Végh
Máté Végh
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!