29

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 didn't worked.

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

Roman Snitko
  • 786
  • 5
  • 15
Jack
  • 460
  • 1
  • 4
  • 10

2 Answers2

63

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

tim.baker
  • 843
  • 1
  • 12
  • 29
Dave Farthing
  • 1,004
  • 8
  • 6
  • Thanks Dave, now it is appearing on all pages, I just want this class which have layout of page-layout-2columns-left – Jack Mar 02 '16 at 06: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 02 '16 at 07: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 02 '16 at 08:14
  • I just added the same on category.xml and it worked, thanks :) – Jack Mar 02 '16 at 10:59
  • how to add only for login user? – Sunil Patel Jun 08 '16 at 07:04
3

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

Sam Granger
  • 141
  • 1