Grid Systems, Drupal and Semantics (why CSS frameworks are not that bad in Drupal)

It seems Grid systems, or CSS frameworks, are being picked up by the Drupal themer community. I think this is a good thing. Some think it is a bad thing. So let us have a look at the downsides and upsides of grid systems in Drupal.

Grid Systems force you to change your HTML, that is bad.

This is wrong, for three reasons.

  1. It might be bad, if it were true. But not all grid systems, or their implementations require changes to the HTML, in theory. I love SASS and its tool-kit Compass. Compass pulls in grid systems such as Blueprint (native) or 960.gs (plugin), in such a way, that the CSS is the only thing you rewrite.
… you can apply battle-tested styles from frameworks like Blueprint to your style sheets instead of your markup.
  1. The second reason, is that changing your HTML might not be bad at all. More on this in the next argument “semantic by the way. But, summarized: only if you can afford being a purist, is this an argument. In all other cases, there is nothing wrong with changing and moving some HTML around. Off course, only to some extend (see Semantics). But GS usually require only minimal changes to your markup.

  2. The third reason lies with Drupal. In order to control the exact outputted HTML, you need not only a huge amount of Drupal theming experience, you need patience. And a gigantic maintainance budget. Drupal works with overrides: it will output source X by default, untill you decide you want to change it into Y. Now, for a theme_item_list, that is not too hard. But inherently complex functions such as themetable, it is. And these themable “things” are themed globally: if you change the item-list-generator, all your lists throughout entire Drupal, will be changed. Not necessarily bad, but it takes a large part of full control of your source away from you. Drupal also works with nested theme-calls. Theme-page calls theme_foo, theme_foo calls themebar, calls theme_item-list. Imagine hunting down that one item-list where you insist on having a .first and .last class, or a .horizontal-list, as required by your CSS framework. And lastly, Drupal is modular and flexible. Depending on your enabled modules, configuration, context or situation the source will change drastically. A logged in user may see different source and elements then an anonymous visitor. An admin with certain quick-edit-module might get popup-links when hovering certain elements. And dynamic modules, such as Views or CCK allow you to configure not only the data, but also the way the data is shown. These are all examples of modules that alter your source drastically. Again: full control of what is outputted is nearly impossible. So Grid Systems in Drupal are hard to achieve and require a lot of work, especially in details such as smaller elements on your site. But it is not that, when Drupal requires a lot of changes for minor changes in source, the concept of “making minor changes to source to force certain display” is wrong all-together. In Drupal it may not be practical, true. But the concept itself is not that wrong.

Semantics.

Changing the source may be bad because of semantics. Purists say that needing to change your markup (the meaning of the information) in order to change the display (the visualisation of the information) is wrong and was never the idea of HTML.

I agree.

However, purists may not have to deal with Internet Explorer in their work (Unfortunately, I do). And purists will steer away from Drupal. Or should.

Taken from a random Drupal site. Whitespace and identation deliberately left the way it is generated.

                                <div class="view-content">
                        <div class="views-row-1 views-row-odd views-row-first views-row-last">

<div class="views-field-body">
                <div class="field-content"><p style="font-size:1.2em"><img src="sites/default/files/images/drupal.png" style="float:right; margin-left:20px" />Met Drupal: maken en beheren van simpele tot en met complexe websites. Dit is de site van de Belgische en Nederlandse Drupal-community. Lees hier over Drupal's <a href="over-drupal" rel="nofollow">sterke punten</a>.<a href="node/1819" rel="nofollow"><br /></a></p>
    </div>
</div>
<a href="/sites/default/files/drupal-6.17.tar.gz"><img src="/sites/all/themes/lagelanden/images/download-drupal-btn.png" alt="" title="" width="215" height="32" /></a>  </div>
                </div>

Font sizes? Inline CSS? Field-content? empty alt tags? No alt tags at all? 4 nested Divs for a single paragraph? rel nofollow on something that clearly should be followed? Empty A-tags? How people, who work daily with a system that outputs such sources by default, dare mention the argument “semantics”, is beyond me.

