Customizing the header in Magento 2 can seem daunting at first due to its complex architecture and use of multiple technologies like HTML, CSS, LESS, and XML. However, by following these steps and leveraging some best practices from our team at Laravel Company (https://laravelcompany.com/magento-development), you can create a seamless custom header experience.
Understanding the Magento 2 Header Structure
The Magento 2 header consists of multiple components like the logo, top links, search form, cart, etc. These are defined by XML layouts and controlled through PHP files in your theme. By default, the header is located at app/design/frontend/<Vendor>/<Theme>/Magento_Theme/layout/default.xml.
Removing the Default Header
To start building your custom header, you’ll first need to remove the existing one. Open default.xml and find the following line:
<referenceContainer name="header.container">
<block class="Magento\Theme\Block\Html\Header" name="header" before="-"/>
</referenceContainer>
Comment out or delete this block.
Creating Your Custom Header Layout
Create a new layout file at app/design/frontend/<Vendor>/<Theme>/Magento_Theme/layout/custom_header.xml with the following content:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etclayouts.xsd">
<update handle="customer_logged_out"/>
<body>
<referenceContainer name="header.container">
<block class="<Vendor>\<Module>\Block\YourCustomHeader"
name="custom.header" template="Magento_Theme::html/your_custom_header.phtml"
before="-"/>
</referenceContainer>
</body>
</page>
Replace <Vendor> and <Module> with your actual vendor and module names.
Building Your Custom Header Block
Create the PHP block file app/code/<Vendor>/<Module>/Block/YourCustomHeader.php:
<?php
namespace <Vendor>\<Module>\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
class YourCustomHeader extends Template
{
public function __construct(Context $context)
{
parent::__construct($context);
}
}
This block will display your custom header using the provided HTML template.
Creating Your Custom HTML Header
Create the HTML file app/design/frontend/<Vendor>/<Theme>/Magento_Theme/templates/html/your_custom_header.phtml:
<div class="custom-header">
<!-- Add your HTML and CSS here -->
</div>
You can now add your regular HTML structure, including any CSS classes or IDs that correspond to your existing website.
Styling Your Custom Header
To apply styles to your custom header, you’ll need to use LESS. Create a new LESS file at app/design/frontend/<Vendor>/<Theme>/web/css/source/_custom_header.less:
.custom-header {
/* Add your CSS rules here */
}
Then include this file in your main styles.less file.
Conclusion
By following these steps, you’ve successfully removed the default Magento 2 header and created a custom one that matches the design of your existing website. Remember to clear your cache after each update (php bin/magento cache:clean).
At Laravel Company (https://laravelcompany.com/magento-development), we specialize in helping businesses like yours customize their Magento platforms for optimal user experiences and conversions. Contact us today to see how our expert team can assist with your specific needs!