Currently, I am working to HTML to Magento 2 conversion and I got the HTML from the designer which have the class in the body so I added it generally add

in default.xml file in my Magento theme

but the main problem is body class is different for other pages


body class for home page is "home_page_body_class"

and for other pages is "other_page_body_class"

I do not understand how to achieve it.

If anyone has an idea about that please share me.

1 Answer 1


You can do this by using a plugin.


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">   
<type name="Magento\Framework\View\Result\Page">
    <plugin name="myModuleResultPage" type="Myvendor\Mymodule\Plugin\Result\Page"/>


namespace Myvendor\Mymodule\Plugin\Result;

use Magento\Framework\App\ResponseInterface;

class Page
    private $context;

    public function __construct(
        \Magento\Framework\View\Element\Context $context
    ) {
        $this->context = $context;

    public function beforeRenderResult(
        \Magento\Framework\View\Result\Page $subject,
        ResponseInterface $response
        if($this->context->getRequest()->getFullActionName() == 'checkout_cart_index'){
        return [$response];

Reference: https://www.mexbs.com/magento-blog/adding-a-custom-body-class-to-a-page-in-magento-2/

Hope it helps!!!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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