This tagsoup in the example, is mostly the fault of views, which, in practice, adds gigantic loads of meaningless markup.

A class like “views-row-1 views-row-odd views-row-first views-row-last” is debatable. Some say that these are correct semantics. Maybe. But even if they are, the way some classes are embedded and some are chained makes no meaningful sense.

Why is .view-content outside of views-row and its subclasses? Why are these subclasses, but us views-field-body not a subclass of field-content? Why do we need these in the first place?

The answer is technical: because that markup it is dynamically generated with hilghly flexible and complex code, and we still want to provide enough handgrips for desingers to attach their CSS to.

Certainly not semantic. You cannot convince me that the subclasses views-row-odd views-row-first views-row-last make any semantic sense. Last and first together? It is the only item in the list, so it, technically is correct that it is both the last and the first item. And since it is the first, it is also the odd item. But semantic, meaning. Certainly not more then some additional grid-two-column class. Odd, even classes are just as semantic as classes used to identify columns in a grid.

Now, I will agree with you that the difference between:

<p class="paragraph teaser">
 <img src="sites/default/files/images/drupal.png" alt="Drupal screenshot showing the coolness of Drupal" />
 Met Drupal: .... Lees hier over Drupal's
 <a href="over-drupal">sterke punten</a>
</p>

and:

<p class="paragraph teaser grid-left">
 <img src="sites/default/files/images/drupal.png" alt="Drupal screenshot showing the coolness of Drupal" />
 Met Drupal: .... Lees hier over Drupal's 
 <a href="over-drupal" class="inline-button">sterke punten</a>
</p>

is important. And that the latter is worse then the first. But Drupal’s markup does not even get close to my handcoded and cleaned example. Adding a .grid-left to the tagsoup from the example output of views makes absolutely no semantic difference. At all. Adding it to the corrected, and cleaned examples below does make a difference.

My random example may be a particularly bad example. But before you comment with urls to examples that are cleaner, consider the heading-layout. Consider the source for logged in admins. And evaluate the entire source/markup ratio. It will be bad in Drupal. Please prove me wrong. :)

The other point is that semantics are a little overvalued. Not that I think we should abandon the idea of putting meaning in our HTML and go for the dirty solutions such as table-based layouts. I just say we should be pragmatic. Source order, for example. Most screen-readers and Braille terminals “look” at CSS. That’s because, in practice, most sites change “meaning” by chaning the layout. A form-label that stands above the form-element (like, by default in Drupal), even if done with CSS, will force the Braille terminal to insert a linebreak”: users must take an action to enter the form-field. Changing the CSS so that a label is not display:block, but display:inline will make your forms a lot more accessible. Being a purist gets you only halfway in this: you will still need to look at the entire picture: javascript, CSS and HTML. No (sane) web-indexer will ignore javascript entirely. The google bot may not execute all javascript, but will certainly evaluate it to see if the source is altered trough these scripts.

Good semantics are not just putting a navigation below the content and providing a “skip to navigation”-link. Good semantics are about the entire picture. From source order, via minimalistic source (four nested divs around a single paragraph, for goodness sake!), via correct weight of elements (heading layout etcetera’s), untill meaning-altering javascript or CSS. In practical Drupal this is as good as impossible; you can develop and design a minimalistic Drupal, but those are not the sites that stand for Drupal examples. It will, most probably, be considered an ugly, boring or not-very-representative site. Views is a de-facto standard. Zen a theme for theme-developers probably has the worst source/content ratio of all themes. And it is the most used theme.

Good semantics is about the big picture. And no Drupalsite will manage be semantically correct in that big picture. Not without a huge amount of work, that leaves you with a maintainance nightmare, overrides that, in lines of code, will be far larger then their originals. And a content- and editors- handbook that will make all editors depressed.

