{"id":1024,"date":"2020-05-26T14:30:47","date_gmt":"2020-05-26T14:30:47","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/05\/26\/add-custom-css-class-through-xml-file-in-magento-2\/"},"modified":"2025-05-22T12:58:17","modified_gmt":"2025-05-22T07:28:17","slug":"add-custom-css-class-through-xml-file-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-custom-css-class-through-xml-file-in-magento-2\/","title":{"rendered":"How to Add Custom CSS Class Through XML File in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 is widely popular among the developers\u2019 community, owing to its insane capability to customize and tweak the default configuration and code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With increasing online stores and various types of businesses built with Magento 2 as the platform, there ought to be crazy requirements to fulfill customers\u2019 expectations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Recently, I found myself in the exact same scenario where I was required to&nbsp;<em><strong>add custom CSS class through XML file in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As it is not recommended to completely change the layout of the XML, I preferred the below solution:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to add custom CSS class through XML file in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The below example is to add CSS class in the product page:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the below code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n      xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceContainer name=\"product.info.media\" htmlClass=\"custom col-lg-4 col-md-12 col-sm-12 product media\" \/>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Removing blocks and adding custom CSS\/HTML classes are the usual requirements during store development. So, please bookmark this solution which is surely going to help you someday!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with the Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also Read:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/meetanshi.com\/blog\/enable-merge-css-javascript-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Enable Merge CSS, Javascript in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/override-phtml-file-using-custom-module-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Override a phtml File Using a Custom Module in Magento 2<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 is widely popular among the developers\u2019 community, owing to its insane capability to customize and tweak the default configuration and code. With increasing&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1024","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1024"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1024\/revisions"}],"predecessor-version":[{"id":14895,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1024\/revisions\/14895"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}