9 ms·
Cargo Cult CSS
- daelen 13y agoGood read. It has me asking quite a few questions of what I consider to be best practice.
- zir1 13y agoPlease stop it with the massive fonts on web pages
- TheZenPsycho 13y agoPlease familiarise yourself with the command/control + minus shortcut.
- antris 13y ago> The web is fundamentally a semantic medium. I think this premise is outdated. The web is more and more hosting apps rather than documents.
- intranation 13y agoNot sure what that has to do with semantics. Are you saying that NAV etc. aren't valid semantics on applications?
- antris 13y ago> 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.
- KoehlerBear 13y agoSome users do, assistive technologies can benefit significantly from semantic HTML and class names.
- rowanmanning 13y agoSemantic HTML elements, yes, but CSS class names are ignored by assistive technology at large.
- Isofarro 13y agoThis "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 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 http://www.fixmystreet.com/around?pc=SW1A+2AA
- asdasf 13y agoWhat is fixmystreet supposed to be an example of?
- venticco 13y agoI had mixed feeling until I saw “USE IDS, FOR THE LOVE OF GOD”, then I stopped worrying and stopped reading.
- antris 13y ago> if you’re writing CSS professionally then there’s really no good excuse for not using a CSS preprocessor. CSS preprocessors aren't without their own problems as well. An app that I'm currently working on has a history of semantic HTML, but having no room for compromise had resulted in a CSS file that is 4000 lines long. There were lots of cases where an innocent looking @extend created monster selectors (https://twitter.com/mezzoblue/status/390274599167877120/photo/1 https://twitter.com/mezzoblue/status/390274599167877120/phot...). This is non-obvious behaviour that should be avoided.
- kapowaz 13y ago> This is non-obvious behaviour that should be avoided. Generating selectors automatically obviously carries the risk of creating something excessively large that you don't want. But I want to challenge the widely-held view that a long CSS file is necessarily bad. First of all, what does 4,000 lines mean in terms of actual document weight? And what is the weight of the gzipped file actually served? Repetition in selectors might look bad on the surface, but this is exactly the kind of thing that compresses well. Once actually served this file then ought to be cached well, and so the issue of document weight is potentially moot. Obviously there are extremes beyond which it may no longer be sensible to serve something like this, but arguably at that point you ought to be noticing issues in your un-preprocessed source anyway.
- ahoge 13y ago> 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.
- LoonyPandora 13y ago> 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.
- ahoge 13y agoWell, 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.
- 13y ago
- pupppet 13y ago> 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.
- ahoge 13y agoPreprocessors 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.
- kapowaz 13y agoIf 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.
- pupppet 13y agoThere are very few large scale projects that warrant it. Preprocessors save you a few search and replaces, little else. You're left with a stylesheet that is no longer recognizable to its author and can't be debugged via your browser's developer tools.
- NickLarsen 13y agoLet 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.
- russelluresti 13y agoThis is an argument I had with myself a few years ago, when OOCSS started becoming popular. Ultimately, what I determined is that there's a new definition of what "semantic" means in front-end architecture. Semantic doesn't just describe the content anymore, it describes the function as well. So using classes like "module-box" or whatever is perfectly fine. There's a bunch of articles out there on the new semantics, you can look them up as I can't be bothered to link to them. Ultimately, though, the "separation of concerns" was a good principle to use when the web was a bunch of documents meant to provide content. It isn't that way anymore. The web has evolved in use to include web apps and other forms of media, so our principles must evolve as well. It seems silly to cling to past definitions as immutable when we work in a field that changes drastically nearly every year. Also, anyone who advocates using a selector like "ul.members li a" and then complains about the maintainability issues of using non-semantic classes when an update needs to be made should really reconsider their hypocrisy. Using selectors like that (aside from being slow performing) completely tie the markup to the presentation. Hell, if all you did was update the list to an ordered list instead of an unordered one you'd have to update the CSS as well. The reality is that any change in the UI big enough to warrant updating the markup is going to warrant updating the CSS, and vice versa. Worrying about making them completely separate is impractical and will just cause you to waste time in development. If you're concerned about the discoverability of the code to a new developer, DOCUMENT IT. Do not create crappy selectors. Create a UI style guide that implements all of the major UI components and have the developer reference the style guide. Use something like KSS to link between the style guide and the CSS. There's no excuse for using bad selectors in the name of ease of on-boarding.
- Isofarro 13y ago"Ultimately, though, the "separation of concerns" was a good principle to use when the web was a bunch of documents meant to provide content. It isn't that way anymore. The web has evolved in use to include web apps and other forms of media, so our principles must evolve as well." The "webapps are not documents" position is hokum. Web apps are merely the application of HTML, CSS and JavaScript to create more functional/dynamic documents. But documents they still are. "Also, anyone who advocates using a selector like "ul.members li a" and then complains about the maintainability issues of using non-semantic classes when an update needs to be made should really reconsider their hypocrisy." That's a nice strawman you've propped up there. "If you're concerned about the discoverability of the code to a new developer, DOCUMENT IT. Do not create crappy selectors. Create a UI style guide that implements all of the major UI components and have the developer reference the style guide." Amen.
- hovertrain 13y agoConsider the authors examples: The first example should really use extend or it will output the content of the news-item for each selector separately, and the news-item mixin contains many properties with static values that will be repeated. This example also couple the styles to the content, it should now only be used for news, this is not about premature optimization, but consistently working against code reuse, why cannot the authors beautiful blue box be used on something else then news. The alternative, the box-standard can be use multiple times and it is easy to understand and modify from the developer perspective and to anticipate the result (“what is breaking?”). Lastly why do author use the element (div) selector, the element selector is not necessary and it prevents author from writing it on section or article if that would semantically correct. For the second example, yes, the example is extreme, and I do not see many people writing that many selectors, but the author is right that OOCSS will create multiple classes. Writing this box, I would probably have something like this: box box--special box--extra-padding. After the example the author argues that this is approach transfers the close coupling to the markup. Let us say we have a 100 pages and one style sheet, should the CSS really serve all those 100 pages or should each individual pages deal with one style sheet. With a 100 pages, it will be impossible to keep track of all the pages when you are writing your CSS. From my perspective, the last example is legit, but element selectors with descendent selectors might cause performance issues, because every time the browser hits a link (a) it have to evaluate the hierarchy in the selector. Btw, the ul element selector is unnecessary.