Are grid systems bad?

In theory: yes. In practice: hardly; but only in a place where you control your source and therefore can afford to be entirely semantically correct. They require minimal changes to your source. Adding style to a place that should only contain meaning. Adding a class=”horizontal-list” to an UL, in order to make it horizontal is bad practice. In a place where the rest of the source is perfect. But in a tagsoup like that of Drupal, a single class=”horizontal-list” will not make anything worse. Provided you can add that class in the first place, without large code changes (that need to be maintained). And having to re-order some HTML, but keeping it valid, is always a lot better then getting into ugly IE6 CSS hacks, that not only make your CSS invalid, but often add huge amount of extra CSS complexity to your designs.

Any Drupal themer, who does not want a CSS framework, because it does not use HTML like it should be used, is acting silly. Drupal, by default, renders HTML that is so far from semantically correct, that the additional downside of a few extra non-semantic classes, or the downside of a few extra not-so-well-source-ordered blocks does exactly nothing: and certainly not make your source less correct. If you really care for semantics, start with the low-hanging fruit and make Drupal, or its contributions, a little more semantically correct. A Drupal themer who says that using a grid framework is not very practical, because Drupal is far too dynamic and full control of the outputted HTML, is more correct.

I’m feeling a little bit

I’m feeling a little bit confused. GS’s are very old. Every (good) designer works with it! Why not use this GS also for your site?

Comming from our own maintained CMS, the first time i looked into de Drupal tagsoup, i was shocked. So much HTML! On the other hand, it really helped me getting all the styling done the first times i used Drupal.

So what do we have:
1. Lot’s of HTML (class / id’s / wrappers) we perhaps might or might not use
2. Too less time (money) to build it for our customers the most semantic way

How can we get this done? How will we be able to tell Drupal which parts of tagsoup it must give us and which parts not, without going through all the tpl’s? Would it be possible to interpret the CSS and give back those used.. (ah never mind, too dirty!)

For my point of view: i really would like to have only the necessary HTML / styles at the end of the HTML-output!

There are many reasons why

There are many reasons why someone chooses not to use a Grid System.
The most important, however, is “the design does not cut it”.

A Grid system will only be usefull if the design was made in a “grid”. Else you will be only limited by the GS and not benefit from it.

And yes, “to have only the necessary HTML / styles at the end of the HTML-output”! Dream on. Drupal is a CMS, with a particularly strange view(theme) layer: overriding defaults, instead of starting with nothing. This, I think will never be possible in Drupal. It may not be bad. But it seriously limits the effectiveness of a GS; result: GS in Drupal are a bad idea.

I wouldn’t consider myself

I wouldn’t consider myself a purist, but document order does matter a lot and most screen readers I’ve used for testing don’t seem to care all that much about the order the CSS forces the markup to be displayed in and just follow the order of the document, as if it were linearised.

I’ve also noticed that when a design wasn’t specifically made to fit in a grid, it can be very hard to do so, whereas I am still to get a design that I can’t fit into a negative-margin zen-like layout. I’ve tried endlessly to help people who wanted to tweak their grid-based theme just a little bit here and there to match their design and it can get ridiculously tedious.

Also, I don’t really get what SASS has got to do with anything here, that’s just a CSS preprocessor that allows you to use variables, calculations and compound styles (like border-radius becoming -moz-border-radius, -webkit-border-radius, …). Using a grid system actually partially removes the use of those (pretty handy) things, your widths are all described in your classes and you’re not suppose to override a grid system’s definitions.

Like I mentioned on twitter, the CCK and Views thing is a totally different discussion, which also has it’s solutions that are entirely agnostic of what layout system you use (for instance, Views’ idea of a grid is a table, but it isn’t called a table, it’s called a grid…)

