Magento 2: How to add css class to body tag programmatically

valir picture valir · Nov 16, 2016 · Viewed 7.5k times · Source

I am trying to figure out how to add css class to body tag programmatically using Magento 2 framework

Answer

koosa picture koosa · Dec 16, 2016

You can add a class to the body from a block by overriding the _prepareLayout method:

public function _prepareLayout(){
    $this->pageConfig->addBodyClass('my-class');
    return parent::_prepareLayout();
}