Classes? Where We’re Going, We Don’t Need Classes (CSS)
coding.smashingmagazine.com
coding.smashingmagazine.com
"This is a well-written and well-argued piece, but I have to ask: have you ever built a large site with many templates, components and layouts? This approach is arguably impossible to implement for that context, not to mention inappropriate. Yes, CSS3 has some fairly advanced selectors, but littering your stylesheet with HTML-dependent stuff like “section ~ article h2″ is only going to cause you pain when you want to refactor, or simply target headings that don’t follow a section. Either you end up with 20-line-long lists of selectors, or you’re afraid to touch your HTML structure in case everything blows up.
I really don’t follow your argument about elements looking the same in different places being aggressive/inappropriate. Have you ever designed a user interface? The goal is to produce consistent and reusable components that the user will intuitively recognise and use. If my “post comment” button looks different each time I put it somewhere new, what kind of experience does my user have? Do my comment counts decline as a result? This doesn’t just effect nerdy semantics, it’s real-world usability too.
Finally, I don’t really buy your argument that we should avoid using classes because some mysterious third party content providers might use our entire HTML structure and it won’t look right. If we designed for that use case we’d all have sites like Jakob Nielsen’s (eg: plain and vanilla). It’s also possible to use semantic markup (blockquote tags for quotes, etc) and use classes, they’re not mutually exclusive. Also, if somebody pulls in my HTML which uses my custom classes, who says they have to load my CSS too? The classes won’t impact their site’s design unless there’s a naming overlap.
In summary: classless HTML might work well on small, mostly trivial sites, but long term, it’s not scalable or modular (as Jonathan Snook’s SMACSS framework explains)."
Still a case can be made to reduce use of classes. e.g. for input[type=text], etc. Typically for any one site the style for most elements should remain consistent - you wouldn't have half a dozen different styles of input[type=text].
I haven't worked on an especially large website before and I'm unsure of the following points:
1. Let's say you're integrating two site-lets into a bigger site. Would namespace be a problem for classes specified in the two site-lets? What about namespace clashes between different "plugins" (e.g. jquery UI or something less polished).
2. Would the use of classes lead to "specificity war"? e.g. lets say we have the following CSS:
h1:first-child { padding: 10px; }
h1.large-heading { font-size:x-large; padding: 25px; }
And then later a web developer comes along and slaps in a h1 like this <div><h1 class="large-heading">some text</h1> <p> some content </div>
He finds it breaks the h1:first-child rule and so goes and adds: h1:first-child { padding: 10px !important; }
h1.large-heading { font-size:x-large; padding: 25px; }
And we can see where this is heading... when another developer sees the h1:first-child rule overruled his p + h1:first-child rule and slaps an !important next to it.If we're using relative position selectors instead there wouldn't this problem be solved?
body > h1:first-child { padding: 10px; }
body > div > div > h1:first-child { font-size:x-large; padding: 25px; }
body p + h1:first-child { font-size:large; padding: 25px; }
I know generally you don't use headings like this but I'm trying to craft a simple example. I am not sure it still carries my point properly, so I'm crossing my fingers.You mentioned being afraid of changing the HTML if your CSS is html dependent. Well if your website warrants a refactor of HTML wouldn't it warrant a refactor of CSS as well? After all, CSS is for describing how a piece of HTML should be presented. If you want to describe how an alternate set of HTML be presented, you use an alternate set of CSS rules, hopefully instead of injecting style information into content HTML that should ideally be only presenting information. There's a reason we stopped using <b></b> and <font></font> within our HTML, right?
You could easily have more than half a dozen. You might have 3 different color schemes (light, medium, dark inverted), 5 different widths (what kind of form field is it?), 3 different font size contexts with customized padding, one with autocomplete and one without, one that's flat and one that's 3D, one that displays default text and one that doesn't... multiplying those yields 360 different variations, generated from 17 different classes.
Not all those variations will actually be used, but this is certainly a reasonable visual vocabulary for a medium-size site.
And really it would be better to avoid references to linguistics when talking about a markup language, especially references from early structuralism which, basically, has failed.
> > To wit: browsing the article's source code and searching for "class=" yields 1158 results
I wasn't suggesting the author was responsible for Smashing Magazine's source; I was demonstrating that sufficiently complex websites have good cause for using classes. Styling all of that markup without those classes would be an absolute nightmare.
This is because, in my real-world experience, HTML structure can change very easily. The h1 turns into an h3 for SEO reasons, a wrapper div gets added for a visual effect, an extra span gets thrown inside an existing one. And suddenly, all the CSS is broken because it all depended on the exact structure of the HTML. Maintenance becomes a nightmare.
But if you do everything with named classes, then nothing breaks at all. Writing CSS without classes makes as much sense to me as writing JavaScript code without function names.
My soon former job involves two websites that have a large chunk of its markup with no classes or ids provided by the CMS. It is a nightmare to support or to add a new feature. I have a selector that is eight elements deep, all to target one cell in a table embedded in other tables. Well, to be fair, I'm guessing the author wasn't considering that kind of site. I can scare you at the campfire with stories of the tables I deal with that have no classes or ids. We've rewritten a good chunk of the CMS to clean up the markup and add classes to make my life as a front-end guy much easier.
This reminded me of the tables, I need some quiet time now.
The rhetoric is the design community has been content first design, and such a bold choice in CSS authoring would really support this idea.
I would love to try this out in a real project.
I wish more people would advocate for a middle ground between this content driven approach and an OOCSS approach. They work so nicely together.
Who ever said that you should stop caring about using semantically correct tags when using portable classes for styling?
I see the class functionality in the templating system as a benefit, and makes styling much easier, but for someone who dislikes classes, I can see why the author wouldn't like Drupal.
This represents a trade-off - 'uglier' HTML and extraneous CSS classes in return for a drastic reduction in development effort and the knowledge required to bootstrap a typical project. This trade-off makes sense if an in-depth knowledge of HTML is not your strong point (and even most web devs are not fully-fledged "front end developers" conversant with the finer points of the W3C's latest magnum opus), and if you would benefit from having a lot of boilerplate stuff (user registration, access control, content management, RSS, caching, RDF, email notifications, widgets/blocks, templating engine, etc.) for free. Sure, if you're willing to do all of that stuff yourself, you can ensure that no CSS class is wasted (or, indeed, no CSS class is used at all). But most people can't make that trade-off, and even those who can would struggle to justify it beyond the most trivial or superficial projects.
The best solution is probably to take a framework and modify its output to look the way you want it to. That's why the swipe at Drupal is odd, as pretty much everything Drupal does can be modified via hooks (kinda like AOP) and templates.
So much of working with Drupal is just overriding what Drupal wants to spit out instead - here's this Views markup, or this user registration process, or this database schema, now customize it to your liking. Rails takes the exact opposite approach - here's nothing, now make what you want. As my abilities grow, I'm coming to greatly appreciate the second approach.
Perhaps it's the author's experience with other modern development tools rather than a misunderstanding of Drupal that's informing his bias.
If you haven't checked out other content management systems, you're missing out. Imagine if the Views workflow were made first class and efficient, instead of being tacked on.
'Classitis' is certainly not a concern for parsers because they do not know it is going on. Then again, that's my point.
I beg to differ. You should Google that.
Element names belong to a shared lexicon which all developers and sufficiently up-to-date parsers understand. Classes are invented per project. They are a superimposition. They muddy the water.