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 trialSean Daniel
4,811 PointsCould 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
Eric Butler
33,512 PointsYes, 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.
Piotr Manczak
Front End Web Development Techdegree Graduate 29,277 PointsGood point Eric, thanks.
Lisa Walters
Front End Web Development Techdegree Graduate 15,255 PointsA year later...but SOOOO helpful! Thank you!!!
Sean Daniel
4,811 PointsSean Daniel
4,811 PointsThanks Eric.