I have a site where each product image has a 1px solid border. The colour of the border is currently black. However, I would like the colour of the border to be definied by the category; for example all products with the parent category 'blue' should have a blue border, and all products with the parent category 'red' should have a red border and so on.

Is this achievable in Magento? I know the body tag of the page is given a unique CSS reference but I want the product image border colour to be defined by the top level category, not the child of that category.

Any ideas welcome!

  • You want to an listing page or Individual page
    – Magento 2
    Commented Nov 4, 2015 at 20:10
  • add one attribute text (colour) in category admin side and this attribute colour code value use in fronted
    – Abdul
    Commented Nov 5, 2015 at 6:10

3 Answers 3


Even if you have 100 sub-categories within a specific category you can apply the specific border color to the parent category at once. Let's say you have a category with 100+ sub-categories. Magento will add a .categorypath-living-subcategoryname css class to each sub-category. So each sub-category name is added to the .categorypath class. In this case you can use a CSS wild card to select the first part of the concatenated CSS class. Example can be found here: Using CSS wildcard


if you want to listing page you just check Body tag class name it automatically create class name based on category

<body class=" catalog-category-view categorypath-living-html category-living">

Here category-living is a category name so write a css

.catalog-category-view .category-living .category-products ul li img{border-color:green;}
.catalog-category-view.category-living-home .category-products ul li img{border-color:yellow;}

If it's Individual page

catalog-product-view .category-living-home .product-img-box .product-image{border-color:yellow;}

catalog-product-view .category-living .product-img-box .product-image{border-color:green;}

Or share your website link.

  • Thanks Kothari, but what about if I have a 100 sub-categories within 'living' - I wouldn't want to write 100 individual lines of code...as far as I can tell if this is the case Magento concatenates the body class, so it would become 'category-living-sub-cat-1' etc. Commented Nov 4, 2015 at 20:33

The cleanest way to add this functionality is to create a small module with an observer. I'd observe something category related (maybe catalog_controller_category_init_after).

Have the observer determine if the page is a sub category page, then using the code below add the class of the parent category.

if ($root = $this->getLayout()->getBlock('root')) {

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.