3 ms·
Regarding nesting, I've always followed the use-nesting-instead-of-prefixes model, so I might use ".big-button .title" and ".main-article .title" classes in a p
by crazygringo 13y ago
Regarding nesting, I've always followed the use-nesting-instead-of-prefixes model, so I might use ".big-button .title" and ".main-article .title" classes in a project, but because each title is nested in a different component or whatever, it's easier to read, less redundant, and more "semantic". A project might easily have 50 different ".title" classes, but you know you don't have to worry about collisions. But that's really just a matter of formatting taste, ultimately.
But you say:
> If you're constantly needing to use a mixin to re-define those styles, something else is wrong
> realistically how many different color variations of those buttons do you need to make?
> if you didn't use a mixin... (slightly more verbose maybe, but way more maintainable)
That's an awful lot of presumption, and it's just totally wrong, at least in my case. To use my gradient example: I probably apply this model to 50 or 100 types of elements across a site that's not even that big -- to buttons, to backgrounds, to panels, etc. because the designer likes subtle background variations -- flat design, but not too flat. So nothing is wrong, there are tons of variations, and using mixins is the just following the "dont-repeat-yourself" model.
Obviously, you could call this a model of "tight coupling", but that's a good thing, not bad -- you define a single model of mixins for your site, just like you have a single global CSS reset. It just makes up for certain common deficiencies in CSS, and helps in the creation of a common visual identity of your site where CSS classes themselves just aren't flexible enough. (There's another whole reason to use mixins used exclusively within specific components, but you seem to agree on that above.)
The "slightly more verbose" alternative you propose would actually be incredibly verbose (maybe 7 CSS properties per element instead of 1, if used on 100 types of elements, meaning 700 lines of code instead of 100), because nothing's being reused. If a new browser comes out which requires a slightly different tweak, you would need to manually fix it in 100 places instead of 1, which is horrible. Mixins simply allow you not to repeat yourself, which should be the goal with all good programming. CSS without mixins is kind of like programming in Python without being able to define functions.
(And asking people to version their mixins makes as much sense as versioning functions in a C++ project because changing a function changes its behavior. I.e., not much with rare exceptions. If you change a mixin you've been using for a while, then it goes without saying you inspect where it's used in the site to make sure it doesn't break things.)
- peterhunt 13y ago> A project might easily have 50 different ".title" classes, but you know you don't have to worry about collisions. Not if your UI is of sufficient complexity that you're composing components of other components. Then not only do you have lots of collisions, you have a huge specificity war too. That's why prefixing and single-class-name selectors are generally preferred.
- crazygringo 13y agoOf course. Nesting is just a tool that helps organize your classes. I've found the most maintainable code comes from a well-defined combination of nesting and prefixes (but I tend to use nesting as a default, and prefixes only when necessary) -- they both have their strengths and weaknesses. But CSS precedence is always a problem, if it's not specificity then it's ordering instead, the problem doesn't go away by using prefixes. But using the order that rules are defined in can often be more difficult to track/maintain than using specificity to do the same. Good commenting practice is key in both cases. Nesting with mixins certainly isn't always the answer, but it often provides a much cleaner, more maintainable, more intuitive approach to resolving occasional "specificity wars" than not using them.