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 Improve Your Workflow with Sass Extend the Properties of Selectors

Sean Daniel
Sean Daniel
4,811 Points

Could I have created a mixin instead of @extend?

ORIGINAL SYNTAX
.btn {
  color: $white;
  display: inline-block;
  font-weight: bold;
  text-decoration: none;
  text-transform: uppercase;
  padding: 0.75em 1.5em;
  border-radius: 0.35em;
}

.btn-callout {
  @extend .btn
  font-size: 1.1em;
  background-color: $color-secondary;
}

.btn-info {
  @extend .btn
  font-size: 0.85em;
  background-color: $color-primary;  
  margin-top: auto;
}

WITH A MIXIN INSTEAD

mixins --------------->

@mixin button {
  color: $white;
  display: inline-block;
  font-weight: bold;
  text-decoration: none;
  text-transform: uppercase;
  padding: 0.75em 1.5em;
  border-radius: 0.35em;
}

components -------------------->
.btn {
 @include button;
}

.btn-callout {
  @include button;
  font-size: 1.1em;
  background-color: $color-secondary;
}

.btn-info {
  @include button;
  font-size: 0.85em;
  background-color: $color-primary;  
  margin-top: auto;
}

3 Answers

Yes, you could have. There is an important difference, though, in the way each option will be output into CSS. Using @mixin, all of those declarations will be repeatedly written out, like so:

.btn {
  color: #fff;
  display: inline-block;
  font-weight: bold;
  text-decoration: none;
  text-transform: uppercase;
  padding: 0.75em 1.5em;
  border-radius: 0.35em;
}

.btn-callout {
  color: #fff;
  display: inline-block;
  font-weight: bold;
  text-decoration: none;
  text-transform: uppercase;
  padding: 0.75em 1.5em;
  border-radius: 0.35em;
  font-size: 1.1em;
  background-color: pink;
}

.btn-info {
  color: #fff;
  display: inline-block;
  font-weight: bold;
  text-decoration: none;
  text-transform: uppercase;
  padding: 0.75em 1.5em;
  border-radius: 0.35em;
  font-size: 0.85em;
  background-color: red;
  margin-top: auto;
}

Using @extend, however, will lump whatever you've added the extend to into the initial rule block, like so:

.btn,
.btn-callout,
.btn-info {
  color: #fff;
  display: inline-block;
  font-weight: bold;
  text-decoration: none;
  text-transform: uppercase;
  padding: 0.75em 1.5em;
  border-radius: 0.35em;
}

.btn-callout {
  font-size: 1.1em;
  background-color: pink;
}

.btn-info {
  font-size: 0.85em;
  background-color: red;
  margin-top: auto;
}

So it's a more verbose to do this as a mixin. But, one major limitation with extends is that you can't nest them, and they can also get kinda unpredictable, so I rarely ever use extends. But, up to you. Both do what you want.

Good point Eric, thanks.