4

I want to modify dynamically the style of a page in base of your content type: I tried with a preprocess function insert this code:

if(isset($variables['node'])){
  if($variables['node']->type=='article'){
    drupal_add_css(drupal_get_path('theme','my_theme').'/css/style_node_article.css');
  }
  if($variables['node']->type=='page'){
    drupal_add_css(drupal_get_path('theme','my_theme').'/css/style_node_page.css');
  }
}

This doesn't set the right CSS if I change page from article to page... Thanks

2
  • looks right. did you cleaer the cache. You can also do if(isset($node)), $node is available on any node page
    – iStryker
    Jul 13, 2011 at 15:12
  • Yes i have made that you tell me but when i visited a page the style is the same of the article and the condition is not right! Jul 13, 2011 at 15:17

1 Answer 1

6

Unless you have a ton of CSS you should add the CSS for all the nodes and use the node type class on the wrapper div of the node.

I can't remember the exact classes you can view them in the source. You could do something like this

.node {}
.node-page {}
.node-blog {}

Where the first class is on all nodes and the latter is dynamic for the node.

7
  • This is a way but i want to add css conditionally on a page based on a node type.. problably i dont understand what you tell me....sorry for english.. Jul 13, 2011 at 15:29
  • It's simple: do not use conditional css files (unless they are very, very large). Use the css selectors like div.node-blog instead to target specific content types. Jul 13, 2011 at 15:39
  • I dont understand how to use the selector to modify all the style of a page and not one single css property.. Jul 13, 2011 at 15:48
  • Also, if you're using a sub-theme such as Zen, it gives you handy classes in the body tag with details of the content type.
    – thatboyjim
    Jul 13, 2011 at 17:31
  • Body classes are not limited to the Zen theme. Both D6 and D7 generate a number of body classes, regardless of the theme (but it's up to the theme to print them). This means that you can target every element in your page with a selector based on the body class. For instance: body.node-type-blog a { color: #C00 } would color all links red when viewing a blog node. Jul 13, 2011 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.