by ianstormtaylor · 12y ago · 0 comments
---
For nesting, the problem with nesting is that is messed with specificity way too much. Since adding an extra class inside a nested block is trivial, people start adding more and more. But even just 2 classes is already too much in my opinion. Ideally each component would have classes on each of its inner elements, so that the component-level styles always have a specificity of 0010:
<div class="textfield">
<label class="textfield-label"></label>
<input class="textfield-input">
</div>
If you have a setup like that, you never need to nest because your CSS ends up looking like this: .textfield {}
.textfield-label {}
.textfield-input {}
And that way all of your selectors are minimally invasive in terms of specificity. The problem with nesting is that you end up with selectors that have a specificity of 0020, which means anyone implements those components needs to be sure to achieve that level of specificity or greater to override them.But honestly, when working with small components there just isn't that much typing to justify the possible downsides. Sure I have to type out the extra selectors in the beginning a few more times, but its worth it to do without the unintended consequences of the magic.
The one case where nesting really does come in handy is for page-specific or app-specific stylesheet files. Places where you are adding the "glue" CSS that should always be namespaced to avoid collisions.
---
For mixins, I feel like they only seem useful if the components you're using are broken into small enough pieces to begin with. For example, if you want a complex gradient system on your buttons, put that inside your button styles and then don't touch it from anywhere else. If you're constantly needing to use a mixin to re-define those styles, something else is wrong. Because realistically how many different color variations of those buttons do you need to make? It should be somewhat limited, and they should be able to exist in a single file without issue.
Where things really get screwed is if you aren't versioning your mixins (which most people aren't doing). At that point, you're just tightly coupling changes to that mixins across your entire codebase. If you want to change the way the buttons implement the gradient, with the mixin system you go change the mixin, but now you need to visit every other component that uses that mixin and double-check that you didn't break anything.
Instead, if you didn't use a mixin and the component just contained its gradient logic (slightly more verbose maybe, but way more maintainable) then you don't have that problem.
The other solution is to strictly version your mixins using something like Component and maintain your mixins in a separate repository on GitHub that gets versioned, then that also works because each other component can peg the version of the mixin it is using.
---
My big takeaway from CSS though is that none of the solutions for it are nice right now. And most of these problems should disappear (I think) with proper native extension.
.textfield {
&-label {
...
}
&-input {
...
}
}http://csswizardry.com/2013/01/mindbemding-getting-your-head...
Basically it's anti-nesting and proposes being very specific with regards to class naming:
.block{} .block__element{} .block--modifier{}
I've experimented a bit with this lately but haven't come to a conclusion yet myself. Yes, it does decrease the chances of accidently styling the wrong elements, but it also makes the html more verbose and can in some cases make it more difficult to implement the website w/jQuery (because of the different classes in use, ie. instead of a .selected class you might have .selected__orange, .selected__apple so to switch the selected item you can't just do an .removeClass('selected')/.addClass('selected)
.textfield {}
.textfield input {}
.textfield label {}
.textfield {
label {}
input {}
}
etc. <fieldset class="TextField">
<label class="TextField-label"></label>
<input class="TextField-control">
<label class="TextField-legend"></label>
</fieldset>
Just makes it future-proof for when you want to add other elements that might collide since the number of element types is small.Generally I try to never select on non-class selectors in component-level CSS. Component-level styles should occupy the middle range of class selectors. And then the user should have control over the element selectors (base styles) and id selectors (page-specific styles) so that they can both cascade and override when they need to.
// Sass
[data-UI-component="textfield"] {
& > label:only-of-type,
& > label:first-of-type {}
& > input {}
& > label:last-of-type {}
}
/* css output */
[data-UI-component="textfield"] {}
[data-UI-component="textfield"] > label:only-of-type,
[data-UI-component="textfield"] > label:first-of-type {}
[data-UI-component="textfield"] > input {}
[data-UI-component="textfield"] > label:last-of-type {}
That way, you don’t mess-up your template/html with classitis. I like to reserve the use of classes for js logic (states) and for semantics (SEO, schema.org, …), thus using the `data-x` attribute to create and select components.Sure your component’s html/handlebars/whatever template must be kept in sync with its css, and adding elements could totally mess-up things. But I think that’s okay, since changing a component’s template should require a revisit of its stylesheet. What do you think?
Generally trying to tie it to order in the DOM just becomes very fragile, when really a classname is actually more semantic in terms of the meaning sticking with the element and being able to use it anywhere.
I think trying to separate JS hooks from CSS hooks is an anti-pattern actually. Like having `js-` prefixed classes. Because realistically you aren't changing the class names enough for it to be a problem. And if you do change the class names there's a good chance you'd want to change the `js-` prefixed one too and eliminate any benefit they were giving you in the first place.
You might be able to get it to work for simple components though. But I'd be weary since there are only so many HTML different elements, and the special pseudo-selectors can only get you so far.
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.)
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.
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.