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

Cameron Bourke
Cameron Bourke
10,198 Points

@mixin or @extend, that is the question.

So I just discovered how awesome Sass is, although I'm not able to do anything fancy with it yet. However, the other day it occurred to me that using Sass allows designers to implement the "single responsibility" principle, http://drewbarontini.com/articles/single-responsibility/, into our Sass without running the risk of our html suffering from "classitis".

Basically, we can do all the single responsibility stuff in our scss and only have to reference one class name to that element in our html. To test this I created a code pen, until I soon realised this could be achieved by using either a mixin or an extend. My question to you's that are interested in this problem is, is it better to use a mixin or extend for this? The only down side I have for both is that a mixin goes against the dry principle when the sass is complied to css, but on the other hand using with an extend is having a possible 20 or 30 classes having off one selector could affect performance maybe?

Anyways, heres the two pens.

@mixin http://codepen.io/cambourke/pen/Dykif

@extend http://codepen.io/cambourke/pen/KCsty

Thanks heaps in advance for those who take the time to help me out!

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Cameron,

Having too many selectors grouped together is a very common problem issue in Sass not because of extends but because most developers don't follow the inception rule which is more of a guide but it states that you should never have selectors nested more than 3 levels deep, some developers even push that further and say never go more than 2 levels deep but 3 is nice and clean too.

Now to the question at hand and what a question it is, in my experience either works given the circumstance in which you require that CSS for. Let's go with an analogy.

Say you have 10 selectors that require :before and :after pseudo elements, the best way to get the base CSS going in most peoples minds would be to use a placeholder and extend it to all the children that need it but that's wrong as you end up with selector garbage, a much better option is to create a class and assign the base pseudo styles to it and then assign the class to the parent in the HTML that needs it; this not only removes the selector garbage but gives clarity to the CSS that's generated. From there all you need to do is assign the specific CSS to the children.

Now you might have read that and thought what about single responsibility principle, well I'm a firm believer that CSS should hold the master key, both your HTML and CSS need to share the responsibility as it will result in cleaner code and remove a lot of CSS that the Sass compiler generates.

mixins vs extend?

Extends are great if you are using for defining modular parts of a website that can be re-used throughout your CSS; without that many selectors bound to them which makes them extremely useful in that regard but as I said above you you can easily end up with selector garbage.

Mixins are perfect for the times when you have blocks of code that changes based on the given input, browser prefixed properties, media queries etc.

At the end of the day both have their uses and neither is better than the other but they have their own jobs to do, from my perspective it comes down to a project level that determines which you need to use as every project is unique and more often then not carries different requirements which results in a far different code base.

Cameron Bourke
Cameron Bourke
10,198 Points

Thanks for the comprehensive answer man, definitely cleared a few things up for me!

I've revised that previous pen, I think this result is a lot cleaner like you said, even though there is more classes in the HTML. http://codepen.io/cambourke/pen/wldJC?editors=110