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 Add Reusable Logic to Your Sass Loop Through Lists with @each

Brady Gorman
seal-mask
.a{fill-rule:evenodd;}techdegree
Brady Gorman
Front End Web Development Techdegree Student 7,738 Points

Where is $teacher defined?

So I understand how $teachers was defined as a list but how does it know that each list entry within $teachers is a $teacher?

$teachers: (
'andrew', 'alena','joel','nick');

@each $teacher in $teachers {
    .teacher-#{$teacher} {
        background-image: url('img/#{teacher}.jpg');
}
}

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hi @Brady Gorman 👋

Great question! In an @each loop you access each of the items using a variable. You can name this variable whatever you like but it's common to give it a name that represents the data you're working with.

For example, the snippet below will result in the same output but the variable name is less deceptive.

@each $x in $teachers {
    .teacher-#{$x}{
        background-image: url('img/#{$x}.jpg');
    }
}

Hope this answers your question 😄