Separate CSS file for different content types?

rbenhase - October 12, 2009 - 15:40

Hi, I'm relatively new to Drupal, so my question may seem a bit dumb.

I am creating a Drupal theme to use on a website, but there are essentially three types of page layouts on the website which take slightly different CSS (mostly involving only the background image, logo, and some margin stuff). I'm calling them "Normal Page," "Long Page," and "Special Page," respectively. I tried creating a different content type for each page layout, but I haven't been able to figure out how to call a different CSS stylesheet based on the content type. How could this be done? Or is there a more efficient way?

Thanks.

Some themes (e.g Zen) have

nirbhasa - October 12, 2009 - 16:03

Some themes (e.g Zen) have specific body classes for each content type, you can download that theme and look at the page template to see how they do it

http://drupal.org/project/zen

Then you can vary your CSS per contenttype easily - for example, to have a different h2 tag for your Long Pages, just style body.node-type-long-page h2

Thanks- that was helpful, but

rbenhase - October 12, 2009 - 20:51

Thanks- that was helpful, but I am still having problems. I notice that in the Zen theme directory, page.tpl.php assigns a class to the body tag... however, it uses a string ($body_classes) that apparently comes from a block of code in template.php. I'm not sure how to implement this into my own theme.

It seems odd to me that there's not a simple way to assign a different CSS stylesheet to a different content type in Drupal, but I'd like to figure something out. Thanks for your help.

You could create a base theme

nirbhasa - October 13, 2009 - 14:09

You could create a base theme with one subtheme per content type. Then assign each subtheme to the appropriate content type using themekey or taxonomy_theme modules

Actually, I played around

rbenhase - October 13, 2009 - 15:25

Actually, I played around with your first suggestion and it works just fine. I had to create a template.php file for my theme and copy the code from Zen's template.php file, but then one quick edit to page.tpl.php (to add the class to the body tag) and everything was a breeze! Thanks a lot!

Node Type Theme module

MBroberg - November 14, 2009 - 00:56

Glad you found a solution, but here's another possibility, in case someone else is looking to do the same.
http://drupal.org/project/nodetypetheme

 
 

Drupal is a registered trademark of Dries Buytaert.