Want to get Lullabot article, videocast, and podcast announcements delivered right to your in-box? Let us know your email address (we won't share it) and we'll let you know when anything exciting happens.

Fixing Menu CSS for Themers in Drupal 6

In Lullabot Podcast 46, Jeff Robbins interviewed the Australian team of developers from the 2007 Full Code Press competition. The Drupal team in that competition narrowly lost to the 'custom CMS' team, and one of the things that threw them off was the combination of the contest's tight deadlines... and Drupal's notoriously difficult-to-override menu related CSS rules. Think about it: a contest was designed to show off tools make the best rapid development system for web sites, and Drupal's menu-related CSS ended up dragging the designer down rather than helping. That should be a sign to us that something needs fixing.

The underlying HTML that we generate when building menus isn't bad. Rather, it's the extremely specific rules that core applies to control subtle things like indentation, spacing, bullet icons, and so on for all the menus it generates. Overriding these styles using normal CSS cascade rules is nightmarish, because they're extremely specific -- and CSS gives precedence based on how specific a rule is. In addition, Drupal's menu related CSS rules are dumped into the general 'system.css' file, with everything from tabbed navigation CSS to the rules needed to make draggable textareas work. Simply writing a PHP snippet in template.php that hacks out system.css would break lots of other stuff.

Fortunately, fixing this problem appears to be really easy: so easy, in fact, that we can do it without breaking any APIs and without requiring any new syntax for theme developers. This issue on Drupal.org first splits out just the menu-related CSS rules into a separate system-menus.css file. Then, it cleans up the mechanism by which themes can 'override' other style sheets with their own copies. If a themer wants to entirely replace Drupal's menu styles, they can just copy system-menus.css into their theme directory, add it to their theme's .info file, and change it completely. Drupal will ignore the core version of that CSS file, using the theme's instead, and themers will no longer have to grapple with ugly CSS specificity hacks to make their menu tricks fly.

We've made lots of improvements to Drupal 6's theming system, and the ability to build 'pure' CSS themes using images, .css files, and a descriptive .info file, has the potential to put Drupal theming within reach of a wider range of designers and web artists. Please test it and give it a spin: if we can get this committed, the tiny change will make D6's killer improvements even more powerful.

Comments on this post will automatically be closed three months from the original post date.

Comments

Why not simplify the system css ?

Imho it would be logical to change the system css to be more "generic". This way, it becomes easy for themers to overide the system css. The system css is often very interesting to keep, because they give good presentation for the admin/* pages.

A line must be drawn between styling of the drupal administration UI and the client website. Drupal system css should not have influence on the client menus.

I agree. Has anyone tried

I agree. Has anyone tried changing the specificness of the particular css selectors that cause problems?

Not really.

One of the problems is that changing the rules would impact all of the themes that DO exist for Drupal already. Drupal 6.0 is in 'feature freeze' beta, where changes that break existing APIs, interfaces, and so on can only be made in emergencies (breaking bugs, security fixes, etc.)

The solution used in the patch that was linked doesn't break any of those things, just makes it possible for themes to drop the old css file and insert their own.

That does not parse

a) That's what the 'admin theme' setting in Drupal is for

b) Making everything more modular/accessible is pretty much always a good idea. Conversely keeping things in one-monolithic-file makes modifications much more difficult to find/work-on.

c) There is no such thing as "generic css". Elements either have a style applied to them, or they don't. Even if Drupal didn't include any css that people would still have to worry about overriding all the different browser css settings out there. So again there is no such thing as 'generic css'. For any customization one is always overriding something.

d) The theme improvements in Drupal 6 will make it much easier to override things.

Related discussion:

Related discussion: http://groups.drupal.org/node/6471
and a work in progress: http://groups.drupal.org/node/6355