{"id":7395,"date":"2019-10-04T12:33:52","date_gmt":"2019-10-04T07:03:52","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7395"},"modified":"2019-10-04T15:33:18","modified_gmt":"2019-10-04T10:03:18","slug":"magento-2-add-custom-class-and-attribute-body-tag","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-custom-class-and-attribute-body-tag\/","title":{"rendered":"Magento 2 &#8211; How to Add Custom Class and Attribute to the Body tag"},"content":{"rendered":"<p>In this post I am going to explain how to add attribute and class to the body tag in Magento 2<\/p>\n<p>Magento 2 adds classes to the body tag on every page to help the developers make custom designs per page. In Magento 2 the controller name and page layout name added as a class to the body tag. So now I am going to explain you how you can add your own class to the body tag and how you can add new attribute to the body tag.<\/p>\n<p>Add following code snippet in your layout file.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n\r\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\r\n    &lt;body&gt;\r\n        &lt;attribute name=\"class\" value=\"my-custom-class\"\/&gt;\r\n        &lt;attribute name=\"custom-attr\" value=\"attr-val\"\/&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/page&gt;<\/pre>\n<p>Now clear the cache and check for the result.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7396\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/10\/add-custom-class-body.png\" alt=\"Add Custom Class and Attribute to the Body tag\" width=\"1404\" height=\"151\" srcset=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/10\/add-custom-class-body.png 1404w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/10\/add-custom-class-body-300x32.png 300w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/10\/add-custom-class-body-768x83.png 768w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/10\/add-custom-class-body-1024x110.png 1024w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/10\/add-custom-class-body-800x86.png 800w\" sizes=\"(max-width: 1404px) 100vw, 1404px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to add attribute and class to the body tag in Magento 2 Magento 2 adds classes to the body tag on every page to help the developers make custom designs per page. In Magento 2 the controller name and page layout name added as a class&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-add-custom-class-and-attribute-body-tag\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7410,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7395"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7395"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7395\/revisions"}],"predecessor-version":[{"id":7397,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7395\/revisions\/7397"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7410"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}