Adding a class to block Magento 2
There are 2 ways to do that:-
1- Add wrapping container
to your block
, like :-
<container name="div.header.links" htmlTag="div" htmlClass="header_links" after="-">
<block class="Magento\Framework\View\Element\Html\Links" name="header.links" >
</block>
</container>
2:- Add referenceContainer
tag, like :-
<referenceContainer name="header.links" htmlClass="css_class" htmlTag="div">
<block class="Magento\Cms\Block\Block" name="header.links">
<arguments>
</arguments>
</block>
You can add css as described below by the element
name="class"
<block class="Magento\Framework\View\Element\Html\Links" name="header.links">
<arguments>
<argument name="label" xsi:type="string" translate="false">Text</argument>
<argument name="path" xsi:type="string" translate="false">Text</argument>
<argument name="class" xsi:type="string">header links</argument>
</arguments>
</block>
Looking at the Magento\Framework\View\Element\Html\Link\Current
code for version 2.2.6, there is no class
argument supported (as documented in the previously accepted answer). However class
is an accepted attribute
. It can be used to set the css_class as follows:
<block class="Magento\Framework\View\Element\Html\Link\Current">
<arguments>
<argument name="label" xsi:type="string" translate="true">My Link</argument>
<argument name="path" xsi:type="string">my_page</argument>
<argument name="attributes" xsi:type="array">
<item name="class" xsi:type="string">the-class</item>
</argument>
</arguments>
</block>