I’ve seen grid implementations that were pretty much unmaintainable and I’ve seen Zen implementations that were perfectly maintainable. People that are familiar with Zen or the grid system in question will find it easily maintainable (if the extra CSS makes sense and doesn’t throw WTFs left and right), and people that aren’t familiar with Zen or the grid system in question won’t find it easily maintainable. I don’t find one system easier to maintain than the other, the way you write the CSS is a lot more important for this than the layout system you use.

I’ve been using Zen on a multitude of diverse projects which didn’t really fit into a grid, and I didn’t change a single letter to any of the templates. Even had this one project that swaps out nothing but CSS files and makes the layout look entirely different (CSS Zen garden style), I don’t even want to imagine trying that on a grid. Just make the meaning of a class depend on the context rather than have it be absolute.
A block isn’t a “grid1” in a “container1”, it’s a “block” in a “sidebar”. If you were to make it a “grid1” in a “container1”, you would be specifically defining it’s with on every occurrence instead of just making it scale with it’s container and control it through that. That way you can easily make the width of the container depend on the context and you don’t even have to care at all about how wide the actual block is. So then if your context changed and a sidebar was dropped, you won’t have this gaping hole, but everything will adjust itself to fill the void. And none of these things prevent you from using CSS preprocessors or templating systems that have PHPtemplate beat hands-down, these are (and should be) completely agnostic of what layout-system you go for.

And one last thing, grid systems make a bunch of assumptions that make me uncomfortable… 960.gs for example makes ALL grid boxes positioned relative and those push and pull things are just plain awkward and a result of other assumptions made.

To summarize: I guess I just don’t like how grid systems box me in and require me to use specific class names in total disregard of their context other than fixing their width and offsets in a container that already has a fixed width. But if a design was specifically made for a grid, I totally don’t mind using it, as it does make it a bit easier. But then you’re not only letting the order of your markup dominate the visual appearance, you’re actually designing it specifically to fit into this little box instead of letting creativity roam free. A designer should be able to focus on designing a great looking website, rather than focus on making something fit in a grid and not make it look like poo.

I totally understand people using grids though, it does make it easier if you find CSS as a language too tedious or just don’t want to spend any time learning natural document flow. The arguments against it (just like the arguments for it) are mainly details, nitpicks so it really just boils down to what it is that YOU are comfortable using. I’m comfortable using (most) grid systems, but I’m more comfortable having the freedoms of a traditional layout-method.

Thanks for the long reply.

Thanks for the long reply. That warrants for a long answer :)

I wouldn’t consider myself a purist, but document order does matter a lot and most screen readers I’ve used for testing don’t seem to care all that much about the order the CSS > forces the markup to be displayed in and just follow the order of the document, as if it were linearised.

I tried to explain two things: 1. source order matters! 2. it matters on every level: from document-layout to header-order to the way a form-fields HTML is ordered. In drupal, managing #2 is simply not possible, within a practical project (one with budgets and deadlines); I would say this impossible. So, my point is: stop whining about the possible side-effect of less-perfectly-ordered HTML, if you are not doing it right on every level.

I am, however, also saying that a GS does not neccesarily enforce you a certain source. some do, some don’t.

So this whole argument is moot: good GS’ don’t require unsemantic source orders. And even if they do, in Drupal you simply cannot control source order properly on all neccessary levels.

Also, I don’t really get what SASS has got to do with anything here, that’s just a CSS preprocessor that allows you to use variables, calculations and compound styles (like border-radius becoming -moz-border-radius, -webkit-border-radius, …). Using a grid system actually partially removes the use of those (pretty handy) things, your widths are all described in your classes and you’re not suppose to override a grid system’s definitions.

More correctly “compass”. Compass and SASS rewrite the GRID CSS so its sizes and so on are applied to the classes, IDS and elements. In other words: it renders the argument that one has to use unsemantic classes (like grid-left-nav-250) invalid. You can simply make “#content.teaser” act like a grid. In Drupal such things are impossible, because such technology does not exist. And, considering the very architecture of the theme system in Drupal, such technologies won’t be possible in any nearby release.

