How to add class to sidebar div in Magento layout?

How to add class to sidebar div in Magento layout?

August 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add class to sidebar div in Magento layout?

Adding Class to Sidebar Div in Magento Layout

As a senior developer working extensively with e-commerce platforms like Magento, I often encounter situations where clients need to customize specific pages without affecting the entire storefront. One common request is adding a class to the sidebar div on a particular page.

Understanding the Default Magento Layout Structure

By default, Magento uses XML layouts to define the structure and appearance of its pages. The 2-columns-left layout includes the following relevant line:

<div class="col-left sidebar">

This defines a div element with the classes col-left and sidebar.

Changing the Class: The Challenge

You might think you can simply modify the XML in your theme’s layout file (usually located at app/design/frontend/<Vendor>/<Theme>/layout). However, modifying this directly will apply the change to all pages using the 2-columns-left layout.

For example, if you add a new class like my-class:

<div class="col-left sidebar my-class">

The “my-class” attribute will be applied to every page that uses this layout. This includes not only your specific product page but also category pages and any other pages that use the two-column left layout.

A Targeted Solution: Using Layout Handles

To apply a class change to one specific page while keeping the original unchanged elsewhere, you need to leverage Magento’s layout handles system. Here’s how:

  1. Create a custom.xml file in your theme’s layout folder. If it doesn’t exist, create it under app/design/frontend/<Vendor>/<Theme>/layout.

  2. Add a new layout handle for your specific page. The handle name should be something like <your_namespace>_<pagename>. For example:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:.Framework/etc/layout.xml.xsd">
    <update handle="custom_page_handle" />
</config>

Replace custom_page_handle with your desired handle name.

  1. Reference the new layout in your product page’s XML. In your custom.xml file, add a reference to the new handle:
<referenceContainer name="sidebar.additional">
    <block class="Magento\Framework\View\Element\Template" template="Magento_Theme::template.phtml" after="-"/>
</referenceContainer>

Replace template.phtml with the actual template you want to modify.

  1. Add your custom class in the referenced file. In template.phtml, add the desired class:
<div class="col-left sidebar my-class">
    <!-- Your content here -->
</div>

Remember, the my-class is now applied only on pages using this specific layout handle.

Conclusion

Adding a class to a sidebar div in Magento requires an understanding of its XML layout system. By leveraging custom handles and referring them from your specific page’s layout file, you can apply style changes without affecting other parts of your store.

This approach allows for greater flexibility while maintaining the core theme’s integrity. It’s a best practice I often recommend to my clients at Laravel Company, as it keeps the codebase clean and easily maintainable.

If you have any questions or need further assistance with Magento development, feel free to reach out to our team for expert consultation.

Catalog Themes CLI

Our network