3

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>
1
  • 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 container Commented Oct 6, 2018 at 18:52

3 Answers 3

13

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 :)

6
  • 2
    I believe this will throw the error Element 'referenceBlock', attribute 'htmlClass': The attribute 'htmlClass' is not allowed. Commented Apr 26, 2017 at 11:58
  • In Magento 2.2.0 it will throw this error. Also it will throw it for <referenceContainer> block Commented Sep 28, 2017 at 16:28
  • 1
    <referenceBlock> or <referenceContainer> can be used for add htmlClass ? Commented Jun 12, 2018 at 13:58
  • It can't devdocs.magento.com/guides/v2.1/frontend-dev-guide/layouts/… Commented Jul 20, 2018 at 11:01
  • This question has been correctly answered here: magento.stackexchange.com/questions/134664/… Commented Sep 21, 2018 at 21:18
0

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='-'

-2

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>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.