Request Quote

How to add attribute to the body tag in Magento 2

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.

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php