Live data from Hacker News

Cargo Cult CSS

kapowaz.net

21–30 of 44 posts

Re: Cargo Cult CSS

#21
post #11

> Some CSS framework methodologists suggest that using IDs is a bad idea. The reasoning behind this is that IDs carry such a higher specificity [...] No, the primary reason is that IDs prevent reuse. If you want to build a library of reusable components, IDs are obviously not what you want. As the person who writes these components, you do not care how often one of these components is used. From your point of view, i…

"No, the primary reason is that IDs prevent reuse."

The fact that they cannot be reused in a single page is what makes IDs so useful.

There are benefits of IDs others mentioned, but for me properly used IDs make CSS so much clearer. An ID in CSS code is a flag saying: "custom-crafted for this part of the page only, not to be reused", or "non-modular code".

Unlike a class, an ID informs me with 100% certainty that if modified, it will not affect other instances on the page, because there are none. It's hard to overestimate the value of such information, especially when working on older or someone else's code.

Re: Cargo Cult CSS

#22
post #21
post #11

> Some CSS framework methodologists suggest that using IDs is a bad idea. The reasoning behind this is that IDs carry such a higher specificity [...] No, the primary reason is that IDs prevent reuse. If you want to build a library of reusable components, IDs are obviously not what you want. As the person who writes these components, you do not care how often one of these components is used. From your point of view, i…

"No, the primary reason is that IDs prevent reuse." The fact that they cannot be reused in a single page is what makes IDs so useful. There are benefits of IDs others mentioned, but for me properly used IDs make CSS so much clearer. An ID in CSS code is a flag saying: "custom-crafted for this part of the page only, not to be reused", or "non-modular code". Unlike a class, an ID informs me with 100% certainty that if…

>The fact that [IDs] cannot be reused in a single page is what makes IDs so useful.

This isn't enforced by browsers. They will happily render all elements with the same ID and apply the same styles to all of them.

This kind of thing really isn't useful. If you accidentally put the logo twice on the page, it will be clearly visible. It virtually never happens and when it does it isn't hard to track down.

This "just once" property of IDs really doesn't add anything useful.

>Unlike a class, an ID informs me with 100% certainty that if modified, it will not affect other instances on the page, because there are none.

If I modify one of my building blocks, I know that every instance of it will now look like this.

Consistency is awesome.

Re: Cargo Cult CSS

#23
post #22
post #21

Earlier quoted context omitted.

"No, the primary reason is that IDs prevent reuse." The fact that they cannot be reused in a single page is what makes IDs so useful. There are benefits of IDs others mentioned, but for me properly used IDs make CSS so much clearer. An ID in CSS code is a flag saying: "custom-crafted for this part of the page only, not to be reused", or "non-modular code". Unlike a class, an ID informs me with 100% certainty that if…

> The fact that [IDs] cannot be reused in a single page is what makes IDs so useful. This isn't enforced by browsers. They will happily render all elements with the same ID and apply the same styles to all of them. This kind of thing really isn't useful. If you accidentally put the logo twice on the page, it will be clearly visible. It virtually never happens and when it does it isn't hard to track down. This "just o…

> If I modify one of my building blocks, I know that every instance of it will now look like this.

But that's based on your own system, which may be non-obvious to other developers. The rules of valid HTML dictate the situation in which you will encounter a given ID, so this communicates something to every developer who sees IDs used in a given selector. To me that's tremendously valuable, and far more useful than trying to graft additional meanings onto parts of a single classname.

Whether that system is BEM, OOCSS, SMACSS or some other syntax, this kind of naming convention isn't a native part of CSS.

Re: Cargo Cult CSS

#24
post #5

> The web is fundamentally a semantic medium. I think this premise is outdated. The web is more and more hosting apps rather than documents .

This "apps rather than documents" is hokum. Each time one of these JavaScript-mandatory websites is declared a case-study in web app development best practice, decrying progressive enhancement, there's normally something underneath about building a PhantomJS scraper so that search engines are indexing something more than a blank page. For example, bustle.com's PhamtonJS emulation of being a good webizen: https://twit…

What is fixmystreet supposed to be an example of?

Re: Cargo Cult CSS

#25
post #23
post #22

Earlier quoted context omitted.

