Live data from Hacker News

CSS Classes Considered Harmful

keithcirkel.co.uk

11–20 of 28 posts

Re: CSS Classes Considered Harmful

#11
CSS classes are great at what they're designed for. I don't see the problem.

If you're afraid some random other CSS may come in and ruin your existing classes, you've got other problems. In Javascript I can override random functions in prototypes and leave you to deal with it, but that doesn't mean you should program as if I might.

I've become accustomed to writing SCSS rather than CSS to make generating properly scoped components easier. It saves a lot of declarative repetition when all I want is to style the first and last spans of a div tagged with a certain class while also styling the other components inside the div. I know there are many advanced features in CSS generation libraries but this is all I really care about.

I'm a strict believer in separation of concerns on the web. Not just because it produces cleaner code, but also because it makes custom user styles easier to accomplish.

That said, custom tags are an elegant solution I haven't really considered yet. I found out about them when I couldn't really rely on browsers supporting them and have ignored them ever since. I think they're probably the best of both worlds, though I expect many HTML generation libraries to choke on them.

Re: CSS Classes Considered Harmful

#13
A catalogue of errors one can implement with CSS classes (as easily as using other CSS selectors) and of bad solutions to nonexistent problems.

On top of that, a strange attitude: apart from the embarrassing argument that "old" technology is bad, the author assumes that HTML is written by hand, apparently by a team of monkeys taking pride in the intensity of their fuzz testing, rather than generated correctly from templates.

Hence the emphasis on defensive coding and on treating easily fixed mistakes as a disaster (as if web developers routinely edited pages in production without testing).

Re: CSS Classes Considered Harmful

#14
post #7

The problem they describe is nonexistent. They say is bad because some other CSS can come along and co-opt what big means Doesn't matter, because you would not style ".big" but ".card.big". Yes, if someone would redefine "card", that would be a problem. That's why this "base class" should not be as short as just "card" but rather "userInfoCard" or something like that.

> The problem [he] describe[s] is nonexistent. [He] say[s] is bad because

    some other CSS can come along
    and co-opt what big means
To me the complaint is like saying, when programming, that I shouldn’t define functions with names in a file that’s imported into another file, because “some other” file can “come along” and redefine those names. If that’s a problem then that’s your problem, not the existence of classes. Or names.

Re: CSS Classes Considered Harmful

#15

Wish this article dedicated time to considering whether widespread use of attribute selectors will slow down the browser, but it doesn't seem to. CSS class selectors have been aggressively optimized by browser vendors for decades, vs. selectors that just filter on an arbitrary set of HTML attributes. Modern devices are pretty fast though, so maybe it's not an issue - but I know in the past the cost of doing CSS rule…

I considered exploring it but css selector performance just doesn’t matter with todays engines. There are far greater issues, and far better performance wins than “optimising your selectors”.

Re: CSS Classes Considered Harmful

#16
post #7

The problem they describe is nonexistent. They say is bad because some other CSS can come along and co-opt what big means Doesn't matter, because you would not style ".big" but ".card.big". Yes, if someone would redefine "card", that would be a problem. That's why this "base class" should not be as short as just "card" but rather "userInfoCard" or something like that.

Naming collisions are a pretty real problem when you get to building large systems. They’re a big enough problem for something like CSS Modules to exist (explained further down in the post).

Re: CSS Classes Considered Harmful

#17
post #3

After reading the article I found not a single indication, at all, of CSS classes actually being harmful. But the allusion of the headline worked, and I clicked.

I felt the “considered harmful” style of titles have been overused that no one would reasonably consider it anything but click bait; and so used it as ironic-click-bait.

Re: CSS Classes Considered Harmful

#18
post #3

After reading the article I found not a single indication, at all, of CSS classes actually being harmful. But the allusion of the headline worked, and I clicked.

I felt the “considered harmful” style of titles have been overused that no one would reasonably consider it anything but click bait; and so used it as ironic-click-bait.

Right. It's a tired trope at this point. Sort of like "disrupting" this or that business segment.

Re: CSS Classes Considered Harmful

#19

CSS classes are great at what they're designed for. I don't see the problem. If you're afraid some random other CSS may come in and ruin your existing classes, you've got other problems. In Javascript I can override random functions in prototypes and leave you to deal with it, but that doesn't mean you should program as if I might. I've become accustomed to writing SCSS rather than CSS to make generating properly sco…

For large applications with many thousands of pages, or many thousands of engineers working on the same system, name collisions happen. It’s not just throwing “random css” into the mix, it’s that many disjointed parts of the same application suddenly become connected and interdependent. Especially with “parameter names” like “size”. You really have three choices to solve this: invent elaborate tooling to catch overuse; prepend your classes in the hope that lowers conflicts; or over specify selectors and deal with the specificity issues that fall out from this.

Custom elements are very well supported today! I’d encourage you to spend more time with them!

Re: CSS Classes Considered Harmful

#20

A catalogue of errors one can implement with CSS classes (as easily as using other CSS selectors) and of bad solutions to nonexistent problems. On top of that, a strange attitude: apart from the embarrassing argument that "old" technology is bad, the author assumes that HTML is written by hand, apparently by a team of monkeys taking pride in the intensity of their fuzz testing, rather than generated correctly from te…

I don’t recall saying old technology is bad. I was more trying to say it served its purpose at the time but as websites have gotten more complex we’re still relying on this one single primitive.

It’s true that as systems scale often templating systems are introduced to alleviate writing html, but these systems are rarely complete which means developers - meeds unmet - need to “drop down a layer” and in that should be set up for success not failure. Having a design system that use brittle underlying primitives will not set developers up for success.

Post reply on HN