Show HN: ECSS – Simple rules for efficient CSS (ecss.info)
A list of CSS authoring rules with examples and a Stylelint config accessible from the top of the page.
I've come to these through 20 years of experience and a willingness to make vanilla CSS a better alternative to frameworks.
I encourage you all to comment on the rules themselves and the Stylelint Config for ECSS. Here's the link for faster access (I still suggest at least zipping through the rules beforehand).
https://www.npmjs.com/package/@efficientcss/stylelint-config...
20 comments
[ 3.3 ms ] story [ 55.4 ms ] threadDidn't think about git pre-commit hooks to be honest. But I'll look into it. If you have any ideas/suggestions, they are very welcome!
CSS performance is underrated as well.
> /* Do */
> [id="main"] { max-width: 80ch; }
> /* Don't */
> #main { Max-width: 80ch; }
I strongly disagree with this kind of guideline. The id attribute has very good uses, and in those cases the hash syntax should be the preferred way.
The use of id in an attribute selector is such a bad choice that makes me question the whole document.
As for the #selector, in what cases do you need that much specificity? Not a rhetorical question!
If you use an attribute selector, the engine must search through every single node in the DOM in order to return matches - an incredibly costly operation by comparison.
(Disclaimer, there may well be optimizations I’m not aware of at this point, but I’m pretty sure that’s why ids are generally preferred as a rule of thumb)
Nevertheless, in practice, I haven't seen any perceptible performance cost in using attribute selectors. It may have been the case years ago, when browsers weren't as efficient or devices as powerful.
I'd say the reduction of specificity here is well worth the theoretical cost.
In that case, yes it's less performant. But the specificity problem is so significant that I've followed the rule of never using ids for styling at all. The problems I've run into with CSS have entirely been regarding maintainability and preventing visual bugs, I've never had any CSS performance problem worth addressing. (I almost skipped this submission because I thought that was what is about, I'm glad I didn't.)
I'm sure the browser css engine treats that id attribute selector (without any regex-like) as an #id. Now if it were [id(*|^|$)=''] then it could have performance consequences, but they won't show up until you have thousands upon thousands of elements at which point maybe you should've looked into virtualization yesterday and not jump to wildcard selectors (speaking from experience).
Producing effectively with a minimum of waste, expense, or unnecessary effort.
Is there a better term you're thinking of?
And in my experience, the performance cost of using attribute instead of id selectors is so small as being imperceptible. So, as you say, the gain of suing them far outweighs the possible cost.
Why the hell does a rule talk about consistent padding and then not use css variable? A class name .card is a sure thing to conflict somewhere. Typography inheritance is another good way how to introduce hard to debug bugs. Global scope css - ewww (use only absolute minimum necessary).
Please use css-in-js, Tailwind, shadow dom or at the bare minimum css modules. Friends and future you will thank you.
But hey, maybe they are building some blog. I live in the apps world.
Also some rules are valid like the one about not using margin on the component itself.
Global scope is for base rules, yes. Rhythm, typography, maybe colors only. Still it's great to be able to style these design layers globally!
I'll adjust the examples to reflect this. Thanks for the lesson!
PS: hard to debug bugs? The inspector shows styles handily for global styles!
Of course, as with any convention-based, getting everyone to follow the rules might be difficult, although the stylelint-config helps.
It would be helpful to see an example of the ruleset applied to a large-ish solution. There are a lot of rules, so it's kind of hard to imagine what it would look actually look like in the css.
Could you elaborate a little on what you mean by “just-in-time” rulesets and how rule 22 ("Component styling should only be served with live components.") works in practice? Are you referring to web components?
https://github.com/efficientcss/ecss.info
As for the "just-in-time" CSS, I include the link tag directly in the component itself. Could be Web component or any other type. Yes, the link tag is repeated, but the browser does not download/interpret the file again since it's cached. Been experimenting with this in the past year and I only see advantages.
First, there's a lot less unused CSS in any page (for instance, on Tailwind's front page there is something like 80% unused CSS as per Google coverage tool).
Second, you have a live link to the CSS in your component file (I use Vim but I know VS Code can follow sources too).