Use a different style sheet for each page

I wanted a different style sheet for each page on my site so what I did was add a chunk of php code to the header of my page.tmpl.php file to include a style sheet with the same name as the value for q for each page. For example, the contact page (http://www.yourdrupalsite.com/?q=contact) includes the stylesheet contact.css. Here's what I did:

Locate the line:

<?php
print $styles
?>

And replace it with this:

<?php
 
if($_REQUEST['q']) {
              
$style_var = explode("/", $_REQUEST['q']);
              
$styles .= '<style type="text/css" media="all">@import
url(/path/to/your/styles/'
.$style_var[0].'.css);</style>';
               }
              print
$styles;
?>

Module Styles

mconway - November 20, 2007 - 22:00

A way to adapt this for use in a module, for a specific stylesheet, or js file, or anything really, would be:

$style_me = explode("/", $_REQUEST['q']);
if ($style_me[0] == 'module_name'){
drupal_add_css('custom_css.css','module','screen');
}

 
 

Drupal is a registered trademark of Dries Buytaert.