For me, CSS has been a Solved Problem™ for 5 years now. If a website is small then I write plain HTML/CSS/JS in the old school way and all of these abstractions/systems are YAGNI. If a website is large enough for these abstractions to matter then I'm using React with inline CSS. I get reusability and composition without a noticeable performance tradeoff. You can still build your components to decouple style from cont…
How do you re-use this inline CSS?
CSS Utility Classes and “Separation of Concerns” (2017)
21–30 of 108 posts
Re: CSS Utility Classes and “Separation of Concerns” (2017)
#22I think the utility-based approach overlooks an important point: semantic class names are useful for a lot of things besides writing your styles. "Codeless tracking" systems let you put in the CSS selector for a button and find out how many people clicked on it. UI test automation tools let you specify the CSS selector of an element to click on. User stylesheets let people make tweaks to your site if they have access…
Nothing stops you from adding a semantic class name or ID to a tag for easy reference.
Re: CSS Utility Classes and “Separation of Concerns” (2017)
#23Re: CSS Utility Classes and “Separation of Concerns” (2017)
#24Re: CSS Utility Classes and “Separation of Concerns” (2017)
#25One area that wasn't covered was the ease of creating truly responsive layouts, inline, by defining different utility classes to the same elements based on screen size. "Responsive" is so much more than just the placement -- quite often, mobile version needs different font sizes, overflow behavior, thinner margins (since there is less screen space), etc. Very easy to do with the utility approach.
Re: CSS Utility Classes and “Separation of Concerns” (2017)
#26Earlier quoted context omitted.
How do you re-use this inline CSS?
Not the parent, but the approach I've seen in React is to treat inline CSS as objects like "{ fontWeight: "bold", color: "#000" }". So to re-use it, you can either put it in a constant or make a component that applies the styles to an element (and maybe takes props to override them).
Re: CSS Utility Classes and “Separation of Concerns” (2017)
#27> Isn't this just inline styles? Yes it is. I personally don't like the end result, where by "separating the concerns" you end up hardcoding your style in the HTML markup. For me the utility classes are just exposing the CSS rules to the HTML markup, which I think is actually the opposite of separation of concerns because I when I think of "separate" I think the most of writing code in two different files. > The amaz…
All that said, 100% agreed with your closing sentence! :)
Re: CSS Utility Classes and “Separation of Concerns” (2017)
#28The real issue with css is that there is an enormous amount of code re-use while at the same time almost no code-reuse at all. The author's example highlights this perfectly: he has a "media-card" representing both the "author-bio" and "article-preview" but the "author-bio", in this case, needs to be slightly different. This, to me, is the quintessential css problem. Almost nothing in css is identical, but almost eve…
In programming, this problem would be solved with something like higher order functions or parametric data types, allowing us to both abstract out the commonalities and maintain incredibly specific, easily modifiable, highly customized functionality. Is there an analog in the CSS world?
Re: CSS Utility Classes and “Separation of Concerns” (2017)
#29Re: CSS Utility Classes and “Separation of Concerns” (2017)
#30The decision to make CSS libraries that are reusable across all parts of your application is spot on. Also it helps with theming, eg dark mode. To avoid listing tons of classes on each element, you can use something like less: .my-class { .utility-class-1; .utility-class-2; } Reusable constants also help a lot.