Magento Top Nav: Add CSS class to first category using PHP

Magento Top Nav: Add CSS class to first category using PHP

September 22, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Top Nav: Add CSS class to first category using PHP

Customizing Magento Navigation.php: Adding CSS Class to First Category using PHP

Magento provides built-in top navigation functionality that can be customized for specific requirements such as adding CSS classes to the first category in the menu. This guide will help you understand how to modify the Navigation.php file to achieve this.

Objective: Add a CSS class named ‘sfHover’ to the first category in Magento’s top navigation bar, allowing Superfish to handle the expanded dropdown behavior correctly.

Understanding Magento’s Top Navigation Construction

Magento constructs its top navigation using PHP, which involves determining each category’s position and applying relevant CSS classes. The key functions responsible for this process are:

  1. _getItemPosition($level): This function calculates the position of a category in the top navigation hierarchy. It’s called recursively to determine the position of subcategories.

  2. if ($this->isCategoryActive($category)) { $classes[] = 'active'; }: This line checks if the current category is active (i.e., the page being viewed) and adds the ‘active’ CSS class if true.

  3. if ($isFirst) { $classes[] = 'first'; }: Magento automatically adds a ‘first’ class to the first item in each level of the navigation.

Our goal is to add an extra CSS class (‘sfHover’) specifically to the first category, which will be used by Superfish for its dropdown functionality.

Modifying Navigation.php: Adding the CSS Class

To achieve this, open your Magento installation’s app/code/core/Mage/Catalog/Block/Navigation.php file and locate the following code block:

$isFirst = ($level == 0 && $position == 1);
if ($isFirst) {
    $classes[] = 'first';
}

Here, we need to add our custom CSS class (‘sfHover’) when $isFirst === true. Update this section as follows:

$isFirst = ($level == 0 && $position == 1);
if ($isFirst) {
    $classes[] = 'first';
    $classes[] = 'sfHover'; // Add the custom CSS class here
}

Verifying the Changes

After making this modification, clear your Magento cache to ensure the changes take effect:

  1. Log in to your Magento admin panel.
  2. Navigate to System > Cache Management.
  3. Click on Flush Cache Storage.

Once the cache is cleared, visit your Magento store’s front-end. You should now see that the first category in the top navigation has both ‘first’ and ‘sfHover’ CSS classes applied:

<li class="level0 nav-1 level-top first sfHover parent">

Conclusion

By following this guide, you have successfully customized Magento’s Navigation.php file to add a custom CSS class (‘sfHover’) to the first category in your top navigation bar. This modification enables Superfish to handle the expanded dropdown behavior for the first tab correctly.

Remember that modifying core files directly can lead to issues during upgrades. To avoid this, consider creating a local copy of the Navigation.php file and overriding it using a custom module or theme. This approach maintains the separation between your customizations and Magento’s core code.

For more advanced customization techniques and best practices in Magento development, visit LaravelCompany to explore our comprehensive resources on Magento programming and web development solutions.

Catalog Themes

Our network