18

I am trying to add a css class on body tag but not able to find any file, from where the body tag is rendered.

I need to add one css class on body tag such as others are coming > page-with-filter page-products page-layout-2columns-left myclasshere

in previous version below was the xml code to add class on body.

<reference name="root">
 <action method="addBodyClass"><classname>my-profile</classname></action>
</reference>

I tried using this but it didnt worked.

I need to add one class with "page-layout-2columns-left" template only.

43

In your layout or template file, you need to add an attribute to the body tag, like this:

<body>
    <attribute name="class" value="my-css-class"/>
    ...

Hope that Helps

  • Thanks Dave, now it is appearing on all pages, I just want this class which have layout of page-layout-2columns-left – Jack Mar 2 '16 at 6:30
  • Can't think of a way you'd easily achieve that with layout alone, other than targeting each page handle that uses 2column-left as a layout, much the same as M1. – Dave Farthing Mar 2 '16 at 7:18
  • 1
    I was just thinking to add the same lines in 2-columns-left.xml so this will target only this layout but its not working. – Jack Mar 2 '16 at 8:14
  • I just added the same on category.xml and it worked, thanks :) – Jack Mar 2 '16 at 10:59
  • how to add only for login user? – Sunil Patel Jun 8 '16 at 7:04
0

In case you want to add a dynamic variable, eg. Store Code to the body class, you can take a look at the following example I made: https://github.com/samgranger/StoreCodeBodyClass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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