0

I have 3 stores view and I would like each of them loads a custom CSS file. I see that in content > design > configurations there is an entry for this (scripts and styles sheets) but when I put something like :

<link  rel="stylesheet" type="text/css"  media="all" href="{{MEDIA_URL}}store/firstStore.css" />

It tries to load a CSS file from this URL https://mywebsite.develop/%7B%7BMEDIA_URL%7D%7Dstore/firstStore.css

Have you got an idea?

1

I did this

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="head.additional">
        <block class="Vendor\Theme\Block\Head"/>
    </referenceContainer>
</page>


class Head extends \Magento\Framework\View\Element\Template
{
    /**
     * {@inheritDoc}
     */
    protected function _prepareLayout()
    {
        $storeCode = $this->_storeManager->getStore()->getCode();
        $storeCode = str_replace('_default', '', $storeCode);
        $this->pageConfig->addPageAsset('css/' . $storeCode . '.css');
        parent::_prepareLayout();
    }
}
0

if you have different locale for each store than you can add custom css files ike that in app/design/frontend/yourtheme/web/

/web/i18n/de_DE/css/firststore_style.css
/web/i18n/en_US/css/Second_store_style.css

in your theme deafult_head_block.xml

        <css src="/web/i18n/de_DE/css/firststore_style.css" />

hope it helps

  • Yes but how i create a custom layout xml per store ? I found nothing on the web :/ If i put this on a block on the after body start then, the css will not be in the head – help Jul 17 at 6:32
  • if you are using one theme for all stores then in your theme root directory in Magento_theme layout there you should have defeault_head_block.xml – Yasir Latif Jul 17 at 7:46
0

In admin panel, where you are trying to add CSS will not process the variable {{MEDIA_URL}} which you are using.

If you want to add the css file through admin then you need to give absolute URL of the css file like below.

<link  rel="stylesheet" type="text/css"  media="all" href="https://ENTER_YOUR_SITE_HERE.COM/PATH_TO_CSS/store/firstStore.css" />

The ideal way would be to add through your theme layout.

Hope this answers your query.

  • Yes but with this way the css will no be in pub/static folder and expose to customers the real css paths isn't that good – help Jul 17 at 6:30
  • Yes correct! but he was asking for how to add from admin so i mentioned this. Im presuming that he knows how to add it via theme layout. – Mrugesh Mistry Aug 28 at 8:08

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.