A bit meta, but I need this off my chest: I love how this document starts off saying "this is for react apps". IMO every discussion about CSS coding standards needs to start with context.<p>A lot of old CSS lore came from people who build websites. I mean those fairly uninteractive things, focus on content. Blogs, restaurants, newspapers.<p>Building an application that happens to use the DOM as their UI toolkit is totally different. The whole "reuse the same classes but with different content" thing that CSS classes were designed for becomes less important, and "reuse pieces of <i>behavior</i>" makes a lot more sense.<p>There's probably more domains or subdomains that warrant their own CSS best practices. But I'm totally tired of a blog designer and a react app coder fighting on HN about how the other one is doing it wrong, when really they're just solving different problems.
IMHO, naming conventions such as SUIT, BEM, OOCSS and the like are NOT a good practice, but merely a workaround for dealing with the limitations of a global namespace.<p>My preferred solution are CSS Modules[1], Vue's scoped styling[2] or something similar.<p>[1] <a href="https://github.com/css-modules/css-modules" rel="nofollow">https://github.com/css-modules/css-modules</a><p>[2] <a href="https://github.com/vuejs/vue-loader/blob/master/docs/en/features/scoped-css.md" rel="nofollow">https://github.com/vuejs/vue-loader/blob/master/docs/en/feat...</a>
Some thoughts:<p>- Good CSS design needs zero !important statements. Fix your specificity or your component architecture if you have a need to use !important.<p>- DRY is a good thing, not a bad thing. Maybe straight CSS isn't quite there yet but...<p>- Why not use the tools at your disposal to aid in development (and DRY) such as SASS/LESS?<p>- Flexbox will be great once IE dies the well-earned death it deserves.<p>I'm very happy the author had great success with their setup. What works, works. But I hesitate to assume that just because it works without using DRY principles or other tooling, it means you shouldn't.
".ComponentName-descendentName" nested inside ".ComponentName"?<p>Remember kids, the cascade is TEH B4DZORS - so always include everything you would have gotten from it in every class name. <i>headdesk</i><p>Solidly delivered on the "no DRY" premise. Maybe they should coin a new acronym like "WET": "Write Everything Thrice"
A random brain dump prompted by this statement: "No DRY. It does not scale with CSS, just focus on better components"<p>All programming involves resolving a conflict between two different principles and a lot of the fiercest disagreements are between people that weight the importance of these two things differently:<p>1. Reducing repetition<p>2. Reducing dependencies and side effects<p>The language and it's tooling/ecosystem can affect the importance of these.<p>The project's complexity, rate of change and lifespan is also a factor that might push you one way or the other.<p>But anything that helps one of these harms the other.<p>Thoughts?
> Flexbox is awesome. No need for grid framework;<p>Yes, great, if you can ignore all the IE users. Is that what "modern" means?<p>I'd love to use flexbox where I work, but it's just not feasible to give up all the customers we would lose.
Plug for test-driven CSS: <a href="https://github.com/jamesshore/quixote" rel="nofollow">https://github.com/jamesshore/quixote</a><p>I've experimented with it on a green field project and got promising results. Found I could refactor my CSS with confidence.
I agree with the Flexbox and DRY principles, but it's weird to still rely on arbitrary naming conventions like SUIT when CSS modules have been around for a while now.<p>Naming things has always been difficult, especially in CSS where it can lead to merging/overriding/precedence issues. <i>Not</i> having to <i>think</i> about what CSS class names are either available or possibly conflicting is a benefit from CSS modules that increases productivity by an order of magnitude I've rarely seen in CSS, especially in large codebases.<p>You've got a button and want to call it "button" and style it using ".button"? Go ahead. It will only inherit styles if you <i>explicitly</i> say so. The global namespace remains healthy for each component you write.
Personally, I very much dislike using CSS classes unless required. I prefer having a clean markup with classes used only where they make sense.<p>For a context, I somehow can't wrap my head around writing something like:<p><pre><code> <div class="nav nav-inverse nav-hide-xs">
</code></pre>
when `<nav>` makes more sense. Sure, if you have a case with alternate "inverse" navbar, go ahead with a class `<nav class="inverse">`.<p>About the flexbox, ah, well, even now they have undefined behaviour on several elements such as a `fieldset` [1].<p>[1]: <a href="http://stackoverflow.com/questions/28078681/why-cant-fieldset-be-flex-containers" rel="nofollow">http://stackoverflow.com/questions/28078681/why-cant-fieldse...</a>
FWIW, I'm using Angular (1.x) on a project at work, rather than React. One of the things I did recently was take the CSS file used in the project ("Few Pages App", rather than SPA, which <i>had</i> a common .css file), and turn it into an Angular (javascript) "directive".<p>I wish I had done this earlier. I have no "compile" step, it's just straight js plus the framework. However, I now have a mechanism to use variables for any (new) stuff with repeated settings, inserted into the rest of the text in the "<style> ... </style>" template.
I've developed my last 5 projects (corporate websites with many different layouts) with SASS + Foundation 6 an no naming convention. Instead I relied on nesting selectors.<p>I can behave and usually not go deeper than 4-5 levels. It's a really neat way unambiguously tell a CSS rule where it should belong to. For example I can create a<p><pre><code> section.hero-block{ /* things inside are scoped */ }
</code></pre>
CSS selectors who live outside are pretty basic, utility style ones, they exists on one level, so they can be easily overwritten by the scoped ones if needed.
So, I don't do any front end work in my day-to-day, so this may be a stupid question. This article starts out with how CSS gets a lot of negativity. What alternatives are there? Do browsers understand anything but CSS for styling?
I think an important thing to note here is that this individual's team is very small. If you have a small team that closely collaborates then scaling things which take discipline (like CSS) becomes vastly simpler.<p>CSS is difficult because it takes so much effort to do things the "right" way. It requires a good set of linting and testing tools or constant vigilance to maintain a correct, robust system.<p>As the codebase or team grows the difficulty of that task increases. That, to me, is why CSS often viewed in a negative light.
Recently I got into functional CSS:<p><a href="https://github.com/chibicode/react-functional-css-protips" rel="nofollow">https://github.com/chibicode/react-functional-css-protips</a>
Funny how all the original markup and languages are becoming the machine code of the web. No one writes js anymore, just compiles into js. No one writes css anymore, just SASS -> css. Html? Nope, directives or shadow dom.