6

I have the following custom xml inside of my Magento_theme> layout> default.xml

<referenceBlock name="top.links">
 <block class="Magento\Framework\View\Element\Html\Link" name="contactus.link" after="my-account-link">
 <arguments>
 <argument name="label" xsi:type="string" translate="false">Contact Us</argument>
 <argument name="path" xsi:type="string" translate="false">contact-us</argument>
 </arguments>
 </block>
 <block class="Magento\Framework\View\Element\Html\Link" name="aboutus.link" after="contactus.link">
 <arguments>
 <argument name="label" xsi:type="string" translate="false">About Us</argument>
 <argument name="path" xsi:type="string" translate="false">about-us</argument>
 </arguments>
 </block>
 <referenceBlock name="register-link" remove="true" />
 <referenceBlock name="authorization-link" remove="true" />
 <referenceBlock name="wish-list-link" remove="true" />
 <reeferenceBlock name="my-account-link" remove="false" />
 </referenceBlock>

Im removing blocks and adding custom ones in.

What I'd like to to is add a class to the two custom blocks - aboutus.link and contactus.link - how do i go about doing this? Or can it not be done with xml.

asked Sep 4, 2016 at 14:07

3 Answers 3

7

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>
andy jones
1,3832 gold badges23 silver badges50 bronze badges
answered Sep 4, 2016 at 14:42
1
  • adding class creates an exception in 2.1.2 => section source is not supported, adding css_class doesn't create on but then the class is never added Commented Mar 7, 2017 at 12:27
9

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>
coderGeek
5458 silver badges20 bronze badges
answered Apr 8, 2017 at 16:22
2
6

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>
answered May 9, 2019 at 15:40
2
  • not work for me . i used both <argument class="class" xsi:type="string">the-classname</argument> and <argument name="attributes" xsi:type="array"> <item name="class" xsi:type="string">the-class</item> </argument> my current version is 2.2.6 I include like this <head> <css src="css/firstfile.css" media="all and (min-width: 1px)"/> </head> Commented Jun 25, 2019 at 10:46
  • Thanks a lot. It is working! Where do I find documentation regarding these XML layout arguments? Commented Aug 19, 2021 at 17:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.