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 trialGerald Susanteo
Front End Web Development Techdegree Student 7,117 Pointsusing placeholders
so what are placeholders for? I don't really get it and is still confused also when writing for @extend is it better to use placeholders or normal . for the selectors.
1 Answer
Rabin Gharti Magar
Front End Web Development Techdegree Graduate 20,928 PointsHey Gerald Susanteo,
I will try my best to break this down into small sections so you can fully understand the terms.
@extend directive
allows classes
to share the set of properties with one another. This directive
can be useful if you have similarly styled elements that only differ in some small details. This helps to keep your Sass code very DRY.
For example:
SASS:
.button{
border: none;
padding: 10px
cursor: pointer;
}
.button-submit {
@extend .button;
background-color: green;
}
CSS Output:
.button, .button-submit {
border: none;
padding: 10px
cursor: pointer;
}
.button-submit {
background-color: green;
}
As you can see in the above example, .button-submit
will inherit all the CSS properties from the .button
class through @extend directive
. But .button
class has appeared on CSS output and to make it not appear in a final version of CSS we use placeholder
.
placeholder
is a special kind of selector and it acts a lot like CSS selector and it starts with a %
that won't appear in the CSS output unless @extend
them. I will show you how this works by using the same above example.
SASS:
%button{
border: none;
padding: 10px
cursor: pointer;
}
.button-submit {
@extend %button;
background-color: green;
}
CSS Output:
.button-submit {
border: none;
padding: 10px
cursor: pointer;
}
.button-submit {
background-color: green;
}
I usually prefer to extend a placeholder selector instead of a class selector. In this way, your final version of CSS
won't look as messy. Again, it's your personal preference.
I will share some resources that might be helpful:
- https://csswizardry.com/2014/11/when-to-use-extend-when-to-use-a-mixin/
- https://blog.teamtreehouse.com/extending-placeholder-selectors-with-sass
Hope this helps!
Happy Coding!
Gerald Susanteo
Front End Web Development Techdegree Student 7,117 PointsGerald Susanteo
Front End Web Development Techdegree Student 7,117 PointsThank you so much, Rabin!