Creating custom header extending Magento blank in Magento 2

Creating custom header extending Magento blank in Magento 2

September 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Creating custom header extending Magento blank in Magento 2

Creating Custom Header Extending Magento Blank in Magento 2

Are you struggling to create a custom header for your Magento 2 theme using the Blank theme as a parent? You’re not alone! Let’s dive into how to properly modify the header structure and position it before the main content.

Understanding the Issue

The default layout.xml in the Magento Blank theme positions various blocks within the page wrapper. When you remove elements like header.container, they are still nested inside page.wrapper between main-content and footer. This is why your custom container appears after the main content instead of before it.

Solving the Problem

To create a custom header that precedes the main content, we need to make strategic changes in our theme’s layout.xml. Here’s how to do it:

  1. Remove unnecessary blocks: Start by removing the unwanted links and containers from the body section of your layout.xml.
<referenceBlock name="register-link" remove="true" />
<referenceBlock name="authorization-link" remove="true" />
<referenceBlock name="wish-list-link" remove="true" />
<referenceBlock name="my-account-link" remove="true" />
<referenceBlock name="top.links" remove="true" />
<referenceBlock name="header.panel.wrapper" remove="true" />
<referenceBlock name="header.container" remove="true" />
  1. Define the custom header container: Next, define your new custom.header container inside the page.wrapper. Set the HTML class and tag to control its appearance.
<referenceContainer name="page.wrapper">
    <container name="custom.header" htmlClass="headerSection" htmlTag="div">
        <!-- Your custom blocks will go here -->
    </container>
</referenceContainer>
  1. Place your custom block: Now, place your custom navigation block inside the custom.header container.
<block class="Magento\Framework\View\Element\Template"
       name="navigation.sections"
       before="-"
       template="Magento_Theme::html/sections.phtml">
</block>
  1. Set the main content to be nested: Finally, ensure that the main-content block is nested inside your custom header container.
<referenceContainer name="custom.header" htmlClass="headerSection" htmlTag="div">
    <block class="Magento\Framework\View\Element\Template"
           name="navigation.sections"
           before="-"
           template="Magento_Theme::html/sections.phtml">

        <!-- Your custom blocks will go here -->
    </block>

    <referenceBlock name="main-content" after="navigation.sections"/>

</referenceContainer>

Remember to Clear the Cache

After making these changes, don’t forget to clear your Magento cache to see the updates. You can do this by running:

php bin/magento cache:clean

Conclusion

Creating a custom header in Magento 2 with the Blank theme as a parent requires understanding how layout.xml and containers work together. By strategically removing unnecessary blocks, defining a new container for your custom header, and nesting the main content inside this container, you can achieve the desired layout.

Don’t hesitate to explore the vast resources available at LaravelCompany for more in-depth tutorials on Magento development and theme creation. Happy coding!

Magento 2 Themes Customers Errors

Our network