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 Improve Your Workflow with Sass Use the Ampersand to Reference Parent Selectors

I'm missing the benefit of &

What's the benefit of the &?

In the example below, why not just say .intro p (as opposed to .intro &)? It seems like it's an unnecessary layer of complexity.

p {
  margin-bottom: 1.25em;
  .intro & {
    font-size: 1.2em;
  }
}

1 Answer

Brice Roberts
Brice Roberts
22,415 Points

The ampersand operator is a very powerful tool for nesting classes in CSS. In this example, it doesn't really save you time, because you could have just as easily written

p .intro {
  margin-bottom: 1.25em;
  font-size: 1.2em;
}

In simple code, advanced tricks like this don't really seem logical, but for example...

.button {
  &:visited { }
  &:hover { }
  &:active { }
}

compiles to

.button:visited { }
.button:hover { }
.button:active { }

which is not the same as

.button .hover {
}

check out the link here for more visual and in depth explanation. It really is a helpful tool to save you time.

https://css-tricks.com/the-sass-ampersand/

Thanks for the link. I appreciate it