> The fact that [IDs] cannot be reused in a single page is what makes IDs so useful. This isn't enforced by browsers. They will happily render all elements with the same ID and apply the same styles to all of them. This kind of thing really isn't useful. If you accidentally put the logo twice on the page, it will be clearly visible. It virtually never happens and when it does it isn't hard to track down. This "just o…

> If I modify one of my building blocks, I know that every instance of it will now look like this. But that's based on your own system , which may be non-obvious to other developers. The rules of valid HTML dictate the situation in which you will encounter a given ID, so this communicates something to every developer who sees IDs used in a given selector . To me that's tremendously valuable, and far more useful than…

> But that's based on your own system, which may be non-obvious to other developers.

Yes, conventions are like that. You have to read the docs.

> [...] this kind of naming convention isn't a native part of CSS.

That's true for naming conventions in general.

Well, this stuff was added for a reason. If you know the rules, the SCSS and markup becomes way easier to navigate. Additionally, there are now many properties which can be automatically verified.

It adds structure which wouldn't be there otherwise.

Re: Cargo Cult CSS

#26
Let me start by saying I am the most vocal person about semantic HTML at my company, but it really depends on your purpose, and I'm not sure you are realizing you are taking a hard line. Also, I would have preferred you used the 90% of your words to give better examples of how to do things better than overstating how OOCSS and BEM are wrong. Well developed solutions are always better than just explaining what's wrong unless there is no solution and the existing proposals are wholly unsatisfactory.

Take a step back and realize that there is at least one more way of doing this. Why not have the presentation be the specification, and we can just build the html to conform to the that specification instead (via the use of helpers and all other commonly accepted forms of duplication reduction). Your post basically declares this Doing It Wrong™, but I imagine that's just because you'd rather see better looking html than you would see better looking css.

You have the semantic issues well thought out, however you are flat out declaring data semantics are more important than presentation semantics. For most projects, the HTML is only used in a browser, and isn't the source of truth for the application, i.e. it is inherently coupled with the presentation. They might as well be one and the same because this is it's only purpose in the world. Leading from that, the only issue is maintainability, and here is where your ideas aren't fully developed.

As will all separation of concerns, your solution (the one about breaking news) also adds a layer of misdirection. Sure I don't have to change the html to change the presentation, but now I have figure out where that part of the presentation is modified. My CTO and designer (2 people I have a lot of respect for) were the people who eventually changed my mind on this. They said, "I don't know why this button is blue, but it's supposed to be green". Well it turns out it's blue because it has the class blue on it. Instead of trying to figure out what selector was causing this button to be blue, all I had to do was remove the blue class and add the green class. It took 15 seconds. It took probably 3 or 4 weeks for me to relent on this before I accepted, but it is much easier for everyone (think of the new hires as you pointed out!) if they don't have to know tons of info about the domain, but just look for the offending element and make a simple change in place rather than have to unravel the css toolkit to find where my button was made blue instead of green. Additionally, they know this change is only affecting this one element they were told to change. On our team, that is better maintainability because that is how real changes are specified. In your case, by the time this spec makes it's way to the developer, it needs to have traveled through people who know that part of the web page is a breaking news container and translated from whatever the original complaint was or the developer will likely have another problem to figure out before he can even begin changing what will likely end up being a single css rule.

Loose coupling is simply about defining avenues of change, and it only improves maintainability when it enables making changes easier. This has been extrapolated to mean "does this have to change or not", which is over thinking it a bit. In my example, I made a typical update by changing exactly one thing in exactly one place, without having to traverse any levels of misdirection. Just figuring out how to make one thing not have to change doesn't make it more maintainable.

When we're talking about web pages, it's also important to realize that we're typically not changing just the presentation of static data; we're adding and removing features to figure out what makes this page more useful. The HTML is typically going to change anyway when we're modifying the page, which again leans in favor of a common set of CSS selectors to build against (in terms of avenues of change).

As I said, I'm probably the most outspoken person at my office when it comes to semantic HTML, but it's only when I'm describing actual data, i.e. the parts you read. These parts tend to change very little and can be generated and stored in their already rendered state. The parts that do change more often gain a lot from the benefits of OOCSS and BEM techniques.

Re: Cargo Cult CSS

#27
post #13
post #11

