Live data from Hacker News

Cargo Cult CSS

kapowaz.net

11–20 of 44 posts

Re: Cargo Cult CSS

#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, it only matters if it's used at least once. If it isn't, you can throw it away.

Secondly, if you use IDs which were added for JavaScript or fragment links, you'll introduce some coupling which really shouldn't be there.

Same thing with JavaScript. If you need a class, you should add a prefixed one (e.g. "js-foo"). Obviously, classes like that should never appear in your stylesheet.

Nothing of this has anything to do with cargo cults. There is an actual reason behind every rule.

Re: Cargo Cult CSS

#12
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…

> Nothing of this has anything to do with cargo cults. There is an actual reason behind every rule.

There may be reasons, but I find there is often a lack of understanding about the context in which that rule was developed, and the compromises that were made. A developer not understanding why things must be done in a certain way, but doing it anyway sounds pretty cargo-culty to me.

Re: Cargo Cult CSS

#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 components, IDs are obviously not what you want.

If you're building components that could end up on any page entirely outside of your control, I agree. But: horses for courses. It still has valid uses, and avoiding it entirely isn't necessary.

> Secondly, if you use IDs which were added for JavaScript or fragment links, you'll introduce some coupling which really shouldn't be there.

You're looking at this backwards: IDs exist for many purposes, not just CSS. Selecting elements by ID in JavaScript, or using a fragment identifier to link to within a page are just two other examples of how they can be used, and not considering the other purposes of the attribute is part of the problem (see also ‘CSS classes’).

> Same thing with JavaScript. If you need a class, you should add a prefixed one (e.g. "js-foo").

How does the developer benefit from writing the classname like this? Why is a prefixed classname any more informative to you than a selector that tells you a little about the context in which the element appears? I've actually come around to the view that if you're using this class to select on any other criterion than the semantic purpose of the element is wrong, and it would be more appropriate to use a data attribute to contain the hooks for additional behaviour.

Re: Cargo Cult CSS

#14
post #9

Earlier quoted context omitted.

Not sure what that has to do with semantics. Are you saying that NAV etc. aren't valid semantics on applications?

> Are you saying that NAV etc. aren't valid semantics on applications? What I mean is that the (supposed) benefits of semantic use of CSS classes go out of the window when you're no longer hosting documents to be read by machines. And the user does not care if the classes of HTML elements are semantic or not.

Some users do, assistive technologies can benefit significantly from semantic HTML and class names.

Re: Cargo Cult CSS

#15
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…

> Nothing of this has anything to do with cargo cults. There is an actual reason behind every rule. There may be reasons, but I find there is often a lack of understanding about the context in which that rule was developed, and the compromises that were made. A developer not understanding why things must be done in a certain way, but doing it anyway sounds pretty cargo-culty to me.

Well, that's the definition of a cargo cult. Following some rules without actually knowing why.

If you use IDs because some person on the internet said "Use IDs, for the love of God", you're cargo-culting.

Thing is, I know why I follow these rules. I'm the one who wrote them and refined them over the course of several years. I made informed decisions based on 13+ years of experience of which I spent the last 4 writing ecommerce related frontend code for dozens of websites.

This stuff is never done. It's always based on my current knowledge/experience and the limitations of the current boat-anchor version of IE.

Nowadays, my code is more maintainable, I can actually do some sort of "refactoring", and the total amount of selectors is very close to the optimum.

Anyhow, not using IDs makes writing CSS easier. Even if you don't understand the reasons/mechanics behind it.

Re: Cargo Cult CSS

#16
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://twitter.com/tyleralove/status/367494129091346432

I like how people keep pointing to Google Maps as an example of Web Apps, and how web development best practice is outdated: http://www.fixmystreet.com/around?pc=SW1A+2AA

Re: Cargo Cult CSS

#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.

Re: Cargo Cult CSS

#18
post #9

Earlier quoted context omitted.

> Are you saying that NAV etc. aren't valid semantics on applications? What I mean is that the (supposed) benefits of semantic use of CSS classes go out of the window when you're no longer hosting documents to be read by machines. And the user does not care if the classes of HTML elements are semantic or not.

Some users do, assistive technologies can benefit significantly from semantic HTML and class names.

Semantic HTML elements, yes, but CSS class names are ignored by assistive technology at large.

Re: Cargo Cult CSS

#19
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…

>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 end up on any page entirely outside of your control, I agree.

Well, I don't write every template/view/whatever.

>How does the developer benefit from writing the classname like this?

If you change the JS, you can freely add/remove those "js-" classes.

If you change the structure of the markup, those classes will warn you ahead of time of potential issues. They will also give you something for grepping.

Also, using those classes on the CSS side can be easily identified by tools.

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.

Apart from reset/normalization and base styles, everything is written in the same mechanical style. This makes the code really easy to navigate. It's very organized.

Re: Cargo Cult CSS

#20
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.

Preprocessors help with organization. I have one partial for each of my legos, for example.

They also allow you to do things like cache busting or sprite sheet generation.

Personally, I think it's totally worth it.

Post reply on HN