One common practice when using CSS frameworks such as 960 Grid System, Blueprint, or Baseline is to use a CSS reset. Each web browser applies a set of default styles to HTML elements, and these styles vary among browser vendors. A CSS reset is a stylesheet that clears these default styles so that you know what you’re working with as you implement your theme’s CSS.
The caveat with a CSS reset is that it needs to come before all of your other stylesheets. This presents a problem if you want to use a reset in your Drupal theme: all of the theme’s CSS will be added after Drupal’s system CSS and after any modules’ CSS. If your reset is loaded after these, the system and modules’ styles will all be undone, which probably isn’t what you want. Drupal loads theme styles last because, usually, you’re just adding to or overriding the existing styles, not wiping them all clean. It is possible, however, to have Drupal output a stylesheet from your theme before the system and module stylesheets.
In Drupal 6, the easiest approach is to hard code the link to the reset stylesheet in your page.tpl.php template. Unfortunately, this approach doesn’t take advantage of the built-in CSS aggregation and caching capabilities that Drupal offers, and it might get lost by a sub-theme that provided its own page.tpl.php template. Doing it programmatically in your theme isn’t easy, but a good example can be found in the ninesixty_css_reorder function in Joon Park’s NineSixty theme.
Drupal 7 makes life easier with an enhanced drupal_add_css function. CSS files can now be added to groups and then weighted within those groups to control the order of their presentation. Three numeric constants for groups are provided:
CSS_SYSTEM: Any system-layer CSS.CSS_DEFAULT: Any module-layer CSS.CSS_THEME: Any theme-layer CSS.
This is the actual ordering of the groups; stylesheets in the CSS_SYSTEM will be added first, followed by those in the CSS_DEFAULT group, and finally by your theme’s CSS files which are automatically added to the CSS_THEME group. At first glance, this may not appear to help much, but your theme can put its stylesheets in groups other than CSS_THEME. Consider this code snipet from a template.php file:
/** * Preprocesses the wrapping HTML. * * @param array &$variables * Template variables. */ function yourtheme_preprocess_html(&$variables) { $reset = drupal_get_path("theme", "yourtheme") . "/reset.css"; $options = array( 'group' => CSS_SYSTEM, 'weight' => -10, ); drupal_add_css($reset, $options); }
Assuming that your reset.css is listed in yourtheme.info file (required in Drupal 7), your reset is moved to the same group as Drupal’s system CSS files. So what weight should you use to make sure your CSS gets loaded first? Well, really, you don’t know unless you dig through Drupal’s source to figure out how the system stylesheets are weighted.
Or you can get a bit promiscuous.
Remember that those stylesheet groups are actually numeric? CSS_SYSTEM, CSS_DEFAULT, and CSS_THEME are (currently) the numbers -100, 0, and 100, respectively. The groups are output by their natural numeric ordering, so let’s change one line in the example above:
/** * Preprocesses the wrapping HTML. * * @param array &$variables * Template variables. */ function yourtheme_preprocess_html(&$variables) { $reset = drupal_get_path("theme", "yourtheme") . "/reset.css"; $options = array( 'group' => CSS_SYSTEM - 1, 'weight' => -10, ); drupal_add_css($reset, $options); }
We’ve changed the group from CSS_SYSTEM to CSS_SYSTEM - 1 , giving it a value of -101. This creates a new grouping that is numerically less than CSS_SYSTEM causing our reset to be output before the system stylesheets.
So why is this promiscuous? This tecnique relies on an implementation detail of Drupal’s internals that could potentially change. Techncially, you’re abusing the system, and you should only use it when you have a good reason. Of course, you can also just hard code the reset in your html.tpl.php template, but this carries the same disadvantags as it did in Drupal 6.
Commenting on this Blog post is closed.

Comments
Why not set the weight to an outrageous negative number? Makes more sense to me, as that’s what weight is for.
I don’t see any problem, as the exact ordering of stylesheets is as follows:
First by group
Then by the ‘every_page’ flag, with TRUE coming before FALSE. (reset.css appears on every page, i.e. set to TRUE)
Then by weight.
The problem with using an “outrageous negative number” is that it’s indeterminate and requires knowledge of every weight used in core. In order to ensure your number was negative enough, you would need to examine the entire code base, and a future patch could introduce a more negative weight in the group. This is what is known as a magic constant.
The grouping number
CSS_SYSTEM - 1relies on the public interface and the contract specified in the drupal_add_css documentation: “the group number serves as a weight.” It is deterministic in that it is always one less than a publicly decalred constant.In general, it is a best practice to prefer the approach that is the most deterministic and that requires the least knowledge of the system’s internal workings.
postscript: One might argue that
CSS_SYSTEMcould be an extrema such as~PHP_INT_MAX(PHP doesn’t provide a minimum integer constant), but this is a platform-dependent value, and it would go against Drupal core’s inclination towards reasonable values for declared constants.When I was porting ninesixty to Drupal 7, I discovered
hook_preprocess_css. To me, this seemed to be the cleanest way to manipulate CSS ordering in D7. I decided thatCSS_SYSTEM * 5, was “good enough” for now. The patch is attached to an issue. Feedback welcome.Reordering the CSS in
hook_css_alter(which is what your patch uses) accomplishes the same thing, but my personal inclination is not to rely on regular expressions to do the reordering when a more direct and more readable approach is available.The problem I see with using
CSS_SYSTEM * 5is that it isn’t obvious what’s going on. The result of (x * 5) is only less than x if x is negative, and unless the reader already knows thatCSS_SYSTEMis less than zero, the assumption will be that the result is five times greater.The result of (x - 1), however, is always less than x, regardless of the sign of x, and it holds true even if x is zero.
Someone reading your code to learn theming may not understand your intent just from reading the documentation for
drupal_add_cssandhook_css_alter. And it’s reasonable for a reader to expect the same behavior if they replacedCSS_SYSTEMwith another of the grouping constants, but consider what happens if they do:Subtracting one, however, yields consistent behavior regardless of the grouping constant used:
In this case, both approaches produce the same effect, but when we are writing code for others to extend or learn from, we should prefer logic that gives the most consistent behavior.