By cmckay on
Quick question gang,
I am trying to make some changes to the css of a specific page on my site. How do I go about doing that? I know that I can create a specific page-name.tpl, but let's say I want to give the page a different background color, how do I go about doing that?
Comments
2 ways I can think of easily
2 ways I can think of easily are:
1) Give different class names and id's to the elements in your "page-name.tpl". Then you can have css rules in your style sheet to style them differently.
2) Tell Drupal to load an additional style sheet for that page by using drupal_add_css in your "page-name.tpl". You can then override styling rules or add new ones to change the way the page looks.
Generate unique CSS classes for <body>
The best way is to cause Drupal to generate unique CSS classes for
<body>based on the path of the page. You can then simply add a CSS rule likebody.page-1 .whateverandbody.page-2 .whateverThe code to do this comes from Zen theme (it's also common in many other themes). Since a few slight modifications are needed to convert Zen's code for general use, below is the modified code, which you can simply copy and paste into your theme.
1) Locate the
<body>tag in your page.tpl.php template, and replace it with<body class="<?php print $body_classes; ?>">2) Open (or create if you don't have one) your template.php file. Use the "Find" feature of your code editor to search for "preprocess_page". If you find anything, then you will need to merge the code rather than just paste in the whole thing (more on that later). If you find nothing, then you can simply copy and paste this code into your template.php:
If your theme's template.php already has a preprocess_page function, you need to merge the code into it. A code editor (such as Komodo Edit) with start/end { } highlighting should help make that pretty simple.
Also add this part separately outside of the preprocess_page function, to the bottom of your template.php:
If nothing happens, you may need to clear the theme registry. Visit the Themes page of your site and submit it with no changes (quick way to clear the theme registry). I always recommend this method over page-something.tpl.php if possible. I suggest only using different page templates if you absolutely must have completely different HTML and can't accomplish it with CSS). Having multiple page templates can get difficult to maintain if you make changes (if they're all basically the same page, you have to apply the same changes to all of them every time you change anything).
-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides
Thanks for the detailed
Thanks for the detailed reply Keyz.
So far so good...
Thanks, Keyz, for the detailed steps.
Keyz said:
The best way is to cause Drupal to generate unique CSS classes for based on the path of the page. You can then simply add a CSS rule like body.page-1 .whatever and body.page-2 .whatever
It would be helpful if there were more details or even a recipe on where/how to do this.
For example, how would I implement different CSS on:
how can i apply this in other forms??
this is only specific for the "body'..
just like what hardfocus said:
It would be helpful if there were more details or even a recipe on where/how to do this.
For example, how would I implement different CSS on:
* a specific node
* a specific content type (book, forum, page)
* a custom content type
we need an update to this one..
thanks by the way..
cheers...
Answers...
I realize this post is almost a year old, but I thought I should put an answer in for forum users searching for them:
The code above adds a "bunch" of classes to your body tag. So, it would add a class to the body tag that included the Node ID. So to theme a specific node you would start your selector with
body.node-10 .other-selectors-you-need-to-accomplish-your-goal. Same for a content type; your css selector would start with body.content-page (or whatever the actual class is in the body tag.) Just make your selectors specific enough to override the base styles.Tony
Anthony Pero
Project Lead
Virtuosic Media
http://www.virtuosic.me/