Back to Top

How to add attribute to the body tag in Magento 2

Updated 4 December 2025

Magento 2 automatically adds the controller and layout names as classes to the body tag. In this blog, we’ll show how to add your own custom body class and include an additional attribute with a specific 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.

You may also check our quality Adobe Commerce Extensions.

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