Zen, is a particularly bad example. I was told, but haven’t investigated nor confirmed it, that Zen does an extremely bad job on both SEO-point and accessability point. But that is a whole different post (yet to be written…). The point is, that if this is true, the whole argument of a Grid System becomes moot too: Zen is accepted in Drupal, but does a bad job. Just as bad as a Grid Layout (I was told it is far, far worse), so if you are a purist, steer away from Zen too. But, for the sake of the argument: let us assume Zen is perfectly accessible, screenreadable, mobile-friendly and so on. and a GS is not. Even then, Zen offers no full control of HTML. So even then, you still have gazillions of Divs, Classes, and what more to wade trough; hundreds of more obscure theme_x functions to override, if you want the source the way you need it. Zen, therefore, is far from a good tool for a purist, or someone who cares deeply about the semantics, clean-ness and correct-ness of her sourcecode. Just like a more traditional GS would be something to steer away from. In Drupal, therefore, this is a none-argument: all the arguments against a GS apply just as well to any theme system out there: that is a result of the very architecture of the theme system in Drupal.

A designer should be able to focus on designing a great looking website, rather than focus on making something fit in a grid and not make it look like poo.

Lastly. Agreed, and fully my mistake. Originally, I started my article with the sentence “Grid-based layouts are becomeing wildy opular on the web and the Drupal community picked this up. If your design or designer draws something up in a Grid System…. blablabla”. But that whole introduction on when to use a GS, wether it fits in your project and not was noise: I wanted to focus on the arguments after one has decided that the Design (not the technology) needs a Grid layout. After the designer has decided that she likes the way of designing. After the point you think “This photoshop-mockup really requires a grid-based CSS technology”. Then the argument about semantics and so on start,.

That is where this article starts.

So basically, what you’re

So basically, what you’re saying is “Drupal semantics suck, so we might as well make it suck a tiny bit more”? “Let’s just runaway from this big pile of mess and use a framework like Rails.” I’m glad that most Drupal contributors are a bit more disciplined than that.

Drupal’s templates are written with two important things in mind:

  • Semantics
  • Maintainability

The latter being considered the most important one, currently. For example, div.content in node.tpl.php might not sound very semantic. But thanks to this div, themers are capable of styling lists inside $content without causing style conflicts with ul.links and ul.terms (which are outside div.content). This is just a very simple example, but there are many and there are more complex ones (e.g. in Views). This is just how Drupal’s templates are written; to make the chance of having to override them as small as possible. Sometimes this results in less-semantic elements and/or classes. It’s a matter of priorities.

And don’t get me wrong. I agree that Drupal’s markup needs a lot of improvement; in semantics, and maintenance-wise. But I think adding presentational bulk like 960.gs to the markup is a step in the opposite direction. If you need evidence, just checkout the NCRV trunk ;) (You still have access to that, right?)

I am not saying “Drupal

I am not saying “Drupal semantics suck, so we might as well make it suck a tiny bit more”?

Instead, I am saying “Drupal semantics suck, so the issue of loosing semantics by introducing a grid system is purely academic. Makes no sense”?

I am also not saying “Let’s just runaway from this big pile of mess and use a framework like Rails”, but instead: “The Rails community has dealt with the issue and created a CSS framework where no changes to the markup is required”. In fact, that framwork is available for Drupal, but because of the first reason, not very workable.

In your next paragraph, you actually help me and my argument a lot, by stating explicitely that “Sometimes this results in less-semantic elements and/or classes. It’s a matter of priorities.”

Which, IMHO is the exact same thing that GS do. And the exact thing that makes them bad. However, Drupal, as you state, has chosen this route. Hence a semantic purist should avoid Drupal. If it were only because the Drupal developers did not knopw better, or were technically limited, then it would make sense, as you state, “that Drupal’s markup needs a lot of improvement;”. They don’t, because, as you say: the current situation is a matter of priorities: Drupals has chosen to do it this way.

