Community

How do I use custom css only for nodes created by a custom module

I have a custom module in Drupal 7 that creates nodes. I want a custom css stylesheet (customstyle.css) applied when one of the nodes created by my custom module is rendered, but only when one of the nodes created by my custom module is rendered.

The simple solution (adding stylesheets[all][] = customstyle.css to my custom module's .info-file) applies the style sheet to all pages, including pages containing nodes not created by my custom module. I do not want that.

Comments

Are the nodes created by your

Are the nodes created by your module a unique node type?

If so, look at the body tag, most/many themes will have a class of the form 'node-nodetype' where nodetype is the actual node type. This will allow you to write css rules like

body.node-nodetype .some-other-selector {
....

that allow you to theme any element on a node/content page.

Also most themes include a class in the node wrapper div that reflects the content type. You can use that to target the contents html and can be applied to full nodes as well as teasers.

Thanks, but that still loads the stylesheet

Thanks, but that still loads the stylesheet on every node (it just makes sure its rules only apply to the appropriate nodetype).

I ended up sticking the follwing in hook_node_view():

if ($node->type == 'my_nodetype') {
  drupal_add_css(drupal_get_path('module', ',my_nodetype') . '/customstyle.css');
}
nobody click here