Adding attribute or class to the body tag in Magento 2

add custom class and attribute to body tag

In this tutorial we will explain how you can add your own class to the body tag and also I’ll add an attribute with its value in Magento 2.

See below for a more detailed example.

<?xml version=”1.0″?>
<page xmlns:xsi=”” xsi:noNamespaceSchemaLocation=”urn:magento:framework:View/Layout/etc/page_configuration.xsd”>
             <attribute name=”class” value=”custom-class”/>
              <attribute name=”data-var” value=”some-data”/>

Now clear the cache and check for the result.

The attribute tag is use add attributes to the body.

add class to body magento2