The reason the example you bring up (I don’t see it appropriate to checkout code from projects I am no longer involved in, so I will have to believe your words for it) is so extremely cluttered is because of a point I made above: the way the theme system in Drupal works makes it really hard to add simple, clean, yet unsemantic markup. If you want a GS in Drupal, you probalby need to add cluttered, complex and unsemantic markup. In that case, the argument of a GS adding clutter makes sense. But that is because Drupals way of doing things does not match a GS’s preferred way. Not because the GS method is wrong in itself.

Consider a random example i picked from the 960.gs homepage: http://antonpeck.com/ Here I agree the additional col-1 and col-2, cta_item are ugly. But that is because all the rest is pure beauty. “featured”. Not #views-block-featured views-featured-body views-frontpage-odd, no. #featured! But I think you will agree that in this example the col-1, col-2 are not that bad at all, eventhough unsemantic, and ugly from a purist pov. Especially since they make it practically possible to have a beautifull site, yet minimalistic, nearly perfect markup. Nearly, yes, but would it be possible to have it 100% perfect, working on major browsers and affordable (in developers hours to be payed)?
Again, I stress, the downsides of GS are very visible in Drupal, because of its limitations and choices of the theme and module system. But because they are so visble there, does not mean that they are bad everywhere. As the random example shows.

<div id="content">
       <div id="featured">
         [...]
        </div><!-- #featured -->
        <div id="col-1">
          <div class="art_minilist">
            [...]
    </div><!-- #col-1 -->

    <div id="col-2">            
              <div class="blog_minilist">
              [...]
      </div><!-- .blog_minilist -->
            </div><!-- #col-2 -->

    <hr />

    <div id="col-3">
       <div class="cta_item">

“Drupal semantics suck, so

“Drupal semantics suck, so the issue of loosing semantics by introducing a grid system is purely academic. Makes no sense.”
“Drupal, as you state, has chosen this route. Hence a semantic purist should avoid Drupal.”

Again: accepting that Drupal’s semantics are not the best thing in the world is one thing, making it worse with grid-[…] classes is another.

The fact that Drupal has chosen to strive for a practical balance between semantic and maintainable code doesn’t mean that everything is perfect as it currently is. Like I keep saying, there’s enough room for improvement. And any added unsemantic element or class is one too many.

I don’t see how that is the same as GS do. I’m also not sure I understand your point with #featured vs. #views-block-featured. Drupal is merely prefixing the name with the module it came from to prevent conflicts. What’s so unsemantic about that?

To be completely honest I’ve kind of lost track of what the discussion is essentially about. It began with a link on Twitter to a CMS that had 960.gs in core, and me saying “yuck”. Because a CMS shouldn’t add presentational bulk that any designer wants to change anyway. Designing grid systems is more than choosing between 12 or 16 columns, as I tweeted earlier. Because it’s custom design work, a tool like 960 will only get in your way, causing it to completely fail in its mission. That, and it adds another layer of complexity into your markup.

I mentioned the NCRV project mostly as a joke, but it’s also a perfect example of how 960 makes large and complex designs even more complex. This project has about 15 overrides of page.tpl, just to be able to force 960 into working with different page setups with different sidebar widths, etc. which normally could’ve been done with $body_classes and context-specific CSS selectors. Instead of 15 page templates we could’ve also worked with if () {} else {} snippets, but I’ll guantantee you that would’ve become one HELL of a page.tpl.

I think, what this discussion is essentially about, is you claiming that the Rails version of 960 doesn’t have any of these problems and me not believing one word of it. SASS, which is an attempt to turn CSS into a fully featured programming language or something, doesn’t solve any of this as I currently see it. It just adds another layer of complexity as I see it, to work around it. Workarounds != solutions.

But I’d love you to show me some Compass/SASS/HAML stuff at the Drupal Dev Days, and hopefully show an example of a large, complex design implemented with it. Perhaps it’ll make me reconsider :)