How to add class in top links?

How to add class in top links?

July 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add class in top links?

Adding Classes to Top Links in Magento

Adding a class to top links can help you style specific menu items or apply unique functionality using CSS or JavaScript. In this guide, we’ll explore how to correctly add classes to top links in Magento, focusing on version 1.7.0.0.

Understanding the XML Structure

Before we dive into the code, let’s understand the basic structure of a top link definition:

<reference name="top.links">
    <action method="addLink" translate="label title">
        <label>My Account</label>
        <url helper="customer/getAccountUrl"/>
        <title>My Account</title>
        <prepare/>
        <!-- Additional parameters go here -->
        <position>10</position>
    </action>
</reference>

The Incorrect Approach: Using <aParams> Tag

Many developers attempt to add a class using the <aParams> tag:

<reference name="top.links">
    <action method="addLink" translate="label title" module="customer">
        <label>My Account</label>
        <url helper="customer/getAccountUrl"/>
        <title>My Account</title>
        <prepare/>
        <aParams><class>top-link-myaccount</class></aParams>
        <position>10</position>
    </action>
</reference>

However, this approach doesn’t work as expected in Magento 1.7.0.0 due to the way the addLink method processes parameters.

The Correct Approach: Wrapping <li> and <a> Tags

The correct way to add a class is by wrapping the <a> tag within the <li> element:

<reference name="top.links">
    <action method="addLink" translate="label title" module="customer">
        <label>My Account</label>
        <url helper="customer/getAccountUrl"/>
        <title>My Account</title>
        <prepare/>
        <position>10</position>
        <li><a class="top-link-myaccount"></a></li>
    </action>
</reference>

Here’s the key:

  • We place the <li> element inside the addLink action.
  • Inside this <li>, we include an empty <a> tag with the desired class attribute.

This method ensures that the class is correctly applied to the top link in Magento 1.7.0.0 and later versions.

Why This Works

The <li> element acts as a wrapper for the top link, allowing you to apply specific styles or behaviors using CSS or JavaScript targeting the parent list item. When Magento renders the links, it respects this structure, ensuring your class is applied correctly.

Best Practices

  1. Use descriptive and unique classes for each top link.
  2. Avoid hardcoding classes directly in the layout XML file; consider defining them in your theme’s local.xml to maintain a separation of concerns.
  3. Make sure to flush Magento’s cache after making changes to the layout configuration.
php bin/magento cache:flush

Conclusion

Adding classes to top links in Magento is a crucial aspect of customizing the user interface and applying specific styles or behaviors. By understanding the correct XML structure and wrapping <a> within <li>, you can achieve your desired results.

Remember, always flush the cache after making layout changes to ensure your modifications take effect immediately. For more advanced customizations and best practices in Magento development, visit Laravel Company’s Magento resources.

Magento 1 Extensions Themes Customers

Our network