1

I use Drupal 6.

Everytime I use drupal_set_html_head, my <style> code is being added before my theme's <link> entries.

Basically, I want to override the theme's css files with internal css, so I need my <style> to appear at the very end of <head>

I hope it is clear. Thanks!

  • You could always use css specificity to override the theme's style rules. It might not be optimal, but it will save you from editing the template files. – hampusn Nov 8 '12 at 15:26
  • How do you use css specificity with modules? – Bibokid Nov 9 '12 at 1:02
  • There's not a specific module way. Just override by using a higher specificity, a lazy way would be to copy the rules you want to override and add another class or id in the selector. – hampusn Nov 9 '12 at 1:25
1

Everything you add with drupal_set_html_head() is added before the CSS styles added with drupal_add_css(). In fact, the page template outputs them using the following code.

<head>
  <?php print $head; ?>
  <title><?php print $head_title; ?></title>
  <?php print $styles; ?>
  <?php print $scripts; ?>
  <script type="text/javascript"><?php /* Needed to avoid Flash of Unstyled Content in IE */ ?> </script>
</head> 

$head and $styles are initialized in template_preprocess_page() with the following code.

$variables['head'] = drupal_get_html_head();
$variables['styles'] = drupal_get_css();

As drupal_add_css() is used to add all the CSS styles used by a theme, including the ones defined in its .info file, you can change the order the CSS styles are added by changing the page template. If it would contain the following code, everything added with drupal_set_html_head() will come after everything added with drupal_add_css(). I would not suggest doing this, if not after verifying it doesn't cause any problem.

<head>
  <title><?php print $head_title; ?></title>
  <?php print $styles; ?>
  <?php print $head; ?>
  <?php print $scripts; ?>
  <script type="text/javascript"><?php /* Needed to avoid Flash of Unstyled Content in IE */ ?> </script>
</head> 

Notice that with Drupal 7, you can use drupal_add_css() to use inline CSS style too: Just pass the CSS styles as first argument, and pass array('type' => 'inline', 'weight' => 100) as second argument. You need also to use the group property. In that way, you don't need to alter the template file just to add your CSS styles after the ones added by Drupal.
Generally, it is preferable to add the CSS styles in a file; in that way, they can be aggregated/compressed.

  • 1
    thank you kiamlaluno. This is definitely the correct conclusion regarding my problem. I ended up creating a custom variable on mymodule_preprocess_page(), and then echoing it on the theme page.tpl.php. I really wanted to do it without changing anything on the theme files, but I guess i have no other options :) – Bibokid Nov 8 '12 at 10:21
  • 1
    drupal_add_css for D7 allows you to specify the weight which makes it possible to place the stylesheets in whatever order you'd like. This might not help you now since you use D6, but it might be good to know for future reference (if you upgrade). – hampusn Nov 8 '12 at 15:23
  • thank you for that information hampusn. I will certainly make use of it in the future. – Bibokid Nov 9 '12 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.