Back to Top

How to add attribute to the body tag in Magento 2

Updated 29 February 2024

Here I will explain how to add attributes to the body tag. Magento 2 by default adds the controller name and the page layout name as class to the body tag. In this blog I will explain how you can add your own class to the body tag and also I’ll add an attribute with its value.

In the layout file add the following,

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <attribute name="class" value="custom-class"/>
        <attribute name="data-var" value="some-data"/>
    </body>
</page>

The attribute tag is use add attributes to the body.

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home