How do you add a class to a pre existing xml block?
Example:
<container name="alert.urls" as="alert_urls" label="Alert Urls" after="product.price.tier"/>
 <block class="Magento\Catalog\Block\Product\View" name="product.info" template="product/view/form.phtml" after="alert.urls">
</container>
- 
 This question is unclear as to wether you are asking about adding a htmlClass to a block or container please clarify, as the responses assume block, if so you should edit the title to use the word "block" instead of containerJoel Davey– Joel Davey2018年10月06日 18:52:10 +00:00Commented Oct 6, 2018 at 18:52
3 Answers 3
Recently I've had the need for the exact same function. I decided to look at the core XML files, and found the following code to work:
<referenceBlock name="product.info" htmlClass="yourClassName canUseMutipleNames" />
I found the above code in the core file at this location:
/vendor/magento/module-theme/view/frontend/layout/default.xml
You can use this file as a reference and find some other useful tags like htmlTag :)
- 
 2I believe this will throw the errorElement 'referenceBlock', attribute 'htmlClass': The attribute 'htmlClass' is not allowed.Holly– Holly2017年04月26日 11:58:30 +00:00Commented Apr 26, 2017 at 11:58
- 
 In Magento 2.2.0 it will throw this error. Also it will throw it for<referenceContainer>blockMiroslav Petroff– Miroslav Petroff2017年09月28日 16:28:57 +00:00Commented Sep 28, 2017 at 16:28
- 
 1<referenceBlock>or<referenceContainer>can be used for add htmlClass ?lalit mohan– lalit mohan2018年06月12日 13:58:35 +00:00Commented Jun 12, 2018 at 13:58
- 
 It can't devdocs.magento.com/guides/v2.1/frontend-dev-guide/layouts/…Gediminas– Gediminas2018年07月20日 11:01:56 +00:00Commented Jul 20, 2018 at 11:01
- 
 This question has been correctly answered here: magento.stackexchange.com/questions/134664/…circlesix– circlesix2018年09月21日 21:18:55 +00:00Commented Sep 21, 2018 at 21:18
I have found some code in Magento XML core file
Please Try this code:
container name = "container_name" label="form" htmlTag="div" htmlClass="old-class" after='-'
Since Magento 2.3.2 you need to use arguments to referenceBlock/referenceContainer, for container/block use htmlClass
<arguments>
 <argument name="css_class" xsi:type="string">header links</argument> 
</arguments>
- 
 It does not work with referenceContainer please refer to github.com/magento/magento2/blob/2.4/lib/internal/Magento/…Christophe Ferreboeuf– Christophe Ferreboeuf2022年03月11日 14:31:56 +00:00Commented Mar 11, 2022 at 14:31