How can I change the favicon of a Magento 2 store by code in my custom theme? without using admin backend interface.


Magento will get the default favicon from Magento_Theme module.


 public function getDefaultFavicon()
        return 'Magento_Theme::favicon.ico';

So, in your custom theme, we can add favicon.ico:


If we want to know how Magento gets favicon in depth, we should take a look:


public function getFaviconFile() { ... }
public function getDefaultFavicon() { ... }
protected function prepareFaviconFile() { ... }


 public function prepareFavicon() { ... }

To override the default 16px x 16px favicon manually, add your custom favicon.ico in the /Magento_Theme/web/directory.

To add favicon icons of other sizes, take the following steps:

Add your icons in the /Magento_Theme/web/ directory. In the /Magento_Theme/layout/default_head_blocks.xml layout file specify the paths to the icons and their sizes.

For example, if you added a favicon-32x32.png icon and want it to be used as a 32px x 32px favicon, your default_head_blocks.xml would be like following:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <link src="Magento_Theme::favicon-32x32.png" rel="icon" sizes="32x32" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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