Drupal how-to: Custom CSS file for a node type

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

My CSS is getting a bit messy so I decided it might be good to separate out some of my CSS that is only needed for certain node types. Particularly books (need to change that default navigation!). To do this, I am using the following code in my page.tpl.php file:

<?php
   
global $theme_path;
    global
$base_path;
    if(
$node->type == "book") {
        print
"<link type=\"text/css\" rel=\"stylesheet\" media=\"all\" href=\"" . $base_path . $theme_path . "/css/book.css\" />";
    }
?>

Or is there a better way to do this? The template syntax allows you to insert additional CSS files but it doesn't look like you can put a conditional in depending on node type.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

Megan,

It looks like a good simple solution to me

do you use css caching? might be a problem with that...

did you try posting this question on drupal.org?

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.