> Some CSS framework methodologists suggest that using IDs is a bad idea. The reasoning behind this is that IDs carry such a higher specificity [...] No, the primary reason is that IDs prevent reuse. If you want to build a library of reusable components, IDs are obviously not what you want. As the person who writes these components, you do not care how often one of these components is used. From your point of view, i…

> No, the primary reason is that IDs prevent reuse. Reuse of what, exactly? Selectors? Properties? My assertion is that if you need to re-use properties a lot then you should make use of a preprocessor framework and then use a combination of mixins or extends. Re-use of selectors is an unnecessary artefact of how OOCSS (and similar) suggest you should write your code. > If you want to build a library of reusable comp…

I think most of the advocates for OOCSS would agree with you that preprocessors make them better. But the principles they're proposing are made better by preprocessors, they're not invalidated by them. Preprocessors let you get the benefits of reusable components without polluting the DOM with classnames. Which is better than plain OOCSS in most cases. But OOCSS allows you to have those reusable component benefits without tying you into a specific preprocessor (or any at all), and that is a benefit as well, even if you personally don't think its worth the semantic tradeoff.

Basically

OOCSS with preprocessor => reusable components + minimum DOM impact

OOCSS without preprocessors => reusable component + potentially messy DOM

CSS without OOCSS => potentially clean DOM + potentially non-reusable CSS

Re: Cargo Cult CSS

#28
post #22
post #21

Earlier quoted context omitted.

"No, the primary reason is that IDs prevent reuse." The fact that they cannot be reused in a single page is what makes IDs so useful. There are benefits of IDs others mentioned, but for me properly used IDs make CSS so much clearer. An ID in CSS code is a flag saying: "custom-crafted for this part of the page only, not to be reused", or "non-modular code". Unlike a class, an ID informs me with 100% certainty that if…

> The fact that [IDs] cannot be reused in a single page is what makes IDs so useful. This isn't enforced by browsers. They will happily render all elements with the same ID and apply the same styles to all of them. This kind of thing really isn't useful. If you accidentally put the logo twice on the page, it will be clearly visible. It virtually never happens and when it does it isn't hard to track down. This "just o…

>This isn't enforced by browsers. They will happily render all elements with the same ID and apply the same styles to all of them.

We live in post-xhtml times, browsers will try to make sense and render all sorts of bad code.

>If you accidentally put the logo twice on the page, it will be clearly visible. It virtually never happens and when it does it isn't hard to track down.

You're assuming the visual difference in applied style is always clearly visible, like a repeated logo. No, it's often not, not until someone changes something in a class and the change is populated everywhere the class was reused. An ID is a guarantee it was not reused, or at the very least serves as a "do not reuse" sign.

Edit: formatting

Re: Cargo Cult CSS

#29
post #17

> if you’re writing CSS professionally then there’s really no good excuse for not using a CSS preprocessor. I want to compile my CSS about as much as I want to compile my grocery list. Preprocessors add a layer of complexity on to something that is already impossibly simple.

If your CSS ‘impossibly simple’ then it sounds like you don't need it. Every tool has its place: writing HTML by hand is still worthwhile in certain situations. My article assumes you are writing CSS in a project of sufficiently large scale that how you approach architecting it is important enough to think about.

Re: Cargo Cult CSS

#30
post #19
post #13

Earlier quoted context omitted.

> No, the primary reason is that IDs prevent reuse. Reuse of what, exactly? Selectors? Properties? My assertion is that if you need to re-use properties a lot then you should make use of a preprocessor framework and then use a combination of mixins or extends. Re-use of selectors is an unnecessary artefact of how OOCSS (and similar) suggest you should write your code. > If you want to build a library of reusable comp…

> Reuse of what, exactly? Selectors? Properties? Building blocks or legos. You create a library of these and then you just use them. Soon, you reach a point where implementing new features/views doesn't require any new CSS. You just use what's already there. As a result, the total number of rules is kept at a minimum, which helps with performance and also with maintenance. > If you're building components that could e…

> Basically, it's just a naming convention which tells you something about the purpose of this particular class. In languages like Java, C#, etc. you also use naming conventions to communicate something useful. In CSS, I do the same thing. There is UpperCaseCamelCase, lowerCaseCamelCase, _withLeadingUnderscore, and x-prefixed. All of these things mean something specific.

What does those four styles mean then, respectively? I'm curious.

Post reply on HN