2

Magento version: 2.0.0

How to add a class of style into tag? I think it is added in the xml, right?

2 Answers 2

7

Yes it's done via XML I reckon you can use the attribute tag for this

So in your layout, you can do:

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

An example in the default Magento install can be found here: https://github.com/magento/magento2/blob/2.0.0/app/code/Magento/Customer/view/frontend/layout/customer_account.xml

6
  • I added the htmlClass for <body> tag in the default.xml. then, i clear the cache but it not work for me.
    – Mike Zhang
    Commented Mar 19, 2016 at 10:29
  • @MikeZhang see my edit Commented Mar 19, 2016 at 11:30
  • 3
    Just a note on this functionality it is processed by vendor/magento/framework/View/Page/Config.php::addBodyClass() which strips out a lot of characters using regex and replaces them with - : $className = preg_replace('#[^a-z0-9]+#', '-', strtolower($className));
    – jzahedieh
    Commented Feb 6, 2017 at 15:25
  • +1 @jzahedieh, i.e. adding 'page--blocks' comes out on frontend as 'page-blocks' , annoying :/
    – 00-BBB
    Commented Sep 18, 2018 at 10:47
  • Also replaces underscores, why why why?!! :|
    – 00-BBB
    Commented Sep 18, 2018 at 10:52
1

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

0

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.