Community Documentation

Use a different style sheet for each page

Last updated October 18, 2011. Created by ugerhard on November 29, 2006.
Edited by MGParisi, SLIU, jhodgdon, jenlampton. Log in to edit this 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;
?>

Comments

Module Styles

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');
}

About this page

Drupal version
Drupal 4.7.x

Archive

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here