add body class to content types

glepiza
glepiza used Ask the Experts™
on
Hi experts,

Do you know how can I add a body class to an specific content type? thanks in advance,

Winter
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
How to apply body class to all contents type

HTML Code:

<body class="body">
    <form id="form1" runat="server">
    <div id="div1">
      <div>


      </div>
    </div>
    <div id="div2">

     </div>
     </br>
     </br>
      <textarea id="txtare1" runat="server">Hi glepiza, How are you ? </textarea>
     </br>
     <input id="txtbox1" type="text" value="123456" />
    </form>
</body>
</html>

CSS Code:

 <style type="text/css">

        .body #div1 {
            background-color: red;
            height: 150px;
            width: 150px;
        }

        .body #div1 div {
           
            margin-top:30px;
            background-color:yellow;
            border-style:dashed;
        }

        .body #div2 {
       
         background-color:green;
         height:150px;
         width:150px;
       
        }

        .body textarea {
       
         background-color:white;
         width:300px;
         color:blue;
         font-style:italic;
         font-size:medium;
        }

       .body input {
       
         background-color:purple;
         color:silver;
         border-style:double;
        }


    </style>

Author

Commented:
Hi PatelRitesh

Thanks for your answer. I am not sure if you understood my question. I need to know specifically how you add that class to a custom Content type. Do I need to create a custom module? or a function to template.php? if so, what do I need to add? I´ve been looking around with no luck.

Thanks,

Winter
Aaron FeledyDrupal Developer and Consultant

Commented:
Hi Winter,
I think PatelRitesh didn't realize you were asking about Drupal since it wasn't specifically mentioned in the question text. In Drupal, the best way to do this would be through your theme using the template.php file. Add the following to that replacing "mytheme" with the name of your theme and my-class with the name of the class you'd like to add:
function mytheme_preprocess_html(&$vars) {
  $vars['attributes_array']['class'][] = 'my-class';
}

Open in new window

Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Hi Arrow, thanks for your answer. and yup, you are right, I did not specify Drupal in my question.

One question, would your code add the class to an specific content type for example, animals? or it would add the class to the entire site?

Thanks
Drupal Developer and Consultant
Commented:
Oops, I forgot to specify for content type... In that case it would look something like this:
function mytheme_preprocess_html(&$vars) {
  if ($vars['node']->type == 'animals') {
    $vars['attributes_array']['class'][] = 'my-class';
  }
}

Open in new window

This is just off the top of my head and may need some adjusting, but I think it should work.

Author

Commented:
